[Haskell-cafe] os.path.expanduser analogue

Brandon Allbery allbery.b at gmail.com
Mon Nov 21 03:02:30 CET 2011


On Sun, Nov 20, 2011 at 20:36, Ben Gamari <bgamari.foss at gmail.com> wrote:

> expandUser :: FilePath -> IO FilePath
> expandUser p = if "~/" `isPrefixOf` p
>                  then do u <- getLoginName
>                          return $ u ++ drop 2 p
>                  else return p
>

expandUser "~"         = fmap homeDirectory getLoginName
expandUser ('~':'/':p) = getLoginName >>=
                         fmap ((++ p) . homeDirectory)
                              getUserEntryForName
expandUser ('~':up)    = let (u,p) = break (== '/') up
                          in fmap ((++ (drop 1 p)) . homeDirectory
                                  (getUserEntryForName u)
expandUser p           = p

Although arguably there should be some error checking.

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111120/733f50e6/attachment.htm>


More information about the Haskell-Cafe mailing list