summaryrefslogtreecommitdiff
path: root/Utility/UserInfo.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Utility/UserInfo.hs')
-rw-r--r--Utility/UserInfo.hs32
1 files changed, 20 insertions, 12 deletions
diff --git a/Utility/UserInfo.hs b/Utility/UserInfo.hs
index 617c3e9..7e94caf 100644
--- a/Utility/UserInfo.hs
+++ b/Utility/UserInfo.hs
@@ -1,11 +1,12 @@
{- user info
-
- - Copyright 2012 Joey Hess <joey@kitenet.net>
+ - Copyright 2012 Joey Hess <id@joeyh.name>
-
- License: BSD-2-clause
-}
{-# LANGUAGE CPP #-}
+{-# OPTIONS_GHC -fno-warn-tabs #-}
module Utility.UserInfo (
myHomeDir,
@@ -13,11 +14,14 @@ module Utility.UserInfo (
myUserGecos,
) where
-import Control.Applicative
-import System.PosixCompat
-
import Utility.Env
+import System.PosixCompat
+#ifndef mingw32_HOST_OS
+import Control.Applicative
+#endif
+import Prelude
+
{- Current user's home directory.
-
- getpwent will fail on LDAP or NIS, so use HOME if set. -}
@@ -40,16 +44,20 @@ myUserName = myVal env userName
env = ["USERNAME", "USER", "LOGNAME"]
#endif
-myUserGecos :: IO String
-#ifdef __ANDROID__
-myUserGecos = return "" -- userGecos crashes on Android
+myUserGecos :: IO (Maybe String)
+-- userGecos crashes on Android and is not available on Windows.
+#if defined(__ANDROID__) || defined(mingw32_HOST_OS)
+myUserGecos = return Nothing
#else
-myUserGecos = myVal [] userGecos
+myUserGecos = Just <$> myVal [] userGecos
#endif
myVal :: [String] -> (UserEntry -> String) -> IO String
-myVal envvars extract = maybe (extract <$> getpwent) return =<< check envvars
+myVal envvars extract = go envvars
where
- check [] = return Nothing
- check (v:vs) = maybe (check vs) (return . Just) =<< getEnv v
- getpwent = getUserEntryForID =<< getEffectiveUserID
+#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