Ticket #5648: 5648.patch

File 5648.patch, 2.1 KB (added by pcapriotti, 15 months ago)

Add setEnvironment and clearEnv

  • System/Posix/Env.hsc

    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  
    2121    , getEnvDefault 
    2222    , getEnvironmentPrim 
    2323    , getEnvironment 
     24    , setEnvironment 
    2425    , putEnv 
    2526    , setEnv 
    2627    , unsetEnv 
     28    , clearEnv 
    2729) where 
    2830 
    2931#include "HsUnix.h" 
     
    3436import Foreign.Marshal.Array 
    3537import Foreign.Ptr 
    3638import Foreign.Storable 
    37 import Control.Monad (liftM) 
     39import Control.Monad (liftM, forM_, void) 
    3840import Data.Maybe (fromMaybe) 
    3941#if __GLASGOW_HASKELL__ > 700 
    4042import System.Posix.Internals (withFilePath, peekFilePath) 
     
    7375getEnvironmentPrim :: IO [String] 
    7476getEnvironmentPrim = do 
    7577  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 
    7884 
    7985getCEnviron :: IO (Ptr CString) 
    8086#if darwin_HOST_OS 
     
    102108   dropEq (x,'=':ys) = (x,ys) 
    103109   dropEq (x,_)      = error $ "getEnvironment: insane variable " ++ x 
    104110 
     111-- |'setEnvironment' resets the entire environment to the given list of 
     112-- @(key,value)@ pairs. 
     113 
     114setEnvironment :: [(String,String)] -> IO () 
     115setEnvironment env = do 
     116  clearEnv 
     117  forM_ env $ \(key,value) -> 
     118    setEnv key value True {-overwrite-} 
     119 
    105120-- |The 'unsetEnv' function deletes all instances of the variable name 
    106121-- from the environment. 
    107122 
     
    152167    Just _  -> return () 
    153168    Nothing -> putEnv (key++"="++value) 
    154169#endif 
     170 
     171-- |The 'clearEnv' function clears the environment of all name-value pairs. 
     172clearEnv :: IO () 
     173clearEnv = void c_clearenv 
     174 
     175foreign import ccall unsafe "clearenv" 
     176  c_clearenv :: IO Int