- getEntries :: Addr -> Int -> IO [FilePath]
- getEntries ptr n = do
- str <- _casm_ ``%r = ((char **)%0)[%1];'' ptr n
- if str == ``NULL''
- then return []
- else do
- len <- _ccall_ strlen str
- entry <- stToIO (unpackNBytesST str len)
- _ccall_ free str
- entries <- getEntries ptr (n+1)
- return (entry : entries)
+ loop :: Addr -> Addr -> IO [String]
+ loop dir dirent_ptr = do
+ dirent_ptr <- _ccall_ readDir__ dir
+ if dirent_ptr == ``NULL''
+ then do
+ return []
+ else do
+ str <- _casm_ `` %r=(char*)((struct dirent*)%0)->d_name; '' dirent_ptr
+ -- not using the unpackCString function here, since we have to force
+ -- the unmarshalling of the directory entry right here as subsequent
+ -- calls to readdir() may overwrite it.
+ len <- _ccall_ strlen str
+ entry <- stToIO (unpackNBytesST str len)
+ entries <- loop dir dirent_ptr
+ return (entry:entries)