]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/ffi/ffi.factor
512a913b41995e8d3fd058072a09f6dc9fd23324
[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 CONSTANT: F_OK 0 ! test for existence of file
66 CONSTANT: X_OK 1 ! test for execute or search permission
67 CONSTANT: W_OK 2 ! test for write permission
68 CONSTANT: R_OK 4 ! test for read permission
69
70 FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
71 FUNCTION: int access ( c-string path, int amode ) ;
72 FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
73 FUNCTION: int chdir ( c-string path ) ;
74 FUNCTION: int chmod ( c-string path, mode_t mode ) ;
75 FUNCTION: int fchmod ( int fd, mode_t mode ) ;
76 FUNCTION: int chown ( c-string path, uid_t owner, gid_t group ) ;
77 FUNCTION: int chroot ( c-string path ) ;
78 FUNCTION: int close ( int fd ) ;
79 FUNCTION: int closedir ( DIR* dirp ) ;
80 FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
81 FUNCTION: int dup2 ( int oldd, int newd ) ;
82 FUNCTION: void endpwent ( ) ;
83 FUNCTION: int fchdir ( int fd ) ;
84 FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
85 FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
86 FUNCTION: int fileno ( FILE* stream ) ;
87 FUNCTION: int flock ( int fd, int operation ) ;
88 FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
89 FUNCTION: int futimes ( int id, timeval[2] times ) ;
90 FUNCTION: c-string gai_strerror ( int ecode ) ;
91 FUNCTION: int getaddrinfo ( c-string hostname, c-string servname, addrinfo* hints, addrinfo** res ) ;
92 FUNCTION: c-string getcwd ( c-string buf, size_t size ) ;
93 FUNCTION: pid_t getpid ;
94 FUNCTION: int getdtablesize ;
95 FUNCTION: pid_t getpgrp ;
96 FUNCTION: pid_t getpgid ( pid_t pid ) ;
97 FUNCTION: gid_t getegid ;
98 FUNCTION: uid_t geteuid ;
99 FUNCTION: gid_t getgid ;
100 FUNCTION: c-string getenv ( c-string name ) ;
101
102 FUNCTION: int getgrgid_r ( gid_t gid, group* grp, c-string buffer, size_t bufsize, group** result ) ;
103 FUNCTION: int getgrnam_r ( c-string name, group* grp, c-string buffer, size_t bufsize, group** result ) ;
104 FUNCTION: passwd* getpwent ( ) ;
105 FUNCTION: int killpg ( pid_t pgrp, int sig ) ;
106 FUNCTION: void setpwent ( ) ;
107 FUNCTION: void setpassent ( int stayopen ) ;
108 FUNCTION: passwd* getpwuid ( uid_t uid ) ;
109 FUNCTION: passwd* getpwnam ( c-string login ) ;
110 FUNCTION: int getpwnam_r ( c-string login, passwd* pwd, c-string buffer, size_t bufsize, passwd** result ) ;
111 FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
112 FUNCTION: int getgrouplist ( c-string name, int basegid, int* groups, int* ngroups ) ;
113 FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
114 FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;
115 FUNCTION: int getpriority ( int which, id_t who ) ;
116 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
117 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
118 FUNCTION: group* getgrent ;
119 FUNCTION: void endgrent ( ) ;
120 FUNCTION: int gethostname ( c-string name, int len ) ;
121 FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
122 FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
123 FUNCTION: protoent* getprotobyname ( c-string name ) ;
124 FUNCTION: uid_t getuid ;
125 FUNCTION: uint htonl ( uint n ) ;
126 FUNCTION: ushort htons ( ushort n ) ;
127 ! FUNCTION: int issetugid ;
128 FUNCTION: int isatty ( int fildes ) ;
129 FUNCTION: int ioctl ( int fd, ulong request, void* argp ) ;
130 FUNCTION: int lchown ( c-string path, uid_t owner, gid_t group ) ;
131 FUNCTION: int listen ( int s, int backlog ) ;
132 FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
133 FUNCTION: int mkdir ( c-string path, mode_t mode ) ;
134 FUNCTION: int mkfifo ( c-string path, mode_t mode ) ;
135 FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
136 FUNCTION: int munmap ( void* addr, size_t len ) ;
137 FUNCTION: uint ntohl ( uint n ) ;
138 FUNCTION: ushort ntohs ( ushort n ) ;
139 FUNCTION: int shutdown ( int fd, int how ) ;
140 FUNCTION: int open ( c-string path, int flags, int prot ) ;
141 FUNCTION: DIR* opendir ( c-string path ) ;
142
143 STRUCT: utimbuf
144     { actime time_t }
145     { modtime time_t } ;
146
147 FUNCTION: int utime ( c-string path, utimbuf* buf ) ;
148
149 FUNCTION: int pclose ( void* file ) ;
150 FUNCTION: int pipe ( int* filedes ) ;
151 FUNCTION: void* popen ( c-string command, c-string type ) ;
152 FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
153 FUNCTION: ssize_t readv ( int fd, iovec* iov, int iovcnt ) ;
154
155 FUNCTION: dirent* readdir ( DIR* dirp ) ;
156 FUNCTION: int readdir_r ( void* dirp, dirent* entry, dirent** result ) ;
157 FUNCTION: ssize_t readlink ( c-string path, c-string buf, size_t bufsize ) ;
158
159 CONSTANT: PATH_MAX 1024
160
161 FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
162 FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
163 FUNCTION: int rename ( c-string from, c-string to ) ;
164 FUNCTION: int rmdir ( c-string path ) ;
165 FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
166 FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
167
168 FUNCTION: int setenv ( c-string name, c-string value, int overwrite ) ;
169 FUNCTION: int unsetenv ( c-string name ) ;
170 FUNCTION: int setegid ( gid_t egid ) ;
171 FUNCTION: int seteuid ( uid_t euid ) ;
172 FUNCTION: int setgid ( gid_t gid ) ;
173 FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
174 FUNCTION: int setpgid ( pid_t pid, pid_t gid ) ;
175 FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
176 FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
177 FUNCTION: pid_t setsid ( ) ;
178 FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
179 FUNCTION: int setuid ( uid_t uid ) ;
180 FUNCTION: int socket ( int domain, int type, int protocol ) ;
181 FUNCTION: int symlink ( c-string path1, c-string path2 ) ;
182 FUNCTION: int link ( c-string path1, c-string path2 ) ;
183 FUNCTION: int ftruncate ( int fd, int length ) ;
184 FUNCTION: int truncate ( c-string path, int length ) ;
185 FUNCTION: int unlink ( c-string path ) ;
186 FUNCTION: int utimes ( c-string path, timeval[2] times ) ;
187 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
188 FUNCTION: ssize_t writev ( int fds, iovec* iov, int iovcnt ) ;
189 TYPEDEF: void* sighandler_t
190 FUNCTION: sighandler_t signal ( int signum, sighandler_t handler) ;
191
192 "librt" "librt.so" cdecl add-library