httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Jagielski <...@jaguNET.com>
Subject Re: strncpy is evil
Date Tue, 23 Dec 1997 14:08:26 GMT
Dean Gaudet wrote:
> 
> +
> +char *strncpy(char *dst, const char *src, size_t len)
> +{
> +    char *d = dst;
> +
> +    for(;;) {
> +	if (!len) break;
> +	if (!(*d++ = *src++)) break;
> +	--len;
> +    }
> +    return dst;
> +}
> 

Any reason why you didn't use a while?

char *ap_strncpy(char *dst, const char *src, size_t len)
{
   char *d = dst;
   char *end = dst+len;
   
   while ( ; len && (*d++ = *src++) ; len-- )
     ;

   if (d >= end)
     end = '\0';

   return dst;
}

This version NULL terminates the string as well, which violates
standard strncpy but which you said would be cool.
-- 
====================================================================
      Jim Jagielski            |       jaguNET Access Services
     jim@jaguNET.com           |       http://www.jaguNET.com/
            "Look at me! I'm wearing a cardboard belt!"

Mime
View raw message