http://www.perlmonks.org?node_id=112867

grinder has asked for the wisdom of the Perl Monks concerning the following question:

A few days ago, someone posted a question about reading directories. To discard the current and parent directories that are returned by readdir, someone suggested to:

opendir(DIR, "/users/foo/"); @bar = readdir(DIR); closedir(DIR); for (1..2) {shift @bar;} #get rid of '.' and '..'

Notwithstanding the lack of error checking, and a dubious array hack better handled by splice, merlyn subsequently pointed out that "There is no promise that the first two elements returned from a readdir are dot and dotdot," and I nodded in agreement.

Then I reflected on the fact that I have been using Perl for nearly ten years; I have use Perl on a half a dozen Unix variants, on VMS, on MS-DOS and Win32. In all that time, grovelling through filesystems has always been a large component of my work and yet I cannot recall a single time when . and .. were returned in any but the first two positions. Maybe it happens once in a while for any script in production -- I'm talking about when I'm stepping through some development code I'm testing.

I heed the warning and know that this behaviour is not guaranteed, so I dutifully code:

while( defined( my $file = readdir(DIR) )) { next if $file eq '.' or $file eq '..'; munge($file); }

but maybe it would be more elegant to be able to write code that throws away the first two results returned by readdir and then have a while block that doesn't contain first next if ... check. What I mean is that it's a nearly-invariant test that could be hoisted out of the loop if readdir was a little more deterministic.

My questions are

  1. Does there exist a platform where readdir returns . and .. in other than the first two return values?
  2. Is readdir guaranteed to return . and .. as the first two return values on any platform (specifically *BSD, Linux, Solaris and Win32)? (or more specifically the filesystem in use, such as ext2, ffs, ntfs...)

I realise my assumption is based on the notion that I consider a directory stream to be a linear list. Walking down it is akin to accessing array elements. A hash-based directory stream would produce unordered results, but I'm not sure I've ever encountered one, at least as far as the visible behaviour from userspace is concerned.

--
g r i n d e r