| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 | 
module System.Path
  ( dirsInPath, fileInPath, getFileInPath )
 where
import System.Directory   ( doesFileExist, getAbsolutePath )
import System.FilePath    ( (</>), searchPathSeparator )
import System.Environment ( getEnv )
import Data.List          ( split )
dirsInPath :: IO [String]
dirsInPath = do
  path <- getEnv "PATH"
  return $ split (== searchPathSeparator) path
fileInPath :: String -> IO Bool
fileInPath file = do
  dirs <- dirsInPath
  or  <$> mapM (doesFileExist . (</> file)) dirs
getFileInPath :: String -> IO (Maybe String)
getFileInPath file = dirsInPath >>= checkPath
 where
  checkPath [] = return Nothing
  checkPath (dir:dirs) = do
    let dirfile = dir </> file
    direx <- doesFileExist dirfile
    if direx then getAbsolutePath dirfile >>= return . Just
             else checkPath dirs
 |