strncpy

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Fri Dec 22 05:52:09 AEST 1989


In article <8313 at stiatl.UUCP> cns at stiatl.UUCP (Chris Straut) writes:
| In article <11509 at csli.Stanford.EDU> poser at 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 at 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



More information about the Comp.lang.c mailing list