Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!mit-eddie!ll-xn!adelie!minya!jc From: jc@minya.UUCP (John Chambers) Newsgroups: comp.unix.wizards Subject: Re: /bin/test and stat(2) Message-ID: <283@minya.UUCP> Date: Tue, 13-Oct-87 23:47:14 EDT Article-I.D.: minya.283 Posted: Tue Oct 13 23:47:14 1987 Date-Received: Thu, 15-Oct-87 22:22:03 EDT References: <9723@brl-adm.ARPA> Organization: home Lines: 75 Summary: Is "" a string? Is 0 a number? In article <9723@brl-adm.ARPA>, franco@MIKEY.BBN.COM (Frank A. Lonigro) writes: > In article <15069@topaz.rutgers.edu> ron@topaz.rutgers.edu (Ron Natalie) writes: > >STAT should never be called with NULL, but it should always work > >when called with a zero length string (e.g. ""). > > Boyd Roberts responds: > >As for "", I couldn't disagree more. The zero length string only > >works due to a quirk in namei(). Do you ever see the ``zero length string'' > >named file in a directory? No, you don't. One of my favorite (only partly-)joking ways of explaining to non-computer people why it is that computers are so screwey is to explain that, while the Arabs taught Europeans that 0 was a useful number well over 1000 years ago, the fact is still not accepted by most of the computing field! The funniest examples appear when you look at the way strings of zero length are handled by most software. These days, you'd think it was funny if an accountant thought that 0 was an invalid balance on an account. But most programmers think that 0 is an invalid length for a string of characters, and advocate software that rejects such nonsense (while writing software that blows up on null strings). This discussion shows strong signs of being full of yet more good examples of such pre-0 thinking. In fact, null strings are (and should be) perfectly valid strings, and usually have a clear meaning. For instance, if string s1 is a name of some directory, and s2 is a name of something in that directory, then you should be able to get the name for that something by catenating s1, "/", and s2. Consider the case where s1="foo/bar", and s2="". The result of this operation is "foo/bar/". This should be a valid name of something that is (or at least could be) in "foo/bar". What is that thing? Well, "foo/bar/" could be a valid name of a file within foo/bar, i.e., an entry with a nonzero inode and all the name chars filled with nulls. If this is for some reason considered unacceptable, there is only one other reasonable interpretation, namely that a final '/' on a file name is redundant (just like the second '/' in "foo//bar" is redundant), and the name "foo/bar/" is a synonym for "foo/bar". But if this is true, then it is by transitivity also a synonym for "foo/bar/.", "foo/bar/./." and so on. From this we conclude that "" is synonymous with ".", "./.", and so on. Similarly, we could observe that by catenating "ls " and s1, we expect the resulting command "ls foo/bar" to give a listing of foo/bar. If we apply the same reasoning, we expect that cat("ls " s2) = "ls " will give us a listing of the directory that s2 names. But "ls " lists the current directory. So "" again appears to be a name for the current directory. In general, I think that the inventors of Unix are to be commended for not giving in to the usual mathematical illiteracy of the rest of the computer field, and giving us a system that usually treats zero in a sensible manner. I've done time working on IBM mainframes, where for instance you can't have an empty record in a file or a file with no records. This means special code in your programs to check for null output records and pad them with a space. It means special code to make sure that you write at least one record (with one space) to an output file, which implies keeping track of how much you've written. It means that, on input, a program has to be prepared for records that have been kludged up with extra white space. It means a whole lot of hassle in general that could have been avoided if they had just realized that zero is a valid length of a string, and written their routines to handle the zero case correctly. For that matter, try writing an empty output file on VMS sometime. > have all been coded to rely on this default(this can be verified > by do something like 'ls ""' or 'chmod ""', etc). In my opinion, is > very bad programming practice. And to think that such programs > have been coded this way since the beginning of UNIX is hideous. No, it's just a sign of mathematical sophistication. (:-) -- John Chambers <{adelie,ima,maynard,mit-eddie}!minya!{jc,root}> (617/484-6393)