From de8e0e31b41f9f60aad330d62d06b6c5c1596d9e Mon Sep 17 00:00:00 2001 From: sof Date: Wed, 22 Apr 1998 12:45:38 +0000 Subject: [PATCH] [project @ 1998-04-22 12:45:38 by sof] Simplified impl of getDirectoryContents --- ghc/lib/std/Directory.lhs | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/ghc/lib/std/Directory.lhs b/ghc/lib/std/Directory.lhs index 14be0e4..778ecad 100644 --- a/ghc/lib/std/Directory.lhs +++ b/ghc/lib/std/Directory.lhs @@ -17,7 +17,7 @@ some operating systems, it may also be possible to have paths which are relative to the current directory. \begin{code} -{-# OPTIONS -#include #-} +{-# OPTIONS -#include -#include #-} module Directory ( Permissions(Permissions), @@ -334,26 +334,29 @@ The path refers to an existing non-directory object. \end{itemize} \begin{code} +--getDirectoryContents :: FilePath -> IO [FilePath] getDirectoryContents path = do - ptr <- _ccall_ getDirectoryContents path - if ptr == ``NULL'' + dir <- _ccall_ openDir__ path + ptr <- _ccall_ malloc (``sizeof(struct dirent**)''::Int) + if dir == ``NULL'' then constructErrorAndFail "getDirectoryContents" - else do - entries <- getEntries ptr 0 - _ccall_ free ptr - return entries + else loop dir ptr where - 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) \end{code} If the operating system has a notion of current directories, -- 1.7.10.4