setenv-0.1.1.3/0000755000000000000000000000000012454370507011353 5ustar0000000000000000setenv-0.1.1.3/LICENSE0000644000000000000000000000206712454370507012365 0ustar0000000000000000Copyright (c) 2012-2015 Simon Hengel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. setenv-0.1.1.3/setenv.cabal0000644000000000000000000000175612454370507013654 0ustar0000000000000000name: setenv version: 0.1.1.3 license: MIT license-file: LICENSE copyright: (c) 2012-2015 Simon Hengel author: Simon Hengel maintainer: Simon Hengel category: System synopsis: A cross-platform library for setting environment variables description: A cross-platform library for setting environment variables . Note: Since @base- 4.7.0.0@ the functions @setEnv@ and @unsetEnv@ are provided by @System.Environment@. @System.SetEnv@ merily re-exports those functions when built with @base >= 4.7@. build-type: Simple cabal-version: >= 1.8 source-repository head type: git location: https://github.com/sol/setenv library ghc-options: -Wall hs-source-dirs: src exposed-modules: System.SetEnv build-depends: base == 4.* if !os(windows) build-depends: unix setenv-0.1.1.3/Setup.lhs0000644000000000000000000000011412454370507013157 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMain setenv-0.1.1.3/src/0000755000000000000000000000000012454370507012142 5ustar0000000000000000setenv-0.1.1.3/src/System/0000755000000000000000000000000012454370507013426 5ustar0000000000000000setenv-0.1.1.3/src/System/SetEnv.hs0000644000000000000000000000671112454370507015173 0ustar0000000000000000{-# LANGUAGE CPP, ForeignFunctionInterface #-} module System.SetEnv ( setEnv , unsetEnv ) where #if MIN_VERSION_base(4,7,0) import System.Environment (setEnv, unsetEnv) #else #ifdef mingw32_HOST_OS import GHC.Windows import Foreign.Safe import Foreign.C import Control.Monad #else import qualified System.Posix.Env as Posix #endif #ifdef mingw32_HOST_OS # if defined(i386_HOST_ARCH) # define WINDOWS_CCONV stdcall # elif defined(x86_64_HOST_ARCH) # define WINDOWS_CCONV ccall # else # error Unknown mingw32 arch # endif foreign import WINDOWS_CCONV unsafe "windows.h GetLastError" c_GetLastError:: IO DWORD eRROR_ENVVAR_NOT_FOUND :: DWORD eRROR_ENVVAR_NOT_FOUND = 203 #endif -- | @setEnv name value@ sets the specified environment variable to @value@. -- -- On Windows setting an environment variable to the /empty string/ removes -- that environment variable from the environment. For the sake of -- compatibility we adopt that behavior. In particular -- -- @ -- setEnv name \"\" -- @ -- -- has the same effect as -- -- @ -- `unsetEnv` name -- @ -- -- If you don't care about Windows support and want to set an environment -- variable to the empty string use @System.Posix.Env.setEnv@ from the @unix@ -- package instead. -- -- Throws `Control.Exception.IOException` if @name@ is the empty string or -- contains an equals sign. setEnv :: String -> String -> IO () setEnv key value_ | null value = unsetEnv key | otherwise = setEnv_ key value where -- NOTE: Anything that follows NUL is ignored on both POSIX and Windows. -- We still strip it manually so that the null check above succeds if a -- value starts with NUL, and `unsetEnv` is called. This is important for -- two reasons. -- -- * On POSIX setting an environment variable to the empty string does not -- remove it. -- -- * On Windows setting an environment variable to the empty string -- removes that environment variable. A subsequent call to -- GetEnvironmentVariable will then return 0, but the calling thread's -- last-error code will not be updated, and hence a call to GetLastError -- may not return ERROR_ENVVAR_NOT_FOUND. The failed lookup will then -- result in a random error instead of the expected -- `isDoesNotExistError` (this is at least true for Windows XP, SP 3). -- Explicitly calling `unsetEnv` prevents this. value = takeWhile (/= '\NUL') value_ setEnv_ :: String -> String -> IO () #ifdef mingw32_HOST_OS setEnv_ key value = withCWString key $ \k -> withCWString value $ \v -> do success <- c_SetEnvironmentVariable k v unless success (throwGetLastError "setEnv") foreign import WINDOWS_CCONV unsafe "windows.h SetEnvironmentVariableW" c_SetEnvironmentVariable :: LPTSTR -> LPTSTR -> IO Bool #else setEnv_ k v = Posix.setEnv k v True #endif -- | @unsetEnv name@ removes the specified environment variable from the -- environment of the current process. -- -- Throws `Control.Exception.IOException` if @name@ is the empty string or -- contains an equals sign. unsetEnv :: String -> IO () #ifdef mingw32_HOST_OS unsetEnv key = withCWString key $ \k -> do success <- c_SetEnvironmentVariable k nullPtr unless success $ do -- We consider unsetting an environment variable that does not exist not as -- an error, hence we ignore eRROR_ENVVAR_NOT_FOUND. err <- c_GetLastError unless (err == eRROR_ENVVAR_NOT_FOUND) $ do throwGetLastError "unsetEnv" #else unsetEnv = Posix.unsetEnv #endif #endif