> Copies the first *num* characters of *source* to *destination*.
> If the end of the *source* C string (which is signaled by
> a null-character) is found before *num* characters have been
> copied, *destination* is padded with zeros until a total of *num*
> characters have been written to it.
>
> No null-character is implicitly appended at the end of
> *destination* if *source* is longer than *num* (thus, in this
> case, *destination* may not be a null terminated C string).
>
> I found two problems. From the first section: filling the string
> with unneeded nulls consumes unnecessary time. Why would that
> be an advantage?
Because when you are copying to a fixed width field that is to be
written to disk, failing to copy the nulls could leak sensitive
information onto disk.
> From the second section when the caller asks to copy more chars
> than there is space the string is not null terminated. I see
> that as a defect. It can lead to problems later on. How can
> that be an advantage?
Because the string only needs a null if it is shorter than the field.
I do not have any direct evidence, but I believe that strncpy was
designed for copying *into* fixed-width fields. It mostly works
fine in that environment. The only exception is copying from a
shorter fixed-width field into a longer one, where you need to add
the extra nulls with memset.
--
Lawrence Crowl