It seemed easier to use the system call directly rather than use readdir. The call returns a table of the files in the directory, it calls getdents until it returns zero. There is a convenience function that opens a named file and returns the directory entries so you can write:
local d = assert(S.dirfile("/dev"))
assert(d.zero, "expect to find /dev/zero")
assert(d["."], "expect to find .")
assert(d[".."], "expect to find ..")
Feel free to rip out the (small) amount of relevent code if you only want this function...
I am intending to add OS X support at some point to this library, but it is going to be a fair amount of work. It is tested on 32 and 64 bit x86 and arm.