why isn't strlcpy written like this:

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

why isn't strlcpy written like this:

Luke Small
strlcpy is:

size_t
strlcpy(char *dst, const char *src, size_t dsize)
{
  const char *osrc = src;
  size_t nleft = dsize;

  /* Copy as many bytes as will fit. */
  if (nleft != 0) {
  while (--nleft != 0) {
    if ((*dst++ = *src++) == '\0')
      break;
  }
}

/* Not enough room in dst, add NUL and traverse rest of src. */
  if (nleft == 0) {
    if (dsize != 0)
      *dst = '\0'; /* NUL-terminate dst */
    while (*src++)
           ;
}

  return(src - osrc - 1); /* count does not include NUL */
}



why isn't it like this (this version is faster too):

size_t
strlcpy2(char *dst, const char *src, size_t dsize)
{
  const char *osrc = src;

  if (dsize != 0) {
    /* Copy as many bytes as will fit. */
    while (--dsize != 0)
      if ((*dst++ = *src++) == '\0')
        return(src - osrc - 1);
    *dst = '\0';
  }

  /* Not enough room in dst, traverse rest of src. */
  while (*src++)
           ;

  return(src - osrc - 1); /* count does not include NUL */
}



-Luke