Re: Minor bug in lib/string.c

Wayne Schlitt (wayne@midwestcs.com)
31 May 1999 17:15:27 -0500


In <19990530181356.L1415@mencheca.ch.genedata.com> Matthew Wilcox <Matthew.Wilcox@genedata.com> writes:

>
> Strncpy Considered Harmful. http://pocket.fluff.org/~mrw/strncpy.html

I tend to agree that using strncpy() for anything other than what it
was originally designed for is a mistake.

What was strncpy() designed for? Answer: the Unix V7 filesystem
directory structures. In V7, each "directory" was a specially marked
file that contained 16 byte records. 14 bytes were for the filename,
2 bytes for the inode. You always wanted to wipe out the old data, so
padding the string with NUL's was A Good Thing. Requiring a trailing
NUL was a waste that cost you a precious character in your filename.

Why was such a specialized function put into libc? Well, in many
versions of Unix, you can open a directory just like any other file
and read it yourself. This was before opendir() and readdir() were
invented. So, any program that had to deal with directories needed
this specialized function.

Why was the name of this specialized function lumped in with all the
general str*() functions? Beats the crap out of me. Probably for
the same reason creat() is missing one (and only one) vowel.

-wayne

-- 
Wayne Schlitt can not assert the truth of all statements in this
article and still be consistent.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/