summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAycanIrican <>2010-06-01 22:33:59 (GMT)
committerLuite Stegeman <luite@luite.com>2010-06-01 22:33:59 (GMT)
commitc158324f764cbcab9263aac10db880aff8ee12bd (patch)
tree0fec937c67bb5434ad20723d820f395604555c6e
parent6c3bdf3ac2db55148fc8fc90bdad357fdd4bde53 (diff)
version 0.30.3
-rw-r--r--System/Syscall.hs41
-rw-r--r--System/Syscall/Darwin.hsc51
-rw-r--r--System/Syscall/FreeBSD.hsc16
-rw-r--r--System/Syscall/Linux.hsc81
-rw-r--r--hsyscall.cabal6
5 files changed, 68 insertions, 127 deletions
diff --git a/System/Syscall.hs b/System/Syscall.hs
index b3a7ecf..08fa174 100644
--- a/System/Syscall.hs
+++ b/System/Syscall.hs
@@ -3,14 +3,51 @@
module System.Syscall
( c_open, c_close,
c_read, c_write,
- c_fcntl,
- StructStat(..), c_stat, c_fstat, c_lstat,
+ c_stat,
+ c_fcntl_read, c_fcntl_write, c_fcntl_lock,
c_sendfile
) where
+import System.Posix.Internals
+import System.Posix.Types
+import Foreign.C
+import Foreign.Storable(poke)
+import Foreign.Marshal (alloca)
+import Foreign.Ptr (Ptr, nullPtr)
+
#if defined(LINUX)
import System.Syscall.Linux
#endif
+
#if defined(DARWIN)
import System.Syscall.Darwin
#endif
+
+#if defined(FREEBSD)
+import System.Syscall.FreeBSD
+#endif
+
+-- | A unified primitive signature for the sendfile syscall. The number of bytes written and the offset value
+c_sendfile :: Fd -> Fd -> COff -> CSize -> IO CSsize
+
+#if defined(LINUX)
+c_sendfile ofd ifd o c = do
+ alloca $ \off -> do
+ poke off o
+ r <- c_sendfile_linux ofd ifd off c
+ return r
+#endif
+
+#if defined(DARWIN)
+c_sendfile ofd ifd o c = do
+ alloca $ \sbytes ->
+ do poke sbytes c
+ c_sendfile_darwin ifd ofd o sbytes 0 0
+#endif
+
+#if defined(FREEBSD)
+c_sendfile ofd ifd o c = do
+ alloca $ \sbytes ->
+ do poke sbytes c
+ c_sendfile_freebsd ifd ofd o c 0 sbytes 0
+#endif \ No newline at end of file
diff --git a/System/Syscall/Darwin.hsc b/System/Syscall/Darwin.hsc
index 011bf3d..10b10c4 100644
--- a/System/Syscall/Darwin.hsc
+++ b/System/Syscall/Darwin.hsc
@@ -2,57 +2,14 @@
-- | Darwin System Calls
-- http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/#group_Section_2
module System.Syscall.Linux
- ( c_open, c_close,
- c_read, c_write,
- c_fcntl,
- StructStat(..), c_stat, c_fstat, c_lstat,
- c_sendfile
+ ( c_sendfile_darwin
)
where
-import Foreign
import Foreign.C
-#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
-
-foreign import ccall unsafe "fcntl.h open" c_open
- :: CString -> CInt -> IO CInt
-foreign import ccall unsafe "unistd.h close" c_close
- :: CInt -> IO CInt
-foreign import ccall unsafe "unistd.h read" c_read
- :: CInt -> Ptr () -> CSize -> IO (CSize)
-foreign import ccall unsafe "unistd.h write" c_write
- :: CInt -> Ptr () -> CSize -> IO (CSize)
-foreign import ccall unsafe "fcntl.h fcntl" c_fcntl
- :: CInt -> CInt -> CLong -> IO CInt
-
--- | Stat
--- int stat(const char *path, struct stat *buf);
--- int fstat(int fd, struct stat *buf);
--- int lstat(const char *path, struct stat *buf);
-#include <sys/stat.h>
-
-data StructStat = StructStat {
- stSize :: ! #{type off_t}
- }
-type StructStatPtr = Ptr StructStat
-
-instance Storable StructStat where
- alignment _ = #{alignment struct stat}
- sizeOf _ = #{size struct stat}
- peek p = do
- size <- #{peek struct stat, st_size} p
- return (StructStat size)
- poke p (StructStat size) = do
- #{poke struct stat, st_size} p size
-
-foreign import ccall unsafe "stat" c_stat
- :: CString -> StructStatPtr -> IO CInt
-foreign import ccall unsafe "fstat" c_fstat
- :: CInt -> StructStatPtr -> IO CInt
-foreign import ccall unsafe "lstat" c_lstat
- :: CString -> StructStatPtr -> IO CInt
+import System.Posix.Types (COff,Fd)
-- | sendfile
-- int sendfile(int fd, int s, off_t offset, off_t *len, struct sf_hdtr *hdtr, int flags);
-foreign import ccall unsafe "sys/uio.h sendfile" c_sendfile
- :: Fd -> Fd -> (#type off_t) -> Ptr (#type off_t) -> Ptr () -> CInt -> IO CInt
+foreign import ccall unsafe "sys/uio.h sendfile" c_sendfile_darwin
+ :: Fd -> Fd -> COff -> Ptr COff -> Ptr () -> CInt -> IO CInt
diff --git a/System/Syscall/FreeBSD.hsc b/System/Syscall/FreeBSD.hsc
new file mode 100644
index 0000000..4f39925
--- /dev/null
+++ b/System/Syscall/FreeBSD.hsc
@@ -0,0 +1,16 @@
+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
+-- | Linux System Calls
+-- http://fxr.watson.org/fxr/source/kern/syscalls.master
+module System.Syscall.FreeBSD
+ ( c_sendfile
+ )
+ where
+
+import Foreign
+import Foreign.C
+import System.Posix.Types (COff, Fd)
+
+-- | Sendfile
+-- int sendfile(int fd, int s, off_t offset, size_t nbytes, struct sf_hdtr *hdtr, off_t *sbytes, int flags);
+foreign import ccall unsafe "sys/uio.h sendfile" c_sendfile
+ :: Fd -> Fd -> COff -> CSize -> Ptr () -> Ptr COff -> CInt -> IO CInt
diff --git a/System/Syscall/Linux.hsc b/System/Syscall/Linux.hsc
index b3e3fd1..6e66bc9 100644
--- a/System/Syscall/Linux.hsc
+++ b/System/Syscall/Linux.hsc
@@ -2,90 +2,17 @@
-- | Linux System Calls
-- http://www.kernel.org/doc/man-pages/online/pages/man2/syscalls.2.html
module System.Syscall.Linux
- ( c_open, c_close,
- c_read, c_write,
- c_fcntl,
- StructStat(..), c_stat, c_fstat, c_lstat,
- c_sendfile
+ ( c_sendfile_linux
)
where
import Foreign
import Foreign.C
-#let alignment t = "%lu", (unsigned long)offsetof(struct {char x__; t (y__); }, y__)
-
-#include <unistd.h>
-
--- | Open, Close
--- int open(const char *pathname, int flags);
--- int close(int fd);
-#include <sys/fcntl.h>
-newtype FcntlFlags = FcntlFlags { unFcntlFlags :: CInt } deriving (Eq, Ord)
-
-#{enum FcntlFlags, FcntlFlags,
- o_rdonly = O_RDONLY,
- o_wronly = O_WRONLY,
- o_rdwr = O_RDWR,
- o_creat = O_CREAT,
- o_append = O_APPEND
- }
-
-instance Show FcntlFlags where
- show f | f == o_rdonly = "O_RDONLY"
- | f == o_wronly = "O_WRONLY"
- | f == o_rdwr = "O_RDWR"
- | f == o_creat = "O_CREAT"
- | f == o_append = "O_APPEND"
- | otherwise = show $ unFcntlFlags f
-
-foreign import ccall unsafe "unistd.h open" c_open
- :: CString -> CInt -> IO CInt
-foreign import ccall unsafe "unistd.h close" c_close
- :: CInt -> IO CInt
-
--- | Read, Write
-
-foreign import ccall unsafe "unistd.h read" c_read
- :: CInt -> Ptr () -> CSize -> IO (CSize)
-foreign import ccall unsafe "unistd.h write" c_write
- :: CInt -> Ptr () -> CSize -> IO (CSize)
-
--- | Fcntl
-
-foreign import ccall unsafe "fcntl.h fcntl" c_fcntl
- :: CInt -> CInt -> CLong -> IO CInt
-
--- | Stat
--- int stat(const char *path, struct stat *buf);
--- int fstat(int fd, struct stat *buf);
--- int lstat(const char *path, struct stat *buf);
-#include <sys/stat.h>
-
-data StructStat = StructStat {
- stSize :: ! #{type off_t}
- }
-
-type StructStatPtr = Ptr StructStat
-
-instance Storable StructStat where
- alignment _ = #{alignment struct stat}
- sizeOf _ = #{size struct stat}
- peek p = do
- size <- #{peek struct stat, st_size} p
- return (StructStat size)
- poke p (StructStat size) = do
- #{poke struct stat, st_size} p size
-
-foreign import ccall unsafe "stat" c_stat
- :: CString -> StructStatPtr -> IO CInt
-foreign import ccall unsafe "fstat" c_fstat
- :: CInt -> StructStatPtr -> IO CInt
-foreign import ccall unsafe "lstat" c_lstat
- :: CString -> StructStatPtr -> IO CInt
+import System.Posix.Types (COff,CSsize,Fd)
-- | Sendfile
-- ssize_t sendfile(int out_fd, int in_fd, off_t * offset ", size_t" " count" );
#include <sys/sendfile.h>
-foreign import ccall unsafe "sendfile" c_sendfile
- :: CInt -> CInt -> Ptr (#type off_t) -> (#type size_t) -> IO (#type ssize_t)
+foreign import ccall unsafe "sendfile" c_sendfile_linux
+ :: Fd -> Fd -> Ptr COff -> CSize -> IO CSsize
diff --git a/hsyscall.cabal b/hsyscall.cabal
index 9bc9c6c..b5abccc 100644
--- a/hsyscall.cabal
+++ b/hsyscall.cabal
@@ -1,5 +1,5 @@
Name: hsyscall
-Version: 0.2
+Version: 0.3
Synopsis: FFI to syscalls
Description: FFI to OS syscalls (open, close, sendfile etc...).
Homepage: http://github.com/aycanirican/hsyscall
@@ -30,6 +30,10 @@ Library
if os(darwin)
cpp-options: -DDARWIN
other-modules: System.Syscall.Darwin
+ if os(freebsd)
+ cpp-options: -DFREEBSD
+ other-modules: System.Syscall.FreeBSD
+
-- Packages needed in order to build this package.
Build-depends: base >= 4 && < 5