summaryrefslogtreecommitdiff
path: root/src/Utility/Env/Set.hs
blob: bd835e978744b091861d62bbc958aa2a0f94f809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{- portable environment variables
 -
 - Copyright 2013 Joey Hess <id@joeyh.name>
 -
 - License: BSD-2-clause
 -}

{-# LANGUAGE CPP #-}

module Utility.Env.Set where

#ifdef mingw32_HOST_OS
import qualified System.Environment as E
import qualified System.SetEnv
import Utility.Env
#else
import qualified System.Posix.Env as PE
#endif

{- Sets an environment variable. To overwrite an existing variable,
 - overwrite must be True.
 -
 - On Windows, setting a variable to "" unsets it. -}
setEnv :: String -> String -> Bool -> IO ()
#ifndef mingw32_HOST_OS
setEnv var val overwrite = PE.setEnv var val overwrite
#else
setEnv var val True = System.SetEnv.setEnv var val
setEnv var val False = do
	r <- getEnv var
	case r of
		Nothing -> setEnv var val True
		Just _ -> return ()
#endif

unsetEnv :: String -> IO ()
#ifndef mingw32_HOST_OS
unsetEnv = PE.unsetEnv
#else
unsetEnv = System.SetEnv.unsetEnv
#endif