Commit badbad31 authored by Edward Z. Yang's avatar Edward Z. Yang

Test for getProgName.

Signed-off-by: Edward Z. Yang's avatarEdward Z. Yang <ezyang@mit.edu>
parent d7f30389
......@@ -153,6 +153,8 @@ test('ffi020', [ omit_ways(prof_ways),
test('ffi021', normal, compile_and_run, [''])
test('ffi022', normal, compile_and_run, [''])
if config.platform == 'i386-unknown-mingw32':
# This test needs a larger C stack than we get by default on Windows
flagsFor4038 = ['-optl-Wl,--stack,10485760']
......
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign.C
import Foreign
getProgName :: IO String
getProgName =
alloca $ \ p_argc ->
alloca $ \ p_argv -> do
getProgArgv p_argc p_argv
argv <- peek p_argv
unpackProgName argv
unpackProgName :: Ptr (Ptr CChar) -> IO String -- argv[0]
unpackProgName argv = do
s <- peekElemOff argv 0 >>= peekCString
return (basename s)
where
basename :: String -> String
basename f = go f f
where
go acc [] = acc
go acc (x:xs)
| isPathSeparator x = go xs xs
| otherwise = go acc xs
isPathSeparator :: Char -> Bool
isPathSeparator '/' = True
isPathSeparator _ = False
foreign import ccall unsafe "getProgArgv"
getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
main :: IO ()
main = print =<< getProgName
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment