strncpy
strncpy
Posted Mar 8, 2007 6:53 UTC (Thu) by ncm (guest, #165)In reply to: Some idle comments about Linus' e-mail response and Linux by bronson
Parent article: Quote of the week
In its origin strncpy() had one purpose: filling in directory entries in Version 7 Unix file-system code. It was always called with a length argument of 14, which was the size of the filename field in the struct, and the limit on filename length. Directory entries were zero-filled, not null-terminated.
The real mistake was adopting it into the ANSI C standard library as if it were a regular string function. Of course it's equally a mistake to use it. Nowadays, snprintf() might be a better choice, but a function that calls abort() if its argument is longer than the buffer would be better still. The BSD strlcpy() is another mistake, a fact fortunately recognized by POSIX.
Of course the right thing is to eliminate null-terminated string operations, but that's harder to retrofit.