aboutsummaryrefslogtreecommitdiffhomepage
path: root/System/Posix/Env.hsc
diff options
context:
space:
mode:
authorGravatar Paolo Capriotti <p.capriotti@gmail.com>2012-03-30 19:16:39 +0100
committerGravatar Paolo Capriotti <p.capriotti@gmail.com>2012-03-30 19:16:39 +0100
commit335fd69c61076db160fe7011814e26a577958147 (patch)
treeab5769103de57af3bfd4eff0c46589f6b4be84bc /System/Posix/Env.hsc
parente518038a0eca7dddbbd3d4326355db9c1d6f0068 (diff)
Add setEnvironment and cleanEnv to System.Posix.Env (#5648)
Diffstat (limited to 'System/Posix/Env.hsc')
-rw-r--r--System/Posix/Env.hsc28
1 files changed, 25 insertions, 3 deletions
diff --git a/System/Posix/Env.hsc b/System/Posix/Env.hsc
index 1a72a37..51e8891 100644
--- a/System/Posix/Env.hsc
+++ b/System/Posix/Env.hsc
@@ -21,9 +21,11 @@ module System.Posix.Env (
, getEnvDefault
, getEnvironmentPrim
, getEnvironment
+ , setEnvironment
, putEnv
, setEnv
, unsetEnv
+ , clearEnv
) where
#include "HsUnix.h"
@@ -34,7 +36,7 @@ import Foreign.C.String
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable
-import Control.Monad (liftM)
+import Control.Monad (liftM, forM_, void)
import Data.Maybe (fromMaybe)
#if __GLASGOW_HASKELL__ > 700
import System.Posix.Internals (withFilePath, peekFilePath)
@@ -73,8 +75,12 @@ foreign import ccall unsafe "getenv"
getEnvironmentPrim :: IO [String]
getEnvironmentPrim = do
c_environ <- getCEnviron
- arr <- peekArray0 nullPtr c_environ
- mapM peekFilePath arr
+ -- environ can be NULL
+ if c_environ == nullPtr
+ then return []
+ else do
+ arr <- peekArray0 nullPtr c_environ
+ mapM peekFilePath arr
getCEnviron :: IO (Ptr CString)
#if darwin_HOST_OS
@@ -102,6 +108,15 @@ getEnvironment = do
dropEq (x,'=':ys) = (x,ys)
dropEq (x,_) = error $ "getEnvironment: insane variable " ++ x
+-- |'setEnvironment' resets the entire environment to the given list of
+-- @(key,value)@ pairs.
+
+setEnvironment :: [(String,String)] -> IO ()
+setEnvironment env = do
+ clearEnv
+ forM_ env $ \(key,value) ->
+ setEnv key value True {-overwrite-}
+
-- |The 'unsetEnv' function deletes all instances of the variable name
-- from the environment.
@@ -152,3 +167,10 @@ setEnv key value False = do
Just _ -> return ()
Nothing -> putEnv (key++"="++value)
#endif
+
+-- |The 'clearEnv' function clears the environment of all name-value pairs.
+clearEnv :: IO ()
+clearEnv = void c_clearenv
+
+foreign import ccall unsafe "clearenv"
+ c_clearenv :: IO Int