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.2 2001/08/17 12:50:34 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
46 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
49 foreign import "getProgArgv" getProgArgv :: Ptr Int -> 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
65 return (de_slash "" s)
67 -- re-start accumulating at every '/'
68 de_slash :: String -> String -> String
69 de_slash acc [] = reverse acc
70 de_slash _acc ('/':xs) = de_slash [] xs
71 de_slash acc (x:xs) = de_slash (x:acc) xs
73 -- Computation `getEnv var' returns the value
74 -- of the environment variable {\em var}.
76 -- This computation may fail with
77 -- NoSuchThing: The environment variable does not exist.
79 getEnv :: String -> IO String
81 withCString name $ \s -> do
82 litstring <- c_getenv s
83 if litstring /= nullPtr
84 then peekCString litstring
85 else ioException (IOError Nothing NoSuchThing "getEnv"
86 "no environment variable" (Just name))
88 foreign import ccall "getenv" unsafe
89 c_getenv :: CString -> IO (Ptr CChar)