Keith said:
Well, I'm one of the many people who was unwilling to contradict Mr.
Heathfield, and there's a very simple reason for that: I didn't think he
was wrong. Jacob might want to consider the possibility that many of the
other silent ones also agreed with Mr. Heathfield, even if Jacob finds
that very difficult to imagine.
Neither of the first two arguments to strncpy() needs to be a pointer
to a string.
There's also the fact that it usually either does not copy the entire
string, not even bothering to properly null-terminate it, or writes a
lot of stuff in addition. Only rarely, in normal usage, does it write an
exact copy of the string, and nothing else.
There are other plausible ways of defining what is meant by a "string
copy function", but requiring that it always copy the entire string,
including the terminating null character, and write nothing more than
the string, seems like a reasonable requirement to me.
The other issue debated about strncpy() is whether it was actually any
use. Well, in my programs I routinely face the following situation:
The output file spec allows a fixed amount of space for an array of
characters. The input data that is to be written into that space will
usually be small enough to fit, but cannot be guaranteed to fit. When it
does not fit, it's not an error condition - I'm supposed to put as many
characters from the beginning of the input source into the output array
as possible. Losing the terminating characters is regrettable but
permissible. The character array can be null-terminated, but is not
required to be. However, to make binary comparison of output files
easier, every character after the terminating null should also be null.
The above description applies to many of the "strings" that my programs
have to write, and also to many of the strings that they must read. It
seems to me that strncpy() is tailor-made for such use.