commit ea1904b8fcef94da3341419e7901c569f91209b5
Author: Paolo Capriotti <p.capriotti@gmail.com>
Date: Fri Mar 9 13:54:13 2012 +0000
Add setEnvironment and clearEnv to System.Posix.Env (#5648)
diff --git a/System/Posix/Env.hsc b/System/Posix/Env.hsc
index 1a72a37..51e8891 100644
|
a
|
b
|
|
| 21 | 21 | , getEnvDefault |
| 22 | 22 | , getEnvironmentPrim |
| 23 | 23 | , getEnvironment |
| | 24 | , setEnvironment |
| 24 | 25 | , putEnv |
| 25 | 26 | , setEnv |
| 26 | 27 | , unsetEnv |
| | 28 | , clearEnv |
| 27 | 29 | ) where |
| 28 | 30 | |
| 29 | 31 | #include "HsUnix.h" |
| … |
… |
|
| 34 | 36 | import Foreign.Marshal.Array |
| 35 | 37 | import Foreign.Ptr |
| 36 | 38 | import Foreign.Storable |
| 37 | | import Control.Monad (liftM) |
| | 39 | import Control.Monad (liftM, forM_, void) |
| 38 | 40 | import Data.Maybe (fromMaybe) |
| 39 | 41 | #if __GLASGOW_HASKELL__ > 700 |
| 40 | 42 | import System.Posix.Internals (withFilePath, peekFilePath) |
| … |
… |
|
| 73 | 75 | getEnvironmentPrim :: IO [String] |
| 74 | 76 | getEnvironmentPrim = do |
| 75 | 77 | c_environ <- getCEnviron |
| 76 | | arr <- peekArray0 nullPtr c_environ |
| 77 | | mapM peekFilePath arr |
| | 78 | -- environ can be NULL |
| | 79 | if c_environ == nullPtr |
| | 80 | then return [] |
| | 81 | else do |
| | 82 | arr <- peekArray0 nullPtr c_environ |
| | 83 | mapM peekFilePath arr |
| 78 | 84 | |
| 79 | 85 | getCEnviron :: IO (Ptr CString) |
| 80 | 86 | #if darwin_HOST_OS |
| … |
… |
|
| 102 | 108 | dropEq (x,'=':ys) = (x,ys) |
| 103 | 109 | dropEq (x,_) = error $ "getEnvironment: insane variable " ++ x |
| 104 | 110 | |
| | 111 | -- |'setEnvironment' resets the entire environment to the given list of |
| | 112 | -- @(key,value)@ pairs. |
| | 113 | |
| | 114 | setEnvironment :: [(String,String)] -> IO () |
| | 115 | setEnvironment env = do |
| | 116 | clearEnv |
| | 117 | forM_ env $ \(key,value) -> |
| | 118 | setEnv key value True {-overwrite-} |
| | 119 | |
| 105 | 120 | -- |The 'unsetEnv' function deletes all instances of the variable name |
| 106 | 121 | -- from the environment. |
| 107 | 122 | |
| … |
… |
|
| 152 | 167 | Just _ -> return () |
| 153 | 168 | Nothing -> putEnv (key++"="++value) |
| 154 | 169 | #endif |
| | 170 | |
| | 171 | -- |The 'clearEnv' function clears the environment of all name-value pairs. |
| | 172 | clearEnv :: IO () |
| | 173 | clearEnv = void c_clearenv |
| | 174 | |
| | 175 | foreign import ccall unsafe "clearenv" |
| | 176 | c_clearenv :: IO Int |