2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[DirUtils]{Directory functions}
7 {-# OPTIONS -#include <dirent.h> #-}
13 #if !defined(__GLASGOW_HASKELL__) || __GLASGOW_HASKELL__ >= 302
17 #if __GLASGOW_HASKELL__ >= 300
18 import PrelPack ( unpackNBytesST )
20 import PackBase ( unpackNBytesST )
22 import PrimPacked ( strLength )
23 import GlaExts ( stToIO )
28 The implementation of Directory.getDirectoryContents that ships
29 with ghc-X ( X<=301) is wrong (the C stub fails to allocate
30 space for the terminating NUL for each directory entry name.)
32 To counter for this, we supply a working version here, which will
33 be nuked once we can assume that ghc-3.02 or later is used to
34 compile the compiler sources.
37 getDirectoryContents :: String -> IO [String]
38 getDirectoryContents path = do
39 dir <- _ccall_ opendir path
41 then fail (userError ("DirUtils.getDirectoryContents: couldn't open "++ path))
44 loop :: Addr -> IO [String]
46 dirent_ptr <- _ccall_ readdir dir
47 if (dirent_ptr::Addr) == ``NULL''
52 str <- _casm_ `` %r=(char*)((struct dirent*)%0)->d_name; '' dirent_ptr
53 entry <- stToIO (unpackNBytesST str (strLength str))
55 return (entry:entries)