2012-10-25 22:17:32 +00:00
|
|
|
{- user info
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-02-19 22:06:27 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
import Control.Applicative
|
2013-05-11 20:03:00 +00:00
|
|
|
import System.PosixCompat
|
|
|
|
|
|
|
|
import Utility.Env
|
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
|
2013-05-12 20:38:15 +00:00
|
|
|
myHomeDir = myVal env homeDirectory
|
|
|
|
where
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
env = ["HOME"]
|
|
|
|
#else
|
|
|
|
env = ["USERPROFILE", "HOME"] -- HOME is used in Cygwin
|
|
|
|
#endif
|
2012-10-25 22:17:32 +00:00
|
|
|
|
|
|
|
{- Current user's user name. -}
|
|
|
|
myUserName :: IO String
|
2013-05-12 20:38:15 +00:00
|
|
|
myUserName = myVal env userName
|
|
|
|
where
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
env = ["USER", "LOGNAME"]
|
|
|
|
#else
|
|
|
|
env = ["USERNAME", "USER", "LOGNAME"]
|
|
|
|
#endif
|
2012-10-25 22:17:32 +00:00
|
|
|
|
2013-01-06 17:34:08 +00:00
|
|
|
myUserGecos :: IO String
|
2013-02-27 06:39:22 +00:00
|
|
|
#ifdef __ANDROID__
|
2013-02-19 22:06:27 +00:00
|
|
|
myUserGecos = return "" -- userGecos crashes on Android
|
|
|
|
#else
|
2013-01-06 17:34:08 +00:00
|
|
|
myUserGecos = myVal [] userGecos
|
2013-02-19 22:06:27 +00:00
|
|
|
#endif
|
2013-01-06 17:34:08 +00:00
|
|
|
|
2012-10-25 22:17:32 +00:00
|
|
|
myVal :: [String] -> (UserEntry -> String) -> IO String
|
|
|
|
myVal envvars extract = maybe (extract <$> getpwent) return =<< check envvars
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
check [] = return Nothing
|
|
|
|
check (v:vs) = maybe (check vs) (return . Just) =<< getEnv v
|
|
|
|
getpwent = getUserEntryForID =<< getEffectiveUserID
|