blob: 30a08f1998767f61c4d64d94d3ba6d5dd1e1bc3c (
plain)
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
|
module System.Home where
import System.IO.Error
import System.Posix.User
import System.FilePath
-- Return home directory for given user, or currently logged in user.
getUserHome :: Maybe String -> IO (Either IOError String)
getUserHome Nothing = (Just <$> getEffectiveUserName) >>= getUserHome
getUserHome (Just name) = tryIOError $ homeDirectory <$> getUserEntryForName name
-- If the first component of path is either a tilde `~', or a tilde
-- joined with a user name `~hugo', expand that to the revelant home
-- directory, and return the modified path.
expandTilde :: FilePath -> IO FilePath
expandTilde path = do
let (fst' : rest) = splitDirectories path
mfst <- case fst' of
"~" -> getUserHome Nothing
('~':name) -> getUserHome (Just name)
_ -> return . Left . userError $ "Nothing"
return . joinPath $ case mfst of
Right fst -> fst : rest
Left _ -> rest
|