summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAycanIrican <>2010-05-31 16:31:14 (GMT)
committerLuite Stegeman <luite@luite.com>2010-05-31 16:31:14 (GMT)
commit6f99465f847c09f463dcec42e6d6a3c80db5a727 (patch)
tree3fa1f4ddc7c05b184190bad3974cdff3fa154d7d
version 0.10.1
-rw-r--r--LICENSE30
-rw-r--r--Setup.hs3
-rw-r--r--System/Syscall.hs16
-rw-r--r--System/Syscall/Darwin.hsc58
-rw-r--r--System/Syscall/Linux.hsc91
-rw-r--r--hsyscall.cabal42
6 files changed, 240 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..b916d6b
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright Aycan iRiCAN 2010
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Aycan iRiCAN nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..cd7dc32
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,3 @@
+#!/usr/bin/env runhaskell
+import Distribution.Simple
+main = defaultMain
diff --git a/System/Syscall.hs b/System/Syscall.hs
new file mode 100644
index 0000000..b3a7ecf
--- /dev/null
+++ b/System/Syscall.hs
@@ -0,0 +1,16 @@
+{-# LANGUAGE CPP #-}
+
+module System.Syscall
+ ( c_open, c_close,
+ c_read, c_write,
+ c_fcntl,
+ StructStat(..), c_stat, c_fstat, c_lstat,
+ c_sendfile
+ ) where
+
+#if defined(LINUX)
+import System.Syscall.Linux
+#endif
+#if defined(DARWIN)
+import System.Syscall.Darwin
+#endif
diff --git a/System/Syscall/Darwin.hsc b/System/Syscall/Darwin.hsc
new file mode 100644
index 0000000..011bf3d
--- /dev/null
+++ b/System/Syscall/Darwin.hsc
@@ -0,0 +1,58 @@
+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
+-- | 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
+ )
+ 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
+
+-- | 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
diff --git a/System/Syscall/Linux.hsc b/System/Syscall/Linux.hsc
new file mode 100644
index 0000000..b3e3fd1
--- /dev/null
+++ b/System/Syscall/Linux.hsc
@@ -0,0 +1,91 @@
+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
+-- | 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
+ )
+ 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
+
+-- | 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)
diff --git a/hsyscall.cabal b/hsyscall.cabal
new file mode 100644
index 0000000..ccd0c95
--- /dev/null
+++ b/hsyscall.cabal
@@ -0,0 +1,42 @@
+Name: hsyscall
+Version: 0.1
+Synopsis: FFI to syscalls
+Description: FFI to OS syscalls (open, close, sendfile etc...).
+Homepage: http://sites.google.com/site/iricanaycan/hsyscall
+License: BSD3
+License-file: LICENSE
+Author: Aycan iRiCAN
+Maintainer: aycan.irican@core.gen.tr
+Stability: Experimental
+Category: System
+Build-type: Simple
+-- Build-Depends: base >= 4 && < 5
+
+-- Extra files to be distributed with the package, such as examples or
+-- a README.
+-- Extra-source-files:
+
+-- Constraint on the version of Cabal needed to build this package.
+Cabal-version: >=1.2
+
+Library
+ -- Modules exported by the library.
+ Exposed-modules: System.Syscall
+
+ ghc-options: -Wall
+
+ if os(linux)
+ cpp-options: -DLINUX
+ other-modules: System.Syscall.Linux
+ if os(darwin)
+ cpp-options: -DDARWIN
+ other-modules: System.Syscall.Darwin
+ -- Packages needed in order to build this package.
+ Build-depends: base >= 4 && < 5
+
+ -- Modules not exported by this package.
+ -- Other-modules:
+
+ -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
+ Build-tools: hsc2hs
+