Skip to content

Synchronize ByteString and String modules #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions System/Posix/Env/ByteString.hsc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE Trustworthy #-}

-----------------------------------------------------------------------------
-- |
Expand All @@ -21,23 +21,28 @@ module System.Posix.Env.ByteString (
, getEnvDefault
, getEnvironmentPrim
, getEnvironment
, setEnvironment
, putEnv
, setEnv
, unsetEnv
, unsetEnv
, clearEnv

-- * Program arguments
, getArgs
) where

#include "HsUnix.h"

import Control.Monad
import Foreign
import Foreign.C
import Data.Maybe ( fromMaybe )

import System.Posix.Env ( clearEnv )
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Data.ByteString (ByteString)
import Data.ByteString.Internal (ByteString (PS), memcpy)

-- |'getEnv' looks up a variable in the environment.

Expand Down Expand Up @@ -96,6 +101,18 @@ getEnvironment = do
| BC.head y == '=' = (x,B.tail y)
| otherwise = error $ "getEnvironment: insane variable " ++ BC.unpack x

-- |'setEnvironment' resets the entire environment to the given list of
-- @(key,value)@ pairs.
--
-- @since 2.8.0.0
setEnvironment ::
[(ByteString,ByteString)] {- ^ @[(key,value)]@ -} ->
IO ()
setEnvironment env = do
clearEnv
forM_ env $ \(key,value) ->
setEnv key value True {-overwrite-}

-- |The 'unsetEnv' function deletes all instances of the variable name
-- from the environment.

Expand All @@ -116,15 +133,25 @@ foreign import capi unsafe "HsUnix.h unsetenv"
c_unsetenv :: CString -> IO ()
# endif
#else
unsetEnv name = putEnv (name ++ "=")
unsetEnv name = putEnv (BC.snoc name '=')
#endif

-- |'putEnv' function takes an argument of the form @name=value@
-- and is equivalent to @setEnv(key,value,True{-overwrite-})@.

putEnv :: ByteString {- ^ "key=value" -} -> IO ()
putEnv keyvalue = B.useAsCString keyvalue $ \s ->
throwErrnoIfMinus1_ "putenv" (c_putenv s)
putEnv (PS fp o l) = withForeignPtr fp $ \p -> do
-- https://pubs.opengroup.org/onlinepubs/009696899/functions/putenv.html
--
-- "the string pointed to by string shall become part of the environment,
-- so altering the string shall change the environment. The space used by
-- string is no longer used once a new string which defines name is passed to putenv()."
--
-- hence we must not free the buffer
buf <- mallocBytes (l+1)
memcpy buf (p `plusPtr` o) l
pokeByteOff buf l (0::Word8)
throwErrnoIfMinus1_ "putenv" (c_putenv (castPtr buf))

foreign import ccall unsafe "putenv"
c_putenv :: CString -> IO CInt
Expand Down
32 changes: 32 additions & 0 deletions tests/PutEnv001.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -O0 -Wno-name-shadowing #-}

module Main (main) where

import Data.String ( fromString )
import System.Mem
import System.Posix.Env.ByteString
import Test.Tasty
import Test.Tasty.HUnit

-- test regression of incorrect 'free': https://github.com/haskell/unix/issues/68#issue-170072591
main :: IO ()
main = do
putEnv "foo=bar"
defaultMain $ testGroup "All" [ test ]

test :: TestTree
test = testCase "putEnv" $ do
performMinorGC
env <- System.Posix.Env.ByteString.getEnv (fromString "foo")
performMinorGC
print env
env <- System.Posix.Env.ByteString.getEnv (fromString "foo")
performMinorGC
print env
env <- System.Posix.Env.ByteString.getEnv (fromString "foo")
performMinorGC
print env
env <- System.Posix.Env.ByteString.getEnv (fromString "foo")
print env
env @?= Just (fromString "bar")
Comment on lines +18 to +32
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure is a little hard to trigger... but this worked for me and the prints are necessary.

8 changes: 8 additions & 0 deletions unix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,11 @@ test-suite Terminal
default-language: Haskell2010
build-depends: base, unix, tasty-hunit
ghc-options: -Wall

test-suite PutEnv001
hs-source-dirs: tests
main-is: PutEnv001.hs
type: exitcode-stdio-1.0
default-language: Haskell2010
build-depends: base, unix, tasty, tasty-hunit
ghc-options: -Wall -with-rtsopts=-V0 -O0