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.3 2001/12/21 15:07:26 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
29 #ifdef __GLASGOW_HASKELL__
33 -- ---------------------------------------------------------------------------
34 -- getArgs, getProgName, getEnv
36 -- Computation `getArgs' returns a list of the program's command
37 -- line arguments (not including the program name).
39 getArgs :: IO [String]
42 alloca $ \ p_argv -> do
43 getProgArgv p_argc p_argv
44 p <- fromIntegral `liftM` peek p_argc
46 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
49 foreign import "getProgArgv" unsafe
50 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
52 -- Computation `getProgName' returns the name of the program
55 getProgName :: IO String
58 alloca $ \ p_argv -> do
59 getProgArgv p_argc p_argv
63 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
64 unpackProgName argv = do
65 s <- peekElemOff argv 0 >>= peekCString
68 basename :: String -> String
73 | isPathSeparator x = go xs xs
74 | otherwise = go acc xs
76 isPathSeparator :: Char -> Bool
77 isPathSeparator '/' = True
78 #ifdef mingw32_TARGET_OS
79 isPathSeparator '\\' = True
81 isPathSeparator _ = False
84 -- Computation `getEnv var' returns the value
85 -- of the environment variable {\em var}.
87 -- This computation may fail with
88 -- NoSuchThing: The environment variable does not exist.
90 getEnv :: String -> IO String
92 withCString name $ \s -> do
93 litstring <- c_getenv s
94 if litstring /= nullPtr
95 then peekCString litstring
96 else ioException (IOError Nothing NoSuchThing "getEnv"
97 "no environment variable" (Just name))
99 foreign import ccall "getenv" unsafe
100 c_getenv :: CString -> IO (Ptr CChar)