Dot files always first in directory?
Gregory Kemnitz
kemnitz at mitisft.Convergent.COM
Fri May 5 06:19:25 AEST 1989
In article <11108 at bloom-beacon.MIT.EDU>, jik at athena.mit.edu (Jonathan I. Kamens) writes:
>
> It it safe to assume when writing a program which manipulates
> directories that . and .. will always be the first two entries in a
> directory?
>
> If I can't assume that, then I've got to compare every file in the
> directory to "." and "..", and this would probably slow the program
> down even more than it already is.
>
After checks on our file system, I found that this assumption is PROBABLY
correct. However, this type of thing is EXTREMELY DANGEROUS, unless it is
part of the definition of the directory structure (It might well be - if it
is ignore this message and I'll be eating flames). This type of assumption
usually leades to unportable code. (will your code run on UNIX forever??)
A portable way to implement this assumption (without error checking for
clarity):
struct dirent (direct??) *dir1, *dir2;
BOOLEAN dot_and_dotdot_read;
dir1 = readdir(dirp);
dir2 = readdir(dirp);
dot_and_dotdot_read = (!strcmp(dir1->file_name, ".")
&& !strcmp(dir2->file_name, ".."))
|| (!strcmp(dir1->file_name, "..")
&& !strcmp(dir2->file_name, ".");
if (dot_and_dotdot_read)
{
proceed using assumption...
}
else
{
proceed carefully. Remember to process dir1 and dir2
}
I may be wrong about the members of the structures, but you get the point.
Also, I omitted error checking. In any code of this type, you should always
check the return value.
Greg Kemnitz
More information about the Comp.unix.wizards
mailing list