1 -----------------------------------------------------------------------------
3 -- Module : System.Environment
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- $Id: Environment.hs,v 1.4 2002/01/02 14:40:11 simonmar Exp $
13 -- Miscellaneous information about the system environment.
15 -----------------------------------------------------------------------------
17 module System.Environment
19 , getArgs -- :: IO [String]
20 , getProgName -- :: IO String
21 , getEnv -- :: String -> IO String
30 #ifdef __GLASGOW_HASKELL__
34 -- ---------------------------------------------------------------------------
35 -- getArgs, getProgName, getEnv
37 -- Computation `getArgs' returns a list of the program's command
38 -- line arguments (not including the program name).
40 getArgs :: IO [String]
43 alloca $ \ p_argv -> do
44 getProgArgv p_argc p_argv
45 p <- fromIntegral `liftM` peek p_argc
47 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
50 foreign import "getProgArgv" unsafe
51 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
53 -- Computation `getProgName' returns the name of the program
56 getProgName :: IO String
59 alloca $ \ p_argv -> do
60 getProgArgv p_argc p_argv
64 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
65 unpackProgName argv = do
66 s <- peekElemOff argv 0 >>= peekCString
69 basename :: String -> String
74 | isPathSeparator x = go xs xs
75 | otherwise = go acc xs
77 isPathSeparator :: Char -> Bool
78 isPathSeparator '/' = True
79 #ifdef mingw32_TARGET_OS
80 isPathSeparator '\\' = True
82 isPathSeparator _ = False
85 -- Computation `getEnv var' returns the value
86 -- of the environment variable {\em var}.
88 -- This computation may fail with
89 -- NoSuchThing: The environment variable does not exist.
91 getEnv :: String -> IO String
93 withCString name $ \s -> do
94 litstring <- c_getenv s
95 if litstring /= nullPtr
96 then peekCString litstring
97 else ioException (IOError Nothing NoSuchThing "getEnv"
98 "no environment variable" (Just name))
100 foreign import ccall "getenv" unsafe
101 c_getenv :: CString -> IO (Ptr CChar)