Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: strncpy Keywords: strncpy null termination Message-ID: <1947@crdos1.crd.ge.COM> Date: 21 Dec 89 18:52:09 GMT References: <11509@csli.Stanford.EDU> <8313@stiatl.UUCP> Organization: GE Corp R&D Center, Schenectady NY Lines: 26 Reply-exos:@crdgw1:To: davidsen@crdos1.crd.ge.com (bill davidsen) In article <8313@stiatl.UUCP> cns@stiatl.UUCP (Chris Straut) writes: | In article <11509@csli.Stanford.EDU> poser@csli.Stanford.EDU (Bill Poser) writes: | > Why is it that strncpy(3) does not null terminate | >the string if the length of the source string is greater than | >or equal to the length of the target buffer? | [ other stuff deleted ] | | [ answer deleted ] I was asked this question just a few days ago mydelf. After some thinking, this appears to be consistent with other behavior, namely fgets() which drops the trailing newline if the buffer is too small. This makes sense if you think of strncpy() as a way to copy to a buffer without overflow. For buffer size N you copy N-1 characters and manually force an end of string in the last byte. If you think of it as a way to copy N characters from a string to a buffer which is always big enough it doesn't make sense. It's a question of intended use. We hacked out a quick version which does always terminate the string rather than add logic to all the places in which it was being used. -- bill davidsen (davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen) "The world is filled with fools. They blindly follow their so-called 'reason' in the face of the church and common sense. Any fool can see that the world is flat!" - anon