Ticket #5930: 0002-Add-System.Environment.lookupEnv-as-per-trac-5930.patch

File 0002-Add-System.Environment.lookupEnv-as-per-trac-5930.patch, 2.6 KB (added by elaforge, 2 years ago)

add lookupEnv

  • System/Environment.hs

    From 3f167fd6ddb074b008efa240f5d5cca329990eea Mon Sep 17 00:00:00 2001
    From: Evan Laforge <qdunkan@gmail.com>
    Date: Sun, 11 Mar 2012 19:38:42 -0700
    Subject: [PATCH 2/2] Add System.Environment.lookupEnv, as per trac #5930
    
    ---
     System/Environment.hs |   42 +++++++++++++++++++++++++++++-------------
     1 files changed, 29 insertions(+), 13 deletions(-)
    
    diff --git a/System/Environment.hs b/System/Environment.hs
    index 72d7eba..3dd9502 100644
    a b basename f = go f f 
    190190 
    191191getEnv :: String -> IO String 
    192192#ifdef mingw32_HOST_OS 
    193 getEnv name = withCWString name $ \s -> try_size s 256 
    194   where 
    195     try_size s size = allocaArray (fromIntegral size) $ \p_value -> do 
    196       res <- c_GetEnvironmentVariable s p_value size 
    197       case res of 
    198         0 -> do 
    199                   err <- c_GetLastError 
    200                   if err == eRROR_ENVVAR_NOT_FOUND 
    201                    then ioe_missingEnvVar name 
    202                    else throwGetLastError "getEnv" 
    203         _ | res > size -> try_size s res -- Rare: size increased between calls to GetEnvironmentVariable 
    204           | otherwise  -> peekCWString p_value 
     193getEnv name = do 
     194    mbVal <- lookupEnv name 
     195    case mbVal of 
     196        Nothing -> do 
     197            err <- c_GetLastError 
     198            if err == eRROR_ENVVAR_NOT_FOUND 
     199                then ioe_missingEnvVar name 
     200                else throwGetLastError "getEnv" 
     201        Just val -> return val 
    205202 
    206203eRROR_ENVVAR_NOT_FOUND :: DWORD 
    207204eRROR_ENVVAR_NOT_FOUND = 203 
    eRROR_ENVVAR_NOT_FOUND = 203 
    209206foreign import stdcall unsafe "windows.h GetLastError" 
    210207  c_GetLastError:: IO DWORD 
    211208 
     209#else 
     210getEnv name = maybe (ioe_missingEnvVar name) return =<< lookupEnv name 
     211#endif 
     212 
     213-- | Return the value of the environment variable @var@, or @Nothing@ if 
     214-- there is no such value. 
     215-- 
     216-- For POSIX users, this is equivalent to 'System.Posix.Environ.Env.getEnv'. 
     217lookupEnv :: String -> IO (Maybe String) 
     218#ifdef mingw32_HOST_OS 
     219lookupEnv name = withCWString name $ \s -> try_size s 256 
     220  where 
     221    try_size s size = allocaArray (fromIntegral size) $ \p_value -> do 
     222      res <- c_GetEnvironmentVariable s p_value size 
     223      case res of 
     224        0 -> return Nothing 
     225        _ | res > size -> try_size s res -- Rare: size increased between calls to GetEnvironmentVariable 
     226          | otherwise  -> peekCWString p_value 
     227 
    212228foreign import stdcall unsafe "windows.h GetEnvironmentVariableW" 
    213229  c_GetEnvironmentVariable :: LPTSTR -> LPTSTR -> DWORD -> IO DWORD 
    214230#else 
    215 getEnv name = 
     231lookupEnv name = 
    216232    withCString name $ \s -> do 
    217233      litstring <- c_getenv s 
    218234      if litstring /= nullPtr