1 -----------------------------------------------------------------------------
3 -- Module : System.Environment
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/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
20 #ifdef __GLASGOW_HASKELL__
27 import Control.Exception ( bracket )
29 #ifdef __GLASGOW_HASKELL__
48 -- ---------------------------------------------------------------------------
49 -- getArgs, getProgName, getEnv
51 -- Computation `getArgs' returns a list of the program's command
52 -- line arguments (not including the program name).
54 #ifdef __GLASGOW_HASKELL__
55 getArgs :: IO [String]
58 alloca $ \ p_argv -> do
59 getProgArgv p_argc p_argv
60 p <- fromIntegral `liftM` peek p_argc
62 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
65 foreign import ccall unsafe "getProgArgv"
66 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
69 Computation 'getProgName' returns the name of the program as it was
72 However, this is hard-to-impossible to implement on some non-Unix
73 OSes, so instead, for maximum portability, we just return the leafname
74 of the program as invoked. Even then there are some differences
75 between platforms: on Windows, for example, a program invoked as foo
76 is probably really @FOO.EXE@, and that is what 'getProgName' will return.
78 getProgName :: IO String
81 alloca $ \ p_argv -> do
82 getProgArgv p_argc p_argv
86 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
87 unpackProgName argv = do
88 s <- peekElemOff argv 0 >>= peekCString
91 basename :: String -> String
96 | isPathSeparator x = go xs xs
97 | otherwise = go acc xs
99 isPathSeparator :: Char -> Bool
100 isPathSeparator '/' = True
101 #ifdef mingw32_TARGET_OS
102 isPathSeparator '\\' = True
104 isPathSeparator _ = False
107 -- Computation `getEnv var' returns the value
108 -- of the environment variable {\em var}.
110 -- This computation may fail with
111 -- NoSuchThing: The environment variable does not exist.
113 getEnv :: String -> IO String
115 withCString name $ \s -> do
116 litstring <- c_getenv s
117 if litstring /= nullPtr
118 then peekCString litstring
119 else ioException (IOError Nothing NoSuchThing "getEnv"
120 "no environment variable" (Just name))
122 foreign import ccall unsafe "getenv"
123 c_getenv :: CString -> IO (Ptr CChar)
126 @withArgs args act@ - while executing action @act@, have 'System.getArgs'
130 p <- System.Environment.getProgName
134 @withProgName name act@ - while executing action @act@, have 'System.getProgName'return @name@.
136 withProgName nm act = do
137 xs <- System.Environment.getArgs
140 -- Worker routine which marshals and replaces an argv vector for
141 -- the duration of an action.
143 withArgv new_args act = do
144 pName <- System.Environment.getProgName
145 existing_args <- System.Environment.getArgs
146 bracket (setArgs new_args)
147 (\argv -> do setArgs (pName:existing_args); freeArgv argv)
150 freeArgv :: Ptr CString -> IO ()
152 size <- lengthArray0 nullPtr argv
153 sequence_ [peek (argv `advancePtr` i) >>= free | i <- [size, size-1 .. 0]]
156 setArgs :: [String] -> IO (Ptr CString)
158 vs <- mapM newCString argv >>= newArray0 nullPtr
159 setArgsPrim (length argv) vs
162 foreign import ccall unsafe "setProgArgv"
163 setArgsPrim :: Int -> Ptr CString -> IO ()
164 #endif /* __GLASGOW_HASKELL__ */