Use USER and HOME environment when set, and only fall back to getpwent, which doesn't work with LDAP or NIS.

This commit is contained in:
Joey Hess 2012-10-25 18:17:32 -04:00
parent 2018de53a3
commit 7ee0ffaeb9
13 changed files with 49 additions and 20 deletions

32
Utility/UserInfo.hs Normal file
View file

@ -0,0 +1,32 @@
{- user info
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.UserInfo (
myHomeDir,
myUserName
) where
import Control.Applicative
import System.Posix.User
import System.Posix.Env
{- Current user's home directory.
-
- getpwent will fail on LDAP or NIS, so use HOME if set. -}
myHomeDir :: IO FilePath
myHomeDir = myVal ["HOME"] homeDirectory
{- Current user's user name. -}
myUserName :: IO String
myUserName = myVal ["USER", "LOGNAME"] userName
myVal :: [String] -> (UserEntry -> String) -> IO String
myVal envvars extract = maybe (extract <$> getpwent) return =<< check envvars
where
check [] = return Nothing
check (v:vs) = maybe (check vs) (return . Just) =<< getEnv v
getpwent = getUserEntryForID =<< getEffectiveUserID