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 -- Miscellaneous information about the system environment.
13 -----------------------------------------------------------------------------
15 module System.Environment
17 , getArgs -- :: IO [String]
18 , getProgName -- :: IO String
19 , getEnv -- :: String -> IO String
28 #ifdef __GLASGOW_HASKELL__
32 -- ---------------------------------------------------------------------------
33 -- getArgs, getProgName, getEnv
35 -- Computation `getArgs' returns a list of the program's command
36 -- line arguments (not including the program name).
38 getArgs :: IO [String]
41 alloca $ \ p_argv -> do
42 getProgArgv p_argc p_argv
43 p <- fromIntegral `liftM` peek p_argc
45 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
48 foreign import ccall unsafe "getProgArgv"
49 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
51 -- Computation `getProgName' returns the name of the program
54 getProgName :: IO String
57 alloca $ \ p_argv -> do
58 getProgArgv p_argc p_argv
62 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
63 unpackProgName argv = do
64 s <- peekElemOff argv 0 >>= peekCString
67 basename :: String -> String
72 | isPathSeparator x = go xs xs
73 | otherwise = go acc xs
75 isPathSeparator :: Char -> Bool
76 isPathSeparator '/' = True
77 #ifdef mingw32_TARGET_OS
78 isPathSeparator '\\' = True
80 isPathSeparator _ = False
83 -- Computation `getEnv var' returns the value
84 -- of the environment variable {\em var}.
86 -- This computation may fail with
87 -- NoSuchThing: The environment variable does not exist.
89 getEnv :: String -> IO String
91 withCString name $ \s -> do
92 litstring <- c_getenv s
93 if litstring /= nullPtr
94 then peekCString litstring
95 else ioException (IOError Nothing NoSuchThing "getEnv"
96 "no environment variable" (Just name))
98 foreign import ccall unsafe "getenv"
99 c_getenv :: CString -> IO (Ptr CChar)