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