1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! Copyright (C) 2008 Eduardo Cavazos.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: alien alien.c-types alien.syntax kernel libc
5 sequences continuations byte-arrays strings math namespaces
6 system combinators vocabs.loader accessors
7 stack-checker macros locals generalizations unix.types
8 io vocabs classes.struct unix.time alien.libraries ;
13 CONSTANT: PROT_WRITE 2
17 CONSTANT: MAP_SHARED 1
18 CONSTANT: MAP_PRIVATE 2
24 : MAP_FAILED ( -- alien ) -1 <alien> ; inline
26 CONSTANT: NGROUPS_MAX 16
28 CONSTANT: DT_UNKNOWN 0
40 FUNCTION: char* strerror ( int errno ) ;
42 ERROR: unix-error errno message ;
44 : (io-error) ( -- * ) errno dup strerror unix-error ;
46 : io-error ( n -- ) 0 < [ (io-error) ] when ;
48 ERROR: unix-system-call-error args errno message word ;
50 MACRO:: unix-system-call ( quot -- )
54 n ndup quot call dup 0 < [
58 word unix-system-call-error
64 HOOK: open-file os ( path flags mode -- fd )
69 { [ os linux? ] [ "unix.linux" require ] }
70 { [ os bsd? ] [ "unix.bsd" require ] }
71 { [ os solaris? ] [ "unix.solaris" require ] }
75 "unix.debugger" require
86 FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
87 FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
88 FUNCTION: int chdir ( char* path ) ;
89 FUNCTION: int chmod ( char* path, mode_t mode ) ;
90 FUNCTION: int fchmod ( int fd, mode_t mode ) ;
91 FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
92 FUNCTION: int chroot ( char* path ) ;
94 FUNCTION: int close ( int fd ) ;
95 FUNCTION: int closedir ( DIR* dirp ) ;
97 : close-file ( fd -- ) [ close ] unix-system-call drop ;
99 FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
100 FUNCTION: int dup2 ( int oldd, int newd ) ;
101 ! FUNCTION: int dup ( int oldd ) ;
102 : _exit ( status -- * )
103 #! We throw to give this a terminating stack effect.
104 int f "_exit" { int } alien-invoke "Exit failed" throw ;
105 FUNCTION: void endpwent ( ) ;
106 FUNCTION: int fchdir ( int fd ) ;
107 FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
108 FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
109 FUNCTION: int flock ( int fd, int operation ) ;
110 FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
111 FUNCTION: int futimes ( int id, timeval[2] times ) ;
112 FUNCTION: char* gai_strerror ( int ecode ) ;
113 FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
114 FUNCTION: char* getcwd ( char* buf, size_t size ) ;
115 FUNCTION: pid_t getpid ;
116 FUNCTION: int getdtablesize ;
117 FUNCTION: gid_t getegid ;
118 FUNCTION: uid_t geteuid ;
119 FUNCTION: gid_t getgid ;
120 FUNCTION: char* getenv ( char* name ) ;
122 FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
123 FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
124 FUNCTION: passwd* getpwent ( ) ;
125 FUNCTION: passwd* getpwuid ( uid_t uid ) ;
126 FUNCTION: passwd* getpwnam ( char* login ) ;
127 FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
128 FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
129 FUNCTION: int getgrouplist ( char* name, int basegid, int* groups, int* ngroups ) ;
130 FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
131 FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;
133 FUNCTION: int getpriority ( int which, id_t who ) ;
134 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
136 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
138 FUNCTION: group* getgrent ;
139 FUNCTION: int gethostname ( char* name, int len ) ;
140 FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
141 FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
142 FUNCTION: uid_t getuid ;
143 FUNCTION: uint htonl ( uint n ) ;
144 FUNCTION: ushort htons ( ushort n ) ;
145 ! FUNCTION: int issetugid ;
146 FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
147 FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
148 FUNCTION: int listen ( int s, int backlog ) ;
149 FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
150 FUNCTION: int mkdir ( char* path, mode_t mode ) ;
151 FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
152 FUNCTION: int munmap ( void* addr, size_t len ) ;
153 FUNCTION: uint ntohl ( uint n ) ;
154 FUNCTION: ushort ntohs ( ushort n ) ;
155 FUNCTION: int shutdown ( int fd, int how ) ;
157 FUNCTION: int open ( char* path, int flags, int prot ) ;
159 M: unix open-file [ open ] unix-system-call ;
161 FUNCTION: DIR* opendir ( char* path ) ;
167 FUNCTION: int utime ( char* path, utimbuf* buf ) ;
169 : touch ( filename -- ) f [ utime ] unix-system-call drop ;
171 : change-file-times ( filename access modification -- )
175 [ utime ] unix-system-call drop ;
177 FUNCTION: int pclose ( void* file ) ;
178 FUNCTION: int pipe ( int* filedes ) ;
179 FUNCTION: void* popen ( char* command, char* type ) ;
180 FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
182 FUNCTION: dirent* readdir ( DIR* dirp ) ;
183 FUNCTION: int readdir_r ( void* dirp, dirent* entry, dirent** result ) ;
184 FUNCTION: ssize_t readlink ( char* path, char* buf, size_t bufsize ) ;
186 CONSTANT: PATH_MAX 1024
188 : read-symbolic-link ( path -- path )
189 PATH_MAX <byte-array> dup [
191 [ readlink ] unix-system-call
192 ] dip swap head-slice >string ;
194 FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
195 FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
196 FUNCTION: int rename ( char* from, char* to ) ;
197 FUNCTION: int rmdir ( char* path ) ;
198 FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
199 FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
200 FUNCTION: int setenv ( char* name, char* value, int overwrite ) ;
201 FUNCTION: int unsetenv ( char* name ) ;
202 FUNCTION: int setegid ( gid_t egid ) ;
203 FUNCTION: int seteuid ( uid_t euid ) ;
204 FUNCTION: int setgid ( gid_t gid ) ;
205 FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
206 FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
207 FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
208 FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
209 FUNCTION: int setuid ( uid_t uid ) ;
210 FUNCTION: int socket ( int domain, int type, int protocol ) ;
211 FUNCTION: int symlink ( char* path1, char* path2 ) ;
212 FUNCTION: int link ( char* path1, char* path2 ) ;
213 FUNCTION: int system ( char* command ) ;
215 FUNCTION: int unlink ( char* path ) ;
217 : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
219 FUNCTION: int utimes ( char* path, timeval[2] times ) ;
221 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
223 "librt" "librt.so" "cdecl" add-library