Pantek Library
Hosting Provided By
CybrHost
High Speed Hosting

Re: strncpy() and NULL termination

From: Dan Cross <tenser(at)spitfire.ecsel.psu.edu>
Date: Mon Feb 17 1997 - 12:47:45 EST


> >(strncpy(3) only returns NULL or a valid pointer, but there is

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 *
strncpy(dst, src, n)

        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?

  • Dan C.
Received on Mon Feb 17 09:47:57 1997

This archive was generated by hypermail 2.1.8 : Wed Aug 23 2006 - 12:41:02 EDT


Contact Us  Legal Notices  Order Services Online 
Pantek Home  Privacy Policy  IT news  Site Map  Pantek Library