_Posix.icl 4.76 KB
Newer Older
1
implementation module System._Posix
2

3
import System._Pointer, System.Time
4
import StdInt
5

6
errno :: !*w -> (!Int,!*w)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
errno world = (getErrno,world)
where
	getErrno :: Int
	getErrno = readInt4S errnoAddr 0
	
	errnoAddr :: Pointer
	errnoAddr = code {
		ccall __error ":p"
	}

strerr :: !Int -> Pointer
strerr world = code {
	ccall strerror "I:p"
}

22
stat :: !{#Char} !{#Char} !*w -> (!Int,!*w)
Bas Lijnse's avatar
Bas Lijnse committed
23
stat path buf world = code {
24
	ccall stat$INODE64 "ss:I:A"
25 26
}

27
unlink :: !{#Char} !*w -> (!Int,!*w)
Bas Lijnse's avatar
Bas Lijnse committed
28 29 30
unlink path world = code {
	ccall unlink "s:I:A"
}
31
fork :: !*w -> (!Int,!*w)
Bas Lijnse's avatar
Bas Lijnse committed
32 33 34
fork world = code {
	ccall fork ":I:A"
}
35
execvp :: !{#Char} !{#Pointer} !*w -> (!Int,!*w)
Bas Lijnse's avatar
Bas Lijnse committed
36 37 38
execvp name argv world = code {
	ccall execvp "sA:I:A"
}
39
waitpid :: !Int !{#Int} !Int !*w -> (!Int,!*w)
Bas Lijnse's avatar
Bas Lijnse committed
40 41 42
waitpid pid status_p options world = code {
    ccall waitpid "IAI:I:A"
}
43
exit :: !Int !*w -> (!.a,!*w)
Bas Lijnse's avatar
Bas Lijnse committed
44 45 46
exit num world = code {
	ccall exit "I:p:A"
}
47
getcwd :: !{#Char} !Int !*w -> (!Pointer,!*w)
48 49 50
getcwd buf size_t world = code {
	ccall getcwd "sI:p:A"
}
51
chdir :: !{#Char} !*w -> (!Int,!*w)
52 53 54
chdir name world = code {
	ccall chdir "s:I:A"
}
55
mkdir :: !{#Char} !Int !*w -> (!Int,!*w)
56 57 58
mkdir name mode world = code {
	ccall mkdir "sI:I:A"
}
59
rmdir :: !{#Char} !*w -> (!Int,!*w)
60 61 62
rmdir name world = code {
	ccall rmdir "s:I:A"
}
63
rename :: !{#Char} !{#Char} !*w -> (!Int,!*w)
64 65 66
rename old new world = code {
	ccall rename "ss:I:A"
}
67
opendir	:: !{#Char} !*w -> (!Pointer,!*w)
68 69 70
opendir path world = code {
	ccall opendir "s:p:A"
}
71
closedir :: !Pointer !*w -> (!Int,!*w)
72 73 74
closedir dir world = code {
	ccall closedir "p:I:A"
}
75
readdir	:: !Pointer !*w -> (!Pointer,!*w)
76 77 78
readdir dir world = code {
	ccall readdir "p:p:A"
}
79 80 81 82
pipe :: !Pointer !*w -> (!Int, !*w)
pipe arr world = code {
    ccall pipe "p:I:A"
}
83 84 85 86
posix_openpt :: !Int !*w -> (!Int, !*w)
posix_openpt flags w = code {
	ccall posix_openpt "I:I:A"
}
Mart Lubbers's avatar
Mart Lubbers committed
87
grantpt     :: !Int !*w -> (!Int, !*w)
88 89 90
grantpt fp w = code {
	ccall grantpt "I:I:A"
}
Mart Lubbers's avatar
Mart Lubbers committed
91
unlockpt    :: !Int !*w -> (!Int, !*w)
92 93 94
unlockpt fp w = code {
	ccall unlockpt "I:I:A"
}
Mart Lubbers's avatar
Mart Lubbers committed
95
ptsname     :: !Int !*w -> (!Pointer, !*w)
96 97 98 99 100 101 102
ptsname fp w = code {
	ccall ptsname "I:p:A"
}
open        :: !Pointer !Int !*w -> (!Int, !*w)
open p flags w = code {
	ccall open "pI:I:A"
}
Mart Lubbers's avatar
Mart Lubbers committed
103 104 105 106
tcgetattr   :: !Int !Pointer !*w -> (!Int, !*w)
tcgetattr fp f w = code {
	ccall tcgetattr "Ip:I:A"
}
107
cfmakeraw   :: !Pointer !*w -> *w
Mart Lubbers's avatar
Mart Lubbers committed
108 109 110 111
cfmakeraw p w = code {
	ccall cfmakeraw "p:V:A"
}
tcsetattr   :: !Int !Int !Pointer !*w -> (!Int, !*w)
112
tcsetattr fp strategy p w = code {
Mart Lubbers's avatar
Mart Lubbers committed
113 114
	ccall tcsetattr "IIp:I:A"
}
115 116 117 118
setsid :: !*w -> *w
setsid w = code {
	ccall setsid ":V:A"
}
119 120 121 122 123 124 125 126 127
dup2 :: !Int !Int !*w -> (!Int, !*w)
dup2 old new world = code {
    ccall dup2 "II:I:A"
}

close :: !Int !*w -> (!Int, !*w)
close fd world = code {
    ccall close "I:I:A"
}
Bas Lijnse's avatar
Bas Lijnse committed
128

129 130 131 132 133
ioctl :: !Int !Int !Pointer !*w -> (!Int, !*w)
ioctl fd op ptr world = code {
    ccall ioctl "IIp:I:A"
}

Steffen Michels's avatar
Steffen Michels committed
134 135 136 137 138
fcntlArg :: !Int !Int !Int !*w -> (!Int, !*w)
fcntlArg fd op arg world = code {
    ccall fcntl "III:I:A"
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
read :: !Int !Pointer !Int !*w -> (!Int, !*w)
read fd buffer nBuffer world = code {
    ccall read "IpI:I:A"
}

write :: !Int !{#Char} !Int !*w -> (!Int, !*w)
write fd buffer nBuffer world = code {
    ccall write "IsI:I:A"
}

select_ :: !Int !Pointer !Pointer !Pointer !Pointer !*w -> (!Int, !*w)
select_ nfds readfds writefds exceptfds timeout world = code {
    ccall select "Ipppp:I:A"
}

154 155 156 157 158
kill :: !Int !Int !*w -> (!Int, !*w)
kill pid sig world = code {
    ccall kill "II:I:A"
}

Steffen Michels's avatar
Steffen Michels committed
159 160 161 162
timegm :: !{#Int} -> Int
timegm tm = code {
	ccall timegm "A:I"
}
163

Bas Lijnse's avatar
Bas Lijnse committed
164 165
malloc :: !Int -> Pointer
malloc num = code {
Mart Lubbers's avatar
Mart Lubbers committed
166
	ccall malloc "I:p"
Bas Lijnse's avatar
Bas Lijnse committed
167
}
168 169 170 171
mallocSt	:: !Int !*w -> (!Pointer, !*w)
mallocSt num w = code {
	ccall malloc "I:p:A"
}
Bas Lijnse's avatar
Bas Lijnse committed
172 173 174 175
free :: !Pointer -> Int 
free ptr = code {
	ccall free "p:I"
}
176 177 178 179
freeSt :: !Pointer !*w -> *w
freeSt ptr world = code {
   ccall free "p:V:A"
}
Bas Lijnse's avatar
Bas Lijnse committed
180 181 182
memcpy_string_to_pointer :: !Pointer !{#Char} !Int -> Pointer
memcpy_string_to_pointer p s n = code {
    ccall memcpy "psp:p"
183
}
184 185 186 187
clock_gettime :: !Int !Pointer !*w -> (!Int, !*w)
clock_gettime _ _ _ = code {
	ccall clock_gettime "Ip:I:A"
}
188 189 190 191
nanosleep :: !Pointer !Pointer !*w -> (!Int, !*w)
nanosleep _ _ _ = code {
	ccall nanosleep "pp:I:A"
}
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

//Mapping to/from byte arrays
unpackStat :: !{#Char} -> Stat
unpackStat s =
    { st_dev			= unpackInt4S s 0
    , st_ino			= unpackInt8  s 8
    , st_mode			= unpackInt2S s 4 
    , st_nlink			= unpackInt2S s 6 
    , st_uid			= unpackInt4S s 16
    , st_gid			= unpackInt4S s 20
	, st_rdev			= unpackInt8  s 24 
    , st_atimespec  	= unpackInt8  s 32
    , st_mtimespec  	= unpackInt8  s 48
    , st_ctimespec  	= unpackInt8  s 64 
	, st_birthtimespec	= unpackInt8  s 80
    , st_size       	= unpackInt8  s 96 
    , st_blocks    		= unpackInt8  s 104 
    , st_blksize    	= unpackInt4S s 112
    , st_flags      	= unpackInt4S s 116
    , st_gen        	= unpackInt4S s 120
    }

sizeOfStat :: Int
sizeOfStat = 144