]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/unix.factor
Merge branch 'master' of git://repo.or.cz/factor/jcg
[factor.git] / basis / unix / unix.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.syntax kernel libc
4 sequences continuations byte-arrays strings math namespaces
5 system combinators vocabs.loader qualified accessors
6 stack-checker macros locals generalizations unix.types
7 io io.files vocabs vocabs.loader ;
8 IN: unix
9
10 : PROT_NONE   0 ; inline
11 : PROT_READ   1 ; inline
12 : PROT_WRITE  2 ; inline
13 : PROT_EXEC   4 ; inline
14
15 : MAP_FILE    0 ; inline
16 : MAP_SHARED  1 ; inline
17 : MAP_PRIVATE 2 ; inline
18
19 : MAP_FAILED -1 <alien> ; inline
20
21 : NGROUPS_MAX 16 ; inline
22
23 : DT_UNKNOWN   0 ; inline
24 : DT_FIFO      1 ; inline
25 : DT_CHR       2 ; inline
26 : DT_DIR       4 ; inline
27 : DT_BLK       6 ; inline
28 : DT_REG       8 ; inline
29 : DT_LNK      10 ; inline
30 : DT_SOCK     12 ; inline
31 : DT_WHT      14 ; inline
32
33 : dirent-type>file-type ( ch -- type )
34     {
35         { DT_BLK  [ +block-device+ ] }
36         { DT_CHR  [ +character-device+ ] }
37         { DT_DIR  [ +directory+ ] }
38         { DT_LNK  [ +symbolic-link+ ] }
39         { DT_SOCK [ +socket+ ] }
40         { DT_FIFO [ +fifo+ ] }
41         { DT_REG  [ +regular-file+ ] }
42         { DT_WHT  [ +whiteout+ ] }
43         [ drop +unknown+ ]
44     } case ;
45
46 C-STRUCT: group
47     { "char*" "gr_name" }
48     { "char*" "gr_passwd" }
49     { "int" "gr_gid" }
50     { "char**" "gr_mem" } ;
51
52 LIBRARY: factor
53
54 FUNCTION: void clear_err_no ( ) ;
55 FUNCTION: int err_no ( ) ;
56
57 LIBRARY: libc
58
59 FUNCTION: char* strerror ( int errno ) ;
60
61 ERROR: unix-error errno message ;
62
63 : (io-error) ( -- * ) err_no dup strerror unix-error ;
64
65 : io-error ( n -- ) 0 < [ (io-error) ] when ;
66
67 ERROR: unix-system-call-error args errno message word ;
68
69 MACRO:: unix-system-call ( quot -- )
70     [let | n [ quot infer in>> ]
71            word [ quot first ] |
72         [
73             n ndup quot call dup 0 < [
74                 drop
75                 n narray
76                 err_no dup strerror
77                 word unix-system-call-error
78             ] [
79                 n nnip
80             ] if
81         ]
82     ] ;
83
84 FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
85 FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
86 FUNCTION: int chdir ( char* path ) ;
87 FUNCTION: int chmod ( char* path, mode_t mode ) ;
88 FUNCTION: int fchmod ( int fd, mode_t mode ) ;
89 FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
90 FUNCTION: int chroot ( char* path ) ;
91
92 FUNCTION: int close ( int fd ) ;
93 FUNCTION: int closedir ( DIR* dirp ) ;
94
95 : close-file ( fd -- ) [ close ] unix-system-call drop ;
96
97 FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
98 FUNCTION: int dup2 ( int oldd, int newd ) ;
99 ! FUNCTION: int dup ( int oldd ) ;
100 : _exit ( status -- * )
101     #! We throw to give this a terminating stack effect.
102     "int" f "_exit" { "int" } alien-invoke "Exit failed" throw ;
103 FUNCTION: void endpwent ( ) ;
104 FUNCTION: int fchdir ( int fd ) ;
105 FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
106 FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
107 FUNCTION: int flock ( int fd, int operation ) ;
108 FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
109 FUNCTION: int futimes ( int id, timeval[2] times ) ;
110 FUNCTION: char* gai_strerror ( int ecode ) ;
111 FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
112 FUNCTION: char* getcwd ( char* buf, size_t size ) ;
113 FUNCTION: pid_t getpid ;
114 FUNCTION: int getdtablesize ;
115 FUNCTION: gid_t getegid ;
116 FUNCTION: uid_t geteuid ;
117 FUNCTION: gid_t getgid ;
118 FUNCTION: char* getenv ( char* name ) ;
119
120 FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
121 FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
122 FUNCTION: passwd* getpwent ( ) ;
123 FUNCTION: passwd* getpwuid ( uid_t uid ) ;
124 FUNCTION: passwd* getpwnam ( char* login ) ;
125 FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
126 FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
127 FUNCTION: int getgrouplist ( char* name, int basegid, int* groups, int* ngroups ) ;
128 FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
129 FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;
130
131 FUNCTION: int getpriority ( int which, id_t who ) ;
132 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
133
134 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
135
136 FUNCTION: group* getgrent ;
137 FUNCTION: int gethostname ( char* name, int len ) ;
138 FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
139 FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
140 FUNCTION: uid_t getuid ;
141 FUNCTION: uint htonl ( uint n ) ;
142 FUNCTION: ushort htons ( ushort n ) ;
143 ! FUNCTION: int issetugid ;
144 FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
145 FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
146 FUNCTION: int listen ( int s, int backlog ) ;
147 FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
148 FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
149 FUNCTION: int munmap ( void* addr, size_t len ) ;
150 FUNCTION: uint ntohl ( uint n ) ;
151 FUNCTION: ushort ntohs ( ushort n ) ;
152 FUNCTION: int shutdown ( int fd, int how ) ;
153
154 FUNCTION: int open ( char* path, int flags, int prot ) ;
155
156 FUNCTION: DIR* opendir ( char* path ) ;
157
158 : open-file ( path flags mode -- fd ) [ open ] unix-system-call ;
159
160 C-STRUCT: utimbuf
161     { "time_t" "actime"  }
162     { "time_t" "modtime" } ;
163
164 FUNCTION: int utime ( char* path, utimebuf* buf ) ;
165
166 : touch ( filename -- ) f [ utime ] unix-system-call drop ;
167
168 : change-file-times ( filename access modification -- )
169     "utimebuf" <c-object>
170     tuck set-utimbuf-modtime
171     tuck set-utimbuf-actime
172     [ utime ] unix-system-call drop ;
173
174 FUNCTION: int pclose ( void* file ) ;
175 FUNCTION: int pipe ( int* filedes ) ;
176 FUNCTION: void* popen ( char* command, char* type ) ;
177 FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
178
179 FUNCTION: dirent* readdir ( DIR* dirp ) ;
180 FUNCTION: int readdir_r ( void* dirp, dirent* entry, dirent** result ) ;
181
182 FUNCTION: ssize_t readlink ( char* path, char* buf, size_t bufsize ) ;
183
184 : PATH_MAX 1024 ; inline
185
186 : read-symbolic-link ( path -- path )
187     PATH_MAX <byte-array> dup [
188         PATH_MAX
189         [ readlink ] unix-system-call
190     ] dip swap head-slice >string ;
191
192 FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
193 FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
194 FUNCTION: int rename ( char* from, char* to ) ;
195 FUNCTION: int rmdir ( char* path ) ;
196 FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
197 FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
198 FUNCTION: int setenv ( char* name, char* value, int overwrite ) ;
199 FUNCTION: int unsetenv ( char* name ) ;
200 FUNCTION: int setegid ( gid_t egid ) ;
201 FUNCTION: int seteuid ( uid_t euid ) ;
202 FUNCTION: int setgid ( gid_t gid ) ;
203 FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
204 FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
205 FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
206 FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
207 FUNCTION: int setuid ( uid_t uid ) ;
208 FUNCTION: int socket ( int domain, int type, int protocol ) ;
209 FUNCTION: int symlink ( char* path1, char* path2 ) ;
210 FUNCTION: int system ( char* command ) ;
211
212 FUNCTION: int unlink ( char* path ) ;
213
214 : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
215
216 FUNCTION: int utimes ( char* path, timeval[2] times ) ;
217
218 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
219
220 {
221     { [ os linux? ] [ "unix.linux" require ] }
222     { [ os bsd? ] [ "unix.bsd" require ] }
223     { [ os solaris? ] [ "unix.solaris" require ] }
224 } cond
225
226 "debugger" vocab [
227     "unix.debugger" require
228 ] when