2012-10-25 18:17:32 -04:00
|
|
|
{- user info
|
|
|
|
-
|
2015-01-21 12:50:09 -04:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-10-25 18:17:32 -04:00
|
|
|
-
|
2014-05-10 11:01:27 -03:00
|
|
|
- License: BSD-2-clause
|
2012-10-25 18:17:32 -04:00
|
|
|
-}
|
|
|
|
|
2013-02-19 18:06:27 -04:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-05-10 16:31:50 -04:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2013-02-19 18:06:27 -04:00
|
|
|
|
2012-10-25 18:17:32 -04:00
|
|
|
module Utility.UserInfo (
|
|
|
|
myHomeDir,
|
2013-01-06 13:34:08 -04:00
|
|
|
myUserName,
|
|
|
|
myUserGecos,
|
2012-10-25 18:17:32 -04:00
|
|
|
) where
|
|
|
|
|
2015-05-10 16:19:56 -04:00
|
|
|
import Utility.Env
|
2016-06-08 15:04:15 -04:00
|
|
|
import Utility.Data
|
2016-11-15 21:29:54 -04:00
|
|
|
import Utility.Exception
|
2015-05-10 16:19:56 -04:00
|
|
|
|
2013-05-11 15:03:00 -05:00
|
|
|
import System.PosixCompat
|
2014-12-29 17:25:59 -04:00
|
|
|
import Control.Applicative
|
2015-05-10 16:19:56 -04:00
|
|
|
import Prelude
|
2012-10-25 18:17:32 -04:00
|
|
|
|
|
|
|
{- Current user's home directory.
|
|
|
|
-
|
|
|
|
- getpwent will fail on LDAP or NIS, so use HOME if set. -}
|
|
|
|
myHomeDir :: IO FilePath
|
2016-11-15 21:29:54 -04:00
|
|
|
myHomeDir = either giveup return =<< myVal env homeDirectory
|
2013-05-12 15:38:15 -05:00
|
|
|
where
|
2013-08-02 12:27:32 -04:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-12 15:38:15 -05:00
|
|
|
env = ["HOME"]
|
|
|
|
#else
|
|
|
|
env = ["USERPROFILE", "HOME"] -- HOME is used in Cygwin
|
|
|
|
#endif
|
2012-10-25 18:17:32 -04:00
|
|
|
|
|
|
|
{- Current user's user name. -}
|
2016-06-08 15:04:15 -04:00
|
|
|
myUserName :: IO (Either String String)
|
2013-05-12 15:38:15 -05:00
|
|
|
myUserName = myVal env userName
|
|
|
|
where
|
2013-08-02 12:27:32 -04:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-05-12 15:38:15 -05:00
|
|
|
env = ["USER", "LOGNAME"]
|
|
|
|
#else
|
|
|
|
env = ["USERNAME", "USER", "LOGNAME"]
|
|
|
|
#endif
|
2012-10-25 18:17:32 -04:00
|
|
|
|
2014-10-31 16:14:12 -04: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
|
2013-02-19 18:06:27 -04:00
|
|
|
#else
|
2016-06-08 15:04:15 -04:00
|
|
|
myUserGecos = eitherToMaybe <$> myVal [] userGecos
|
2013-02-19 18:06:27 -04:00
|
|
|
#endif
|
2013-01-06 13:34:08 -04:00
|
|
|
|
2016-06-08 15:04:15 -04:00
|
|
|
myVal :: [String] -> (UserEntry -> String) -> IO (Either String String)
|
2014-10-31 15:46:44 -04:00
|
|
|
myVal envvars extract = go envvars
|
2012-12-13 00:24:19 -04:00
|
|
|
where
|
2014-10-31 15:46:44 -04:00
|
|
|
#ifndef mingw32_HOST_OS
|
2016-06-08 15:04:15 -04:00
|
|
|
go [] = Right . extract <$> (getUserEntryForID =<< getEffectiveUserID)
|
2014-10-31 15:46:44 -04:00
|
|
|
#else
|
2016-06-08 15:04:15 -04:00
|
|
|
go [] = return $ Left ("environment not set: " ++ show envvars)
|
2014-10-31 15:46:44 -04:00
|
|
|
#endif
|
2016-06-08 15:04:15 -04:00
|
|
|
go (v:vs) = maybe (go vs) (return . Right) =<< getEnv v
|