Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!bloom-beacon!apple!oliveb!pyramid!ctnews!mitisft!kemnitz From: kemnitz@mitisft.Convergent.COM (Gregory Kemnitz) Newsgroups: comp.unix.wizards Subject: Re: Dot files always first in directory? Summary: Unportable code alert!! Message-ID: <672@mitisft.Convergent.COM> Date: 4 May 89 20:19:25 GMT References: <11108@bloom-beacon.MIT.EDU> Organization: Convergent Technologies, San Jose, CA Lines: 46 In article <11108@bloom-beacon.MIT.EDU>, jik@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