File: FilePath.hsc

package info (click to toggle)
haskell-unixutils 1.52-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 112 kB
  • sloc: haskell: 494; makefile: 2
file content (40 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE ForeignFunctionInterface #-}

-- | The function splitFileName is taken from missingh, at the moment
-- missingh will not build under sid.

module System.Unix.FilePath 
    (dirName,
     baseName,
     realpath,
     (<++>))
    where

import Data.List
import System.FilePath (makeRelative, (</>), takeFileName, dropFileName)
--import Text.Regex
import Foreign.C
import Foreign.Marshal.Array

#include <limits.h>
#include <stdlib.h>

-- |Concatenate two paths, making sure there is exactly one path separator.
a <++> b = a </> (makeRelative "" b)

-- |Use dropFileName
dirName :: FilePath -> FilePath
dirName = dropFileName

-- |Use takeFileName
baseName :: FilePath -> String
baseName = takeFileName

-- |resolve all references to /./, /../, extra slashes, and symlinks
realpath :: FilePath -> IO FilePath
realpath fp =
    withCString fp $ \cfp ->
        allocaArray (#const PATH_MAX) $ \res ->
            throwErrnoIfNull "realpath" (c_realpath cfp res) >>= peekCString

foreign import ccall unsafe "realpath" c_realpath :: CString -> CString -> IO CString