Dot files always first in directory?

Brandon S. Allbery allbery at ncoast.ORG
Mon May 15 05:43:02 AEST 1989


As quoted from <28440 at cci632.UUCP> by tvf at cci632.UUCP (Tom Frauenhofer):
+---------------
| In article <19473 at adm.BRL.MIL> mchinni at pica.army.mil (Michael J. Chinni, SMCAR-CCS-E) writes:
| >> From: "Jonathan I. Kamens" <jik at athena.mit.edu>
| >> It it safe to assume when writing a program which manipulates
| >> directories that . and .. will always be the first two entries in a
| >> directory?
| 
| BTW, I wouldn't depend on "." and ".." being the first two listings in
| a directory.  It may be a convention, but I fail to see how hard it would be
| to just write a little extra code to skip these entries when you find them.
+---------------

That's the way I do it; I also check for d_name[0] == '.' first to speed up
the check, as there are almost always more files not beginning with '.' than
files that do in any non-trivial case.  (That is, the speed doesn't matter
in someone's home directory that contains six dot files and no non-dot files,
as happens on a certain system I use when a new user is created.)

Also:  once, I managed to confuse a System III directory when attempting to
relink the kernel.  (I passed the wrong path to make, and it proceeded to
attempt to create the new kernel as "..", which "ld" promptly DELETED when
it couldn't write on it.)  After discovering this, I used /etc/link to
replace the ".." entry... by which time I had created a file for some
reason, so ".." ended up somewhere in the middle of the directory.  Fsck
didn't care about its new location; neither did ls or anything else.

One final comment:  if you KNOW that the file system is Unix, you can be
reasonably certain that the first two entries are "." and "..".  (Excepting,
of course, situations like the above.)  In the case of the HP9000 Unix that
was mentioned previously, I've heard that the underlying filesystem is NOT a
Unix filesystem; the kernel maps it onto an HP file system, and "." and ".."
are magic cookies handled by namei.  This is likely to be common whenever
Unix is running on top of something else, e.g. (early?) Primix, DG's version
of Unix, Eunice, etc., if the underlying OS does not itself use something
similar.

++Brandon
-- 
Brandon S. Allbery, moderator of comp.sources.misc	     allbery at ncoast.org
uunet!hal.cwru.edu!ncoast!allbery		    ncoast!allbery at hal.cwru.edu
      Send comp.sources.misc submissions to comp-sources-misc@<backbone>
NCoast Public Access UN*X - (216) 781-6201, 300/1200/2400 baud, login: makeuser



More information about the Comp.unix.wizards mailing list