]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/unix.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / unix / unix.factor
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 ;
9 IN: unix
10
11 CONSTANT: PROT_NONE   0
12 CONSTANT: PROT_READ   1
13 CONSTANT: PROT_WRITE  2
14 CONSTANT: PROT_EXEC   4
15                        
16 CONSTANT: MAP_FILE    0
17 CONSTANT: MAP_SHARED  1
18 CONSTANT: MAP_PRIVATE 2
19
20 CONSTANT: SEEK_SET 0
21 CONSTANT: SEEK_CUR 1
22 CONSTANT: SEEK_END 2
23
24 : MAP_FAILED ( -- alien ) -1 <alien> ; inline
25
26 CONSTANT: NGROUPS_MAX 16
27
28 CONSTANT: DT_UNKNOWN   0
29 CONSTANT: DT_FIFO      1
30 CONSTANT: DT_CHR       2
31 CONSTANT: DT_DIR       4
32 CONSTANT: DT_BLK       6
33 CONSTANT: DT_REG       8
34 CONSTANT: DT_LNK      10
35 CONSTANT: DT_SOCK     12
36 CONSTANT: DT_WHT      14
37
38 LIBRARY: libc
39
40 FUNCTION: char* strerror ( int errno ) ;
41
42 ERROR: unix-error errno message ;
43
44 : (io-error) ( -- * ) errno dup strerror unix-error ;
45
46 : io-error ( n -- ) 0 < [ (io-error) ] when ;
47
48 ERROR: unix-system-call-error args errno message word ;
49
50 MACRO:: unix-system-call ( quot -- )
51     quot infer in>> :> n
52     quot first :> word
53     [
54         n ndup quot call dup 0 < [
55             drop
56             n narray
57             errno dup strerror
58             word unix-system-call-error
59         ] [
60             n nnip
61         ] if
62     ] ;
63
64 HOOK: open-file os ( path flags mode -- fd )
65
66 <<
67
68 {
69     { [ os linux? ] [ "unix.linux" require ] }
70     { [ os bsd? ] [ "unix.bsd" require ] }
71     { [ os solaris? ] [ "unix.solaris" require ] }
72 } cond
73
74 "debugger" vocab [
75     "unix.debugger" require
76 ] when
77
78 >>
79
80 STRUCT: group
81     { gr_name char* }
82     { gr_passwd char* }
83     { gr_gid int }
84     { gr_mem char** } ;
85
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 ) ;
93
94 FUNCTION: int close ( int fd ) ;
95 FUNCTION: int closedir ( DIR* dirp ) ;
96
97 : close-file ( fd -- ) [ close ] unix-system-call drop ;
98
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 ) ;
121
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 ) ;
132
133 FUNCTION: int getpriority ( int which, id_t who ) ;
134 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
135
136 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
137
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 ) ;
156
157 FUNCTION: int open ( char* path, int flags, int prot ) ;
158
159 M: unix open-file [ open ] unix-system-call ;
160
161 FUNCTION: DIR* opendir ( char* path ) ;
162
163 STRUCT: utimbuf
164     { actime time_t }
165     { modtime time_t } ;
166
167 FUNCTION: int utime ( char* path, utimbuf* buf ) ;
168
169 : touch ( filename -- ) f [ utime ] unix-system-call drop ;
170
171 : change-file-times ( filename access modification -- )
172     utimbuf <struct>
173         swap >>modtime
174         swap >>actime
175         [ utime ] unix-system-call drop ;
176
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 ) ;
181
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 ) ;
185
186 CONSTANT: PATH_MAX 1024
187
188 : read-symbolic-link ( path -- path )
189     PATH_MAX <byte-array> dup [
190         PATH_MAX
191         [ readlink ] unix-system-call
192     ] dip swap head-slice >string ;
193
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 ) ;
214
215 FUNCTION: int unlink ( char* path ) ;
216
217 : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
218
219 FUNCTION: int utimes ( char* path, timeval[2] times ) ;
220
221 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
222