Can any assumption be made on the order of entries in a directory stream obtained with the opendir() function? For example, are .
and ..
always the first two entries? What would cause the order to change?
Asked
Active
Viewed 250 times
1

cschol
- 12,799
- 11
- 66
- 80
-
possible duplicate of [Does readdir() guarantee an order?](http://stackoverflow.com/questions/8977441/does-readdir-guarantee-an-order) – Jim Garrison Feb 17 '12 at 22:16
-
This question is about opendir() not readdir(). – cschol Feb 18 '12 at 15:01
1 Answers
6
Posix says that you cannot rely on .
and ..
being the first two entries.
From The Open Group Base Specifications Issue 6:
The directory entries for dot and dot-dot are optional. This volume of IEEE Std 1003.1-2001 does not provide a way to test a priori for their existence because an application that is portable must be written to look for (and usually ignore) those entries. Writing code that presumes that they are the first two entries does not always work, as many implementations permit them to be other than the first two entries, with a "normal" entry preceding them.

markgz
- 6,054
- 1
- 19
- 41