Skip to content

Commit

Permalink
Add getDirectoryContents' which ignores "." and ".."
Browse files Browse the repository at this point in the history
  • Loading branch information
guibou committed Sep 25, 2015
1 parent ce5ae1f commit 7691e8a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions System/Directory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module System.Directory
, removeDirectoryRecursive
, renameDirectory
, getDirectoryContents
, getDirectoryContents'
-- ** Current working directory
, getCurrentDirectory
, setCurrentDirectory
Expand Down Expand Up @@ -1030,6 +1031,15 @@ getDirectoryContents path =
-- no need to reverse, ordering is undefined
#endif /* mingw32 */

{- | A version of 'getDirectoryContents' which ignores @.@ and @..@
current and parent directories.
-}
getDirectoryContents' :: FilePath -> IO [FilePath]
getDirectoryContents' path =
(filter f) <$> (getDirectoryContents path)
where f filename = filename /= "." && filename /= ".."

#endif /* __GLASGOW_HASKELL__ */


Expand Down
2 changes: 2 additions & 0 deletions tests/GetDirContents001.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ main :: TestEnv -> IO ()
main _t = do
createDirectory dir
T(expectEq) () specials . sort =<< getDirectoryContents dir
T(expectEq) () [] . sort =<< getDirectoryContents' dir
names <- for [1 .. 100 :: Int] $ \ i -> do
let name = 'f' : show i
writeFile (dir </> name) ""
return name
T(expectEq) () (sort (specials <> names)) . sort =<< getDirectoryContents dir
T(expectEq) () (sort (names)) . sort =<< getDirectoryContents' dir
where dir = "dir"
specials = [".", ".."]

0 comments on commit 7691e8a

Please sign in to comment.