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