|
|||||||||||
|
Re: strncpy() and NULL termination
From: Dan Cross <tenser(at)spitfire.ecsel.psu.edu>
Date: Mon Feb 17 1997 - 12:47:45 EST
Okay, just to set the record straight; strncpy(3) does nul padding from i to len - 1 where i is the current position in the dst array in the event that len < strlen(src), so checking to see if dst[len - 1] == '\0' is absolutely fine, without pre-initialization. This is in the man page, and can easily be verfied by looking at the source:
char *
char *dst;
const char *src;
register size_t n;
{
if (n != 0) {
register char *d = dst;
register const char *s = src;
do {
if ((*d++ = *s++) == 0) {
/* NUL pad the remaining n-1 bytes */
while (--n != 0)
*d++ = 0;
break;
}
} while (--n != 0);
}
return (dst);
} Note the while loop inside the do {} while(). Thanks for being on your toes, though. :-) That's what this is all about, right?
This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 12:41:02 EDT |
||||||||||
|
|||||||||||