c20d7d765b2942088329f1f9ff6c23be23285c22
[packages/old-locale.git] / System / Environment.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : System.Environment
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
10 --
11 -- Miscellaneous information about the system environment.
12 --
13 -----------------------------------------------------------------------------
14
15 module System.Environment
16 (
17 getArgs -- :: IO [String]
18 , getProgName -- :: IO String
19 , getEnv -- :: String -> IO String
20 ) where
21
22 import Prelude
23
24 import Foreign
25 import Foreign.C
26 import Control.Monad
27
28 #ifdef __GLASGOW_HASKELL__
29 import GHC.IOBase
30 #endif
31
32 -- ---------------------------------------------------------------------------
33 -- getArgs, getProgName, getEnv
34
35 -- Computation `getArgs' returns a list of the program's command
36 -- line arguments (not including the program name).
37
38 getArgs :: IO [String]
39 getArgs =
40 alloca $ \ p_argc ->
41 alloca $ \ p_argv -> do
42 getProgArgv p_argc p_argv
43 p <- fromIntegral `liftM` peek p_argc
44 argv <- peek p_argv
45 peekArray (p - 1) (advancePtr argv 1) >>= mapM peekCString
46
47
48 foreign import ccall unsafe "getProgArgv"
49 getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
50
51 {-|
52 Computation 'getProgName' returns the name of the program as it was
53 invoked.
54
55 However, this is hard-to-impossible to implement on some non-Unix
56 OSes, so instead, for maximum portability, we just return the leafname
57 of the program as invoked. Even then there are some differences
58 between platforms: on Windows, for example, a program invoked as foo
59 is probably really @FOO.EXE@, and that is what 'getProgName' will return.
60 -}
61 getProgName :: IO String
62 getProgName =
63 alloca $ \ p_argc ->
64 alloca $ \ p_argv -> do
65 getProgArgv p_argc p_argv
66 argv <- peek p_argv
67 unpackProgName argv
68
69 unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
70 unpackProgName argv = do
71 s <- peekElemOff argv 0 >>= peekCString
72 return (basename s)
73 where
74 basename :: String -> String
75 basename f = go f f
76 where
77 go acc [] = acc
78 go acc (x:xs)
79 | isPathSeparator x = go xs xs
80 | otherwise = go acc xs
81
82 isPathSeparator :: Char -> Bool
83 isPathSeparator '/' = True
84 #ifdef mingw32_TARGET_OS
85 isPathSeparator '\\' = True
86 #endif
87 isPathSeparator _ = False
88
89
90 -- Computation `getEnv var' returns the value
91 -- of the environment variable {\em var}.
92
93 -- This computation may fail with
94 -- NoSuchThing: The environment variable does not exist.
95
96 getEnv :: String -> IO String
97 getEnv name =
98 withCString name $ \s -> do
99 litstring <- c_getenv s
100 if litstring /= nullPtr
101 then peekCString litstring
102 else ioException (IOError Nothing NoSuchThing "getEnv"
103 "no environment variable" (Just name))
104
105 foreign import ccall unsafe "getenv"
106 c_getenv :: CString -> IO (Ptr CChar)