]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/ffi/ffi.factor
d5faa8f6d5eaf823ec21c59ddc79fa3b10bde5da
[factor.git] / basis / unix / ffi / ffi.factor
1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.data alien.libraries
4 alien.syntax classes.struct combinators system unix.time
5 unix.types vocabs ;
6 IN: unix.ffi
7
8 <<
9
10 {
11     { [ os linux? ] [ "unix.ffi.linux" require ] }
12     { [ os macosx? ] [ "unix.ffi.macosx" require ] }
13 } cond
14
15 >>
16
17 CONSTANT: PROT_NONE   0
18 CONSTANT: PROT_READ   1
19 CONSTANT: PROT_WRITE  2
20 CONSTANT: PROT_EXEC   4
21
22 CONSTANT: MAP_FILE    0
23 CONSTANT: MAP_SHARED  1
24 CONSTANT: MAP_PRIVATE 2
25
26 CONSTANT: SEEK_SET 0
27 CONSTANT: SEEK_CUR 1
28 CONSTANT: SEEK_END 2
29
30 : MAP_FAILED ( -- alien ) -1 <alien> ; inline
31
32 CONSTANT: DT_UNKNOWN   0
33 CONSTANT: DT_FIFO      1
34 CONSTANT: DT_CHR       2
35 CONSTANT: DT_DIR       4
36 CONSTANT: DT_BLK       6
37 CONSTANT: DT_REG       8
38 CONSTANT: DT_LNK      10
39 CONSTANT: DT_SOCK     12
40 CONSTANT: DT_WHT      14
41
42 : SIG_EFF ( -- obj ) ALIEN: -1 void* <ref> ; inline
43 : SIG_DFL ( -- obj ) ALIEN: 0 void* <ref> ; inline
44 : SIG_IGN ( -- obj ) ALIEN: 1 void* <ref> ; inline
45
46 LIBRARY: libc
47
48 FUNCTION: c-string strerror ( int errno ) ;
49
50 STRUCT: group
51     { gr_name c-string }
52     { gr_passwd c-string }
53     { gr_gid int }
54     { gr_mem c-string* } ;
55
56 STRUCT: protoent
57     { name c-string }
58     { aliases void* }
59     { proto int } ;
60
61 STRUCT: iovec
62     { iov_base void* }
63     { iov_len size_t } ;
64
65 STRUCT: servent
66     { name c-string }
67     { aliases void* }
68     { port int }
69     { proto c-string } ;
70
71 CONSTANT: F_OK 0 ! test for existence of file
72 CONSTANT: X_OK 1 ! test for execute or search permission
73 CONSTANT: W_OK 2 ! test for write permission
74 CONSTANT: R_OK 4 ! test for read permission
75
76 FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
77 FUNCTION: int access ( c-string path, int amode ) ;
78 FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
79 FUNCTION: int chdir ( c-string path ) ;
80 FUNCTION: int chmod ( c-string path, mode_t mode ) ;
81 FUNCTION: int fchmod ( int fd, mode_t mode ) ;
82 FUNCTION: int chown ( c-string path, uid_t owner, gid_t group ) ;
83 FUNCTION: int chroot ( c-string path ) ;
84 FUNCTION: int close ( int fd ) ;
85 FUNCTION: int closedir ( DIR* dirp ) ;
86 FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
87 FUNCTION: int dup2 ( int oldd, int newd ) ;
88 FUNCTION: void endpwent ( ) ;
89 FUNCTION: int fchdir ( int fd ) ;
90 FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
91 FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
92 FUNCTION: int fileno ( FILE* stream ) ;
93 FUNCTION: int flock ( int fd, int operation ) ;
94 FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
95 FUNCTION: int futimes ( int id, timeval[2] times ) ;
96 FUNCTION: c-string gai_strerror ( int ecode ) ;
97 FUNCTION: int getaddrinfo ( c-string hostname, c-string servname, addrinfo* hints, addrinfo** res ) ;
98 FUNCTION: c-string getcwd ( c-string buf, size_t size ) ;
99 FUNCTION: pid_t getpid ;
100 FUNCTION: int getdtablesize ;
101 FUNCTION: pid_t getpgrp ;
102 FUNCTION: pid_t getpgid ( pid_t pid ) ;
103 FUNCTION: gid_t getegid ;
104 FUNCTION: uid_t geteuid ;
105 FUNCTION: gid_t getgid ;
106 FUNCTION: c-string getenv ( c-string name ) ;
107
108 FUNCTION: int getgrgid_r ( gid_t gid, group* grp, c-string buffer, size_t bufsize, group** result ) ;
109 FUNCTION: int getgrnam_r ( c-string name, group* grp, c-string buffer, size_t bufsize, group** result ) ;
110 FUNCTION: passwd* getpwent ( ) ;
111 FUNCTION: int killpg ( pid_t pgrp, int sig ) ;
112 FUNCTION: void setpwent ( ) ;
113 FUNCTION: void setpassent ( int stayopen ) ;
114 FUNCTION: passwd* getpwuid ( uid_t uid ) ;
115 FUNCTION: passwd* getpwnam ( c-string login ) ;
116 FUNCTION: int getpwnam_r ( c-string login, passwd* pwd, c-string buffer, size_t bufsize, passwd** result ) ;
117 FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
118 FUNCTION: int getgrouplist ( c-string name, int basegid, int* groups, int* ngroups ) ;
119 FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
120 FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;
121 FUNCTION: int getpriority ( int which, id_t who ) ;
122 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
123 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
124 FUNCTION: group* getgrent ;
125 FUNCTION: void endgrent ( ) ;
126 FUNCTION: int gethostname ( c-string name, int len ) ;
127 FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
128 FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
129 FUNCTION: protoent* getprotobyname ( c-string name ) ;
130 FUNCTION: servent* getservbyname ( c-string name, c-string prot ) ;
131 FUNCTION: servent* getservbyport ( int port, c-string prot ) ;
132 FUNCTION: uid_t getuid ;
133 FUNCTION: uint htonl ( uint n ) ;
134 FUNCTION: ushort htons ( ushort n ) ;
135 ! FUNCTION: int issetugid ;
136 FUNCTION: int isatty ( int fildes ) ;
137 FUNCTION: int ioctl ( int fd, ulong request, void* argp ) ;
138 FUNCTION: int lchown ( c-string path, uid_t owner, gid_t group ) ;
139 FUNCTION: int listen ( int s, int backlog ) ;
140 FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
141 FUNCTION: int mkdir ( c-string path, mode_t mode ) ;
142 FUNCTION: int mkfifo ( c-string path, mode_t mode ) ;
143 FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
144 FUNCTION: int munmap ( void* addr, size_t len ) ;
145 FUNCTION: uint ntohl ( uint n ) ;
146 FUNCTION: ushort ntohs ( ushort n ) ;
147 FUNCTION: int shutdown ( int fd, int how ) ;
148 FUNCTION: int open ( c-string path, int flags, int prot ) ;
149 FUNCTION: DIR* opendir ( c-string path ) ;
150
151 STRUCT: utimbuf
152     { actime time_t }
153     { modtime time_t } ;
154
155 FUNCTION: int utime ( c-string path, utimbuf* buf ) ;
156
157 FUNCTION: int pclose ( void* file ) ;
158 FUNCTION: int pipe ( int* filedes ) ;
159 FUNCTION: void* popen ( c-string command, c-string type ) ;
160 FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
161 FUNCTION: ssize_t readv ( int fd, iovec* iov, int iovcnt ) ;
162
163 FUNCTION: dirent* readdir ( DIR* dirp ) ;
164 FUNCTION: int readdir_r ( void* dirp, dirent* entry, dirent** result ) ;
165 FUNCTION: ssize_t readlink ( c-string path, c-string buf, size_t bufsize ) ;
166
167 CONSTANT: PATH_MAX 1024
168
169 FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
170 FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
171 FUNCTION: int rename ( c-string from, c-string to ) ;
172 FUNCTION: int rmdir ( c-string path ) ;
173 FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
174 FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
175
176 FUNCTION: int setenv ( c-string name, c-string value, int overwrite ) ;
177 FUNCTION: int unsetenv ( c-string name ) ;
178 FUNCTION: int setegid ( gid_t egid ) ;
179 FUNCTION: int seteuid ( uid_t euid ) ;
180 FUNCTION: int setgid ( gid_t gid ) ;
181 FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
182 FUNCTION: int setpgid ( pid_t pid, pid_t gid ) ;
183 FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
184 FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
185 FUNCTION: pid_t setsid ( ) ;
186 FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
187 FUNCTION: int setuid ( uid_t uid ) ;
188 FUNCTION: int socket ( int domain, int type, int protocol ) ;
189 FUNCTION: int symlink ( c-string path1, c-string path2 ) ;
190 FUNCTION: int link ( c-string path1, c-string path2 ) ;
191 FUNCTION: int ftruncate ( int fd, int length ) ;
192 FUNCTION: int truncate ( c-string path, int length ) ;
193 FUNCTION: int unlink ( c-string path ) ;
194 FUNCTION: int utimes ( c-string path, timeval[2] times ) ;
195 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
196 FUNCTION: ssize_t writev ( int fds, iovec* iov, int iovcnt ) ;
197 TYPEDEF: void* sighandler_t
198 FUNCTION: sighandler_t signal ( int signum, sighandler_t handler) ;
199
200 "librt" "librt.so" cdecl add-library