#ifndef GCISTRING_HF
#define GCISTRING_HF TRUE
/*=========================================================================
 * Copyright (C) VMware Inc. 1986-2011.  All Rights Reserved.
 *
 * Name - gcistring.hf
 *
 * Description - inline implementations of strlcpy, etc 
 *
 * $Id: gcistring.hf,v 1.5 2008-01-09 22:51:04 stever Exp $
 *
 *========================================================================
 */
#include "flag.ht"

#include "stdio.h"
#if (defined(FLG_MSWIN32) || defined(FLG_HPUX_UNIX) || defined(FLG_AIX_UNIX)) || defined(FLG_LINUX_UNIX)
#include <sys/types.h>
#include <string.h>

/* these platforms don't have strlcpy and strlcat, so implement here */

static size_t strlcpy(char *dst, const char *src, size_t siz) {
  char *d = dst;
  const char *s = src;
  size_t n = siz;
  /* Copy as many bytes as will fit */
  if (n != 0 && --n != 0) {
    do {
      if ((*d++ = *s++) == 0)
        break;
    } while (--n != 0);
  }
  /* Not enough room in dst, add NUL and traverse rest of src */
  if (n == 0) {
    if (siz != 0)
      *d = '\0'; /* NUL-terminate dst */
    while (*s++);
  }
  return(s - src - 1); /* count does not include NUL */
}

static size_t strlcat(char *dst, const char *src, size_t siz) {
  char *d = dst;
  const char *s = src;
  size_t n = siz;
  size_t dlen;
  /* Find the end of dst and adjust bytes left but don't go past end */
  while (n-- != 0 && *d != '\0')
    d++;
  dlen = d - dst;
  n = siz - dlen;
  if (n == 0)
    return(dlen + strlen(s));
  while (*s != '\0') {
    if (n != 1) {
      *d++ = *s;
      n--;
    }
    s++;
  }
  *d = '\0';
  return(dlen + (s - src)); /* count does not include NUL */
}
#endif
#endif
