2012-10-25 22:17:32 +00:00
|
|
|
{- user info
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-10-25 22:17:32 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-10-25 22:17:32 +00:00
|
|
|
-}
|
|
|
|
|
2013-02-19 22:06:27 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-05-10 20:31:50 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2013-02-19 22:06:27 +00:00
|
|
|
|
2012-10-25 22:17:32 +00:00
|
|
|
module Utility.UserInfo (
|
|
|
|
myHomeDir,
|
2013-01-06 17:34:08 +00:00
|
|
|
myUserName,
|
|
|
|
myUserGecos,
|
2012-10-25 22:17:32 +00:00
|
|
|
) where
|
|
|
|
|
2017-12-31 20:08:31 +00:00
|
|
|
import Utility.Env.Basic
|
2016-11-16 01:29:54 +00:00
|
|
|
import Utility.Exception
|
2017-11-14 18:14:10 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
import Utility.Data
|
|
|
|
import Control.Applicative
|
2023-08-01 19:17:43 +00:00
|
|
|
import System.Posix.User
|
2023-08-01 21:47:30 +00:00
|
|
|
#if MIN_VERSION_unix(2,8,0)
|
|
|
|
import System.Posix.User.ByteString (UserEntry)
|
|
|
|
#endif
|
2017-11-14 18:14:10 +00:00
|
|
|
#endif
|
2015-05-10 20:19:56 +00:00
|
|
|
|
|
|
|
import Prelude
|
2012-10-25 22:17:32 +00:00
|
|
|
|
|
|
|
{- Current user's home directory.
|
|
|
|
-
|
|
|
|
- getpwent will fail on LDAP or NIS, so use HOME if set. -}
|
|
|
|
myHomeDir :: IO FilePath
|
2023-08-01 21:38:59 +00:00
|
|
|
myHomeDir = either giveup return =<<
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2023-08-01 21:38:59 +00:00
|
|
|
myVal ["HOME"] homeDirectory
|
2013-05-12 20:38:15 +00:00
|
|
|
#else
|
2023-08-01 21:38:59 +00:00
|
|
|
myVal ["USERPROFILE", "HOME"] -- HOME is used in Cygwin
|
2013-05-12 20:38:15 +00:00
|
|
|
#endif
|
2012-10-25 22:17:32 +00:00
|
|
|
|
|
|
|
{- Current user's user name. -}
|
2016-06-08 19:04:15 +00:00
|
|
|
myUserName :: IO (Either String String)
|
2023-08-01 21:38:59 +00:00
|
|
|
myUserName =
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2023-08-01 21:38:59 +00:00
|
|
|
myVal ["USER", "LOGNAME"] userName
|
2013-05-12 20:38:15 +00:00
|
|
|
#else
|
2023-08-01 21:38:59 +00:00
|
|
|
myVal ["USERNAME", "USER", "LOGNAME"]
|
2013-05-12 20:38:15 +00:00
|
|
|
#endif
|
2012-10-25 22:17:32 +00:00
|
|
|
|
2014-10-31 20:14:12 +00:00
|
|
|
myUserGecos :: IO (Maybe String)
|
2018-10-13 05:36:06 +00:00
|
|
|
-- userGecos is not available on Windows.
|
|
|
|
#if defined(mingw32_HOST_OS)
|
2014-10-31 20:14:12 +00:00
|
|
|
myUserGecos = return Nothing
|
2013-02-19 22:06:27 +00:00
|
|
|
#else
|
2016-06-08 19:04:15 +00:00
|
|
|
myUserGecos = eitherToMaybe <$> myVal [] userGecos
|
2013-02-19 22:06:27 +00:00
|
|
|
#endif
|
2013-01-06 17:34:08 +00:00
|
|
|
|
2023-08-01 21:38:59 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2016-06-08 19:04:15 +00:00
|
|
|
myVal :: [String] -> (UserEntry -> String) -> IO (Either String String)
|
2014-10-31 19:46:44 +00:00
|
|
|
myVal envvars extract = go envvars
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2018-04-25 00:10:10 +00:00
|
|
|
go [] = either (const $ envnotset) (Right . extract) <$> get
|
|
|
|
go (v:vs) = maybe (go vs) (return . Right) =<< getEnv v
|
|
|
|
-- This may throw an exception if the system doesn't have a
|
|
|
|
-- passwd file etc; don't let it crash.
|
|
|
|
get = tryNonAsync $ getUserEntryForID =<< getEffectiveUserID
|
2014-10-31 19:46:44 +00:00
|
|
|
#else
|
2023-08-01 21:38:59 +00:00
|
|
|
myVal :: [String] -> IO (Either String String)
|
|
|
|
myVal envvars = go envvars
|
|
|
|
where
|
2023-08-02 14:43:20 +00:00
|
|
|
go [] = return envnotset
|
2023-08-01 21:38:59 +00:00
|
|
|
go (v:vs) = maybe (go vs) (return . Right) =<< getEnv v
|
2014-10-31 19:46:44 +00:00
|
|
|
#endif
|
2018-04-25 00:10:10 +00:00
|
|
|
envnotset = Left ("environment not set: " ++ show envvars)
|