Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
unix
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rinat Striungis
unix
Commits
335fd69c
Commit
335fd69c
authored
13 years ago
by
pcapriotti
Browse files
Options
Downloads
Patches
Plain Diff
Add setEnvironment and cleanEnv to System.Posix.Env (#5648)
parent
e518038a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
System/Posix/Env.hsc
+25
-3
25 additions, 3 deletions
System/Posix/Env.hsc
with
25 additions
and
3 deletions
System/Posix/Env.hsc
+
25
−
3
View file @
335fd69c
...
@@ -21,9 +21,11 @@ module System.Posix.Env (
...
@@ -21,9 +21,11 @@ module System.Posix.Env (
, getEnvDefault
, getEnvDefault
, getEnvironmentPrim
, getEnvironmentPrim
, getEnvironment
, getEnvironment
, setEnvironment
, putEnv
, putEnv
, setEnv
, setEnv
, unsetEnv
, unsetEnv
, clearEnv
) where
) where
#include "HsUnix.h"
#include "HsUnix.h"
...
@@ -34,7 +36,7 @@ import Foreign.C.String
...
@@ -34,7 +36,7 @@ import Foreign.C.String
import Foreign.Marshal.Array
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Ptr
import Foreign.Storable
import Foreign.Storable
import Control.Monad (liftM)
import Control.Monad (liftM
, forM_, void
)
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe)
#if __GLASGOW_HASKELL__ > 700
#if __GLASGOW_HASKELL__ > 700
import System.Posix.Internals (withFilePath, peekFilePath)
import System.Posix.Internals (withFilePath, peekFilePath)
...
@@ -73,8 +75,12 @@ foreign import ccall unsafe "getenv"
...
@@ -73,8 +75,12 @@ foreign import ccall unsafe "getenv"
getEnvironmentPrim :: IO [String]
getEnvironmentPrim :: IO [String]
getEnvironmentPrim = do
getEnvironmentPrim = do
c_environ <- getCEnviron
c_environ <- getCEnviron
arr <- peekArray0 nullPtr c_environ
-- environ can be NULL
mapM peekFilePath arr
if c_environ == nullPtr
then return []
else do
arr <- peekArray0 nullPtr c_environ
mapM peekFilePath arr
getCEnviron :: IO (Ptr CString)
getCEnviron :: IO (Ptr CString)
#if darwin_HOST_OS
#if darwin_HOST_OS
...
@@ -102,6 +108,15 @@ getEnvironment = do
...
@@ -102,6 +108,15 @@ getEnvironment = do
dropEq (x,'=':ys) = (x,ys)
dropEq (x,'=':ys) = (x,ys)
dropEq (x,_) = error $ "getEnvironment: insane variable " ++ x
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
-- |The 'unsetEnv' function deletes all instances of the variable name
-- from the environment.
-- from the environment.
...
@@ -152,3 +167,10 @@ setEnv key value False = do
...
@@ -152,3 +167,10 @@ setEnv key value False = do
Just _ -> return ()
Just _ -> return ()
Nothing -> putEnv (key++"="++value)
Nothing -> putEnv (key++"="++value)
#endif
#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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment