[comp.unix.wizards] Are . and .. first?

rk@unify.UUCP (Ron Kuris) (05/06/89)

In article <3540@udccvax1.acs.udel.EDU> conan@vax1.acs.udel.edu (CONAN THE BARBARIAN) writes:
>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?
>>
>
>NO, its not save to assume that.
>try "touch #abcd" then 'list' the directory.
>

I did this, and then said "od -c <dirname>" and lo and behold -- . and ..
were first!  Remember that ls sorts files before outputting them.

Ron Kuris		(916) 920-9092
rk@unify.UUCP
{{ucdavis,csun,lll-crg}!csusac,pyramid,sequent}!unify!rk
-- 
Ron Kuris		(916) 920-9092
rk@unify.UUCP
{{ucdavis,csun,lll-crg}!csusac,pyramid,sequent}!unify!rk

donn@hpfcdc.HP.COM (Donn Terry) (05/12/89)

It is not safe to assume that . and .. are first (or even that they 
do or do not exist as directory entries).  In developing POSIX, we
found just about all possible variations.

Try this one (as super-user on System V systems; you can do it in C as
super-user on just about any "classical" system.)

cd /somewhere/child
/etc/unlink ..
touch foo
/etc/link /somewhere ..

This will make "foo" the second entry, and put ".." somewhere else.
On other implementations this sequence might be an error, do nothing
but create foo, or (potentially) crash the system.

The only portable way to deal with directories is to use the directory
library (opendir(), etc.), and then to look for and ignore . and ..
if you find them.

Donn Terry
HP