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
27 import System.IO ( bracket )
35 #ifdef __GLASGOW_HASKELL__
43 -- ---------------------------------------------------------------------------
44 -- getArgs, getProgName, getEnv
46 -- Computation `getArgs' returns a list of the program's command
47 -- line arguments (not including the program name).
50 getArgs :: IO [String]
53 alloca $ \ p_argv -> do
54 getProgArgv p_argc p_argv
55 p <- fromIntegral `liftM` peek p_argc
57 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
60 foreign import ccall unsafe "getProgArgv"
61 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
64 Computation 'getProgName' returns the name of the program as it was
67 However, this is hard-to-impossible to implement on some non-Unix
68 OSes, so instead, for maximum portability, we just return the leafname
69 of the program as invoked. Even then there are some differences
70 between platforms: on Windows, for example, a program invoked as foo
71 is probably really @FOO.EXE@, and that is what 'getProgName' will return.
73 getProgName :: IO String
76 alloca $ \ p_argv -> do
77 getProgArgv p_argc p_argv
81 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
82 unpackProgName argv = do
83 s <- peekElemOff argv 0 >>= peekCString
86 basename :: String -> String
91 | isPathSeparator x = go xs xs
92 | otherwise = go acc xs
94 isPathSeparator :: Char -> Bool
95 isPathSeparator '/' = True
96 #ifdef mingw32_TARGET_OS
97 isPathSeparator '\\' = True
99 isPathSeparator _ = False
102 -- Computation `getEnv var' returns the value
103 -- of the environment variable {\em var}.
105 -- This computation may fail with
106 -- NoSuchThing: The environment variable does not exist.
108 getEnv :: String -> IO String
110 withCString name $ \s -> do
111 litstring <- c_getenv s
112 if litstring /= nullPtr
113 then peekCString litstring
114 else ioException (IOError Nothing NoSuchThing "getEnv"
115 "no environment variable" (Just name))
117 foreign import ccall unsafe "getenv"
118 c_getenv :: CString -> IO (Ptr CChar)
121 @withArgs args act@ - while executing action @act@, have 'System.getArgs'
125 p <- System.Environment.getProgName
129 @withProgName name act@ - while executing action @act@, have 'System.getProgName'return @name@.
131 withProgName nm act = do
132 xs <- System.Environment.getArgs
135 -- Worker routine which marshals and replaces an argv vector for
136 -- the duration of an action.
138 withArgv new_args act = do
139 pName <- System.Environment.getProgName
140 existing_args <- System.Environment.getArgs
141 bracket (setArgs new_args)
142 (\argv -> do setArgs (pName:existing_args); freeArgv argv)
145 freeArgv :: Ptr CString -> IO ()
147 size <- lengthArray0 nullPtr argv
148 sequence_ [peek (argv `advancePtr` i) >>= free | i <- [size, size-1 .. 0]]
151 setArgs :: [String] -> IO (Ptr CString)
153 vs <- mapM newCString argv >>= newArray0 nullPtr
154 setArgsPrim (length argv) vs
157 foreign import ccall unsafe "setProgArgv"
158 setArgsPrim :: Int -> Ptr CString -> IO ()
159 #endif /* __HUGS__ */