{-
symbolic links to the executable:

Linux:
/proc/<pid>/exe

Solaris: (Solaris 10 only???)
/proc/<pid>/object/a.out (filename only)
/proc/<pid>/path/a.out (complete pathname)

*BSD:
/proc/<pid>/file
-}

{-# LANGUAGE ForeignFunctionInterface #-}

module System.Environment.Executable.Linux
  ( getExecutablePath  
  , getPID
  )
  where

import Data.Bits
import Data.Word
import Data.Int

import Control.Monad

import Foreign
import Foreign.C

import System.Posix
--import System.FilePath

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

getPID :: IO Int
getPID :: IO Int
getPID = (ProcessID -> Int) -> IO ProcessID -> IO Int
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ProcessID -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (IO ProcessID -> IO Int) -> IO ProcessID -> IO Int
forall a b. (a -> b) -> a -> b
$ IO ProcessID
getProcessID

getExecutablePath :: IO FilePath
getExecutablePath :: IO FilePath
getExecutablePath = do
  Int
pid <- IO Int
getPID
  FilePath
fname <- FilePath -> IO FilePath
readSymbolicLink (FilePath -> IO FilePath) -> FilePath -> IO FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
"/proc/" FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ Int -> FilePath
forall a. Show a => a -> FilePath
show Int
pid FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
"/exe"
  --let (path,exename) = splitFileName fname
  --return path
  FilePath -> IO FilePath
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
fname
  
--------------------------------------------------------------------------------