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 vocabs.loader ;
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 { "char*" "gr_passwd" }
42 { "char**" "gr_mem" } ;
46 FUNCTION: char* strerror ( int errno ) ;
48 ERROR: unix-error errno message ;
50 : (io-error) ( -- * ) errno dup strerror unix-error ;
52 : io-error ( n -- ) 0 < [ (io-error) ] when ;
54 ERROR: unix-system-call-error args errno message word ;
56 MACRO:: unix-system-call ( quot -- )
57 [let | n [ quot infer in>> ]
60 n ndup quot call dup 0 < [
64 word unix-system-call-error
71 FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
72 FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
73 FUNCTION: int chdir ( char* path ) ;
74 FUNCTION: int chmod ( char* path, mode_t mode ) ;
75 FUNCTION: int fchmod ( int fd, mode_t mode ) ;
76 FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
77 FUNCTION: int chroot ( char* path ) ;
79 FUNCTION: int close ( int fd ) ;
80 FUNCTION: int closedir ( DIR* dirp ) ;
82 : close-file ( fd -- ) [ close ] unix-system-call drop ;
84 FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
85 FUNCTION: int dup2 ( int oldd, int newd ) ;
86 ! FUNCTION: int dup ( int oldd ) ;
87 : _exit ( status -- * )
88 #! We throw to give this a terminating stack effect.
89 "int" f "_exit" { "int" } alien-invoke "Exit failed" throw ;
90 FUNCTION: void endpwent ( ) ;
91 FUNCTION: int fchdir ( int fd ) ;
92 FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
93 FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
94 FUNCTION: int flock ( int fd, int operation ) ;
95 FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
96 FUNCTION: int futimes ( int id, timeval[2] times ) ;
97 FUNCTION: char* gai_strerror ( int ecode ) ;
98 FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
99 FUNCTION: char* getcwd ( char* buf, size_t size ) ;
100 FUNCTION: pid_t getpid ;
101 FUNCTION: int getdtablesize ;
102 FUNCTION: gid_t getegid ;
103 FUNCTION: uid_t geteuid ;
104 FUNCTION: gid_t getgid ;
105 FUNCTION: char* getenv ( char* name ) ;
107 FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
108 FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
109 FUNCTION: passwd* getpwent ( ) ;
110 FUNCTION: passwd* getpwuid ( uid_t uid ) ;
111 FUNCTION: passwd* getpwnam ( char* login ) ;
112 FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
113 FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
114 FUNCTION: int getgrouplist ( char* name, int basegid, int* groups, int* ngroups ) ;
115 FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
116 FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;
118 FUNCTION: int getpriority ( int which, id_t who ) ;
119 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
121 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
123 FUNCTION: group* getgrent ;
124 FUNCTION: int gethostname ( char* name, int len ) ;
125 FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
126 FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
127 FUNCTION: uid_t getuid ;
128 FUNCTION: uint htonl ( uint n ) ;
129 FUNCTION: ushort htons ( ushort n ) ;
130 ! FUNCTION: int issetugid ;
131 FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
132 FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
133 FUNCTION: int listen ( int s, int backlog ) ;
134 FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
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 ) ;
141 FUNCTION: int open ( char* path, int flags, int prot ) ;
143 FUNCTION: DIR* opendir ( char* path ) ;
145 : open-file ( path flags mode -- fd ) [ open ] unix-system-call ;
148 { "time_t" "actime" }
149 { "time_t" "modtime" } ;
151 FUNCTION: int utime ( char* path, utimebuf* buf ) ;
153 : touch ( filename -- ) f [ utime ] unix-system-call drop ;
155 : change-file-times ( filename access modification -- )
156 "utimebuf" <c-object>
157 [ set-utimbuf-modtime ] keep
158 [ set-utimbuf-actime ] keep
159 [ utime ] unix-system-call drop ;
161 FUNCTION: int pclose ( void* file ) ;
162 FUNCTION: int pipe ( int* filedes ) ;
163 FUNCTION: void* popen ( char* command, char* type ) ;
164 FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
166 FUNCTION: dirent* readdir ( DIR* dirp ) ;
167 FUNCTION: int readdir_r ( void* dirp, dirent* entry, dirent** result ) ;
169 FUNCTION: ssize_t readlink ( char* path, char* buf, size_t bufsize ) ;
171 CONSTANT: PATH_MAX 1024
173 : read-symbolic-link ( path -- path )
174 PATH_MAX <byte-array> dup [
176 [ readlink ] unix-system-call
177 ] dip swap head-slice >string ;
179 FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
180 FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
181 FUNCTION: int rename ( char* from, char* to ) ;
182 FUNCTION: int rmdir ( char* path ) ;
183 FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
184 FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
185 FUNCTION: int setenv ( char* name, char* value, int overwrite ) ;
186 FUNCTION: int unsetenv ( char* name ) ;
187 FUNCTION: int setegid ( gid_t egid ) ;
188 FUNCTION: int seteuid ( uid_t euid ) ;
189 FUNCTION: int setgid ( gid_t gid ) ;
190 FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
191 FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
192 FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
193 FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
194 FUNCTION: int setuid ( uid_t uid ) ;
195 FUNCTION: int socket ( int domain, int type, int protocol ) ;
196 FUNCTION: int symlink ( char* path1, char* path2 ) ;
197 FUNCTION: int link ( char* path1, char* path2 ) ;
198 FUNCTION: int system ( char* command ) ;
200 FUNCTION: int unlink ( char* path ) ;
202 : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
204 FUNCTION: int utimes ( char* path, timeval[2] times ) ;
206 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
209 { [ os linux? ] [ "unix.linux" require ] }
210 { [ os bsd? ] [ "unix.bsd" require ] }
211 { [ os solaris? ] [ "unix.solaris" require ] }
215 "unix.debugger" require