2014-04-01 17:51:58 +00:00
|
|
|
{- user info
|
|
|
|
-
|
2015-01-06 23:07:40 +00:00
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
2014-04-01 17:51:58 +00:00
|
|
|
-
|
2014-05-10 14:05:28 +00:00
|
|
|
- License: BSD-2-clause
|
2014-04-01 17:51:58 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
|
|
|
module Utility.UserInfo (
|
|
|
|
myHomeDir,
|
|
|
|
myUserName,
|
|
|
|
myUserGecos,
|
|
|
|
) where
|
|
|
|
|
|
|
|
import System.PosixCompat
|
2015-01-06 23:07:40 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
import Control.Applicative
|
|
|
|
#endif
|
2014-04-01 17:51:58 +00:00
|
|
|
|
|
|
|
import Utility.Env
|
|
|
|
|
|
|
|
{- Current user's home directory.
|
|
|
|
-
|
|
|
|
- getpwent will fail on LDAP or NIS, so use HOME if set. -}
|
|
|
|
myHomeDir :: IO FilePath
|
|
|
|
myHomeDir = myVal env homeDirectory
|
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
env = ["HOME"]
|
|
|
|
#else
|
|
|
|
env = ["USERPROFILE", "HOME"] -- HOME is used in Cygwin
|
|
|
|
#endif
|
|
|
|
|
|
|
|
{- Current user's user name. -}
|
|
|
|
myUserName :: IO String
|
|
|
|
myUserName = myVal env userName
|
|
|
|
where
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
env = ["USER", "LOGNAME"]
|
|
|
|
#else
|
|
|
|
env = ["USERNAME", "USER", "LOGNAME"]
|
|
|
|
#endif
|
|
|
|
|
2015-01-06 23:07:40 +00:00
|
|
|
myUserGecos :: IO (Maybe String)
|
|
|
|
-- userGecos crashes on Android and is not available on Windows.
|
|
|
|
#if defined(__ANDROID__) || defined(mingw32_HOST_OS)
|
|
|
|
myUserGecos = return Nothing
|
2014-04-01 17:51:58 +00:00
|
|
|
#else
|
2015-01-06 23:07:40 +00:00
|
|
|
myUserGecos = Just <$> myVal [] userGecos
|
2014-04-01 17:51:58 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
myVal :: [String] -> (UserEntry -> String) -> IO String
|
2015-01-06 23:07:40 +00:00
|
|
|
myVal envvars extract = go envvars
|
2014-04-01 17:51:58 +00:00
|
|
|
where
|
2015-01-06 23:07:40 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
go [] = extract <$> (getUserEntryForID =<< getEffectiveUserID)
|
|
|
|
#else
|
|
|
|
go [] = error $ "environment not set: " ++ show envvars
|
|
|
|
#endif
|
|
|
|
go (v:vs) = maybe (go vs) return =<< getEnv v
|