{-# LINE 1 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE CPP #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Environment.ExecutablePath
-- Copyright   :  (c) The University of Glasgow 2001
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  libraries@haskell.org
-- Stability   :  provisional
-- Portability :  portable
--
-- Function to retrieve the absolute filepath of the current executable.
--
-- @since 4.6.0.0
-----------------------------------------------------------------------------

module System.Environment.ExecutablePath
  ( getExecutablePath
#if !defined(javascript_HOST_ARCH)
  , executablePath
#endif
  ) where

#if defined(javascript_HOST_ARCH)

getExecutablePath :: IO FilePath
getExecutablePath = return "a.jsexe"

#else

-- The imports are purposely kept completely disjoint to prevent edits
-- to one OS implementation from breaking another.


{-# LINE 72 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
import Foreign.C
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
import System.Posix.Internals

{-# LINE 78 "libraries/base/System/Environment/ExecutablePath.hsc" #-}

-- The exported function is defined outside any if-guard to make sure
-- every OS implements it with the same type.

-- | Returns the absolute pathname of the current executable,
-- or @argv[0]@ if the operating system does not provide a reliable
-- way query the current executable.
--
-- Note that for scripts and interactive sessions, this is the path to
-- the interpreter (e.g. ghci.)
--
-- Since base 4.11.0.0, 'getExecutablePath' resolves symlinks on Windows.
-- If an executable is launched through a symlink, 'getExecutablePath'
-- returns the absolute path of the original executable.
--
-- If the executable has been deleted, behaviour is ill-defined and
-- varies by operating system.  See 'executablePath' for a more
-- reliable way to query the current executable.
--
-- @since 4.6.0.0
getExecutablePath :: IO FilePath

-- | Get an action to query the absolute pathname of the current executable.
--
-- If the operating system provides a reliable way to determine the current
-- executable, return the query action, otherwise return @Nothing@.  The action
-- is defined on FreeBSD, Linux, MacOS, NetBSD, and Windows.
--
-- Even where the query action is defined, there may be situations where no
-- result is available, e.g. if the executable file was deleted while the
-- program is running.  Therefore the result of the query action is a @Maybe
-- FilePath@.
--
-- Note that for scripts and interactive sessions, the result is the path to
-- the interpreter (e.g. ghci.)
--
-- Note also that while most operating systems return @Nothing@ if the
-- executable file was deleted/unlinked, some (including NetBSD) return the
-- original path.
--
-- @since 4.17.0.0
executablePath :: Maybe (IO (Maybe FilePath))


--------------------------------------------------------------------------------
-- Mac OS X


{-# LINE 362 "libraries/base/System/Environment/ExecutablePath.hsc" #-}

foreign import ccall unsafe "getFullProgArgv"
    c_getFullProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()

getExecutablePath :: IO FilePath
getExecutablePath =
    (Ptr CInt -> IO FilePath) -> IO FilePath
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO FilePath) -> IO FilePath)
-> (Ptr CInt -> IO FilePath) -> IO FilePath
forall a b. (a -> b) -> a -> b
$ \ Ptr CInt
p_argc ->
    (Ptr (Ptr CString) -> IO FilePath) -> IO FilePath
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr CString) -> IO FilePath) -> IO FilePath)
-> (Ptr (Ptr CString) -> IO FilePath) -> IO FilePath
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr CString)
p_argv -> do
        Ptr CInt -> Ptr (Ptr CString) -> IO ()
c_getFullProgArgv Ptr CInt
p_argc Ptr (Ptr CString)
p_argv
        CInt
argc <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
p_argc
        if CInt
argc CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
> CInt
0
            -- If argc > 0 then argv[0] is guaranteed by the standard
            -- to be a pointer to a null-terminated string.
            then Ptr (Ptr CString) -> IO (Ptr CString)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CString)
p_argv IO (Ptr CString) -> (Ptr CString -> IO CString) -> IO CString
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr CString -> IO CString
forall a. Storable a => Ptr a -> IO a
peek IO CString -> (CString -> IO FilePath) -> IO FilePath
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO FilePath
peekFilePath
            else FilePath -> IO FilePath
forall a. FilePath -> a
errorWithoutStackTrace (FilePath -> IO FilePath) -> FilePath -> IO FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
"getExecutablePath: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
msg
  where msg :: FilePath
msg = FilePath
"no OS specific implementation and program name couldn't be " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++
              FilePath
"found in argv"

executablePath :: Maybe (IO (Maybe FilePath))
executablePath = Maybe (IO (Maybe FilePath))
forall a. Maybe a
Nothing

--------------------------------------------------------------------------------


{-# LINE 384 "libraries/base/System/Environment/ExecutablePath.hsc" #-}

#endif