Dot files always first in directory?

John Buck john at polyof.UUCP
Sat May 6 02:33:46 AEST 1989


In article <11493 at ulysses.homer.nj.att.com>, ekrell at hector.UUCP (Eduardo Krell) writes:
> In article <2778 at buengc.BU.EDU> bph at buengc.bu.edu (Blair P. Houghton) writes:
> >To beat this horse quite dead, any leading character that would sort before
> >the period will place the filename before the . and .. in a directory
> >listing.
> That's because "ls" sorts the directory entries by default. The original
> question was about readdir() which doesn't. Use "od" or write a small
> program using opendir() and readdir() and you'll see that "." and ".."
> are the first and second entries.

The '-f' option of 'ls' lists directories (and other files, for that matter)
in "directory order"; it performs NO sorting.  Usually, '.' and '..' will
appear first.  This is not necessarily true; some wise-guy can easily
change the order of things in directories using fsdb, or some other
tool(s).

The bottom line is, since you are readdir()ing anyway, how hard is it to
add the test after each readdir?  C'mon, it is not going to slow things
down.  An extra compare (or two) is worth the grief you may suffer later.
After all, how many entries are in the directory?  1000? 2000? how long
does 1 or 2K compares take these days?

	/* Notice this first compare will fail most of the time, and the
	 * rest of this 'if' won't even get executed
	 */
	if(dp->d_name[0] == '.' &&
		(dp->d_name[1] == '\0' ||
			(d->d_name[2] == '\0' && dp->d_name[1] == '.'))){
			continue;
	}

John Buck
john at polyof.poly.edu



More information about the Comp.unix.wizards mailing list