| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2005, 2007 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-02-28 13:46:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 18:11:40 -04:00
										 |  |  | USING: alien alien.c-types alien.syntax kernel libc structs sequences | 
					
						
							| 
									
										
										
										
											2008-05-13 19:28:43 -04:00
										 |  |  |        continuations byte-arrays strings | 
					
						
							| 
									
										
										
										
											2008-05-10 06:41:00 -04:00
										 |  |  |        math namespaces system combinators vocabs.loader qualified | 
					
						
							| 
									
										
										
										
											2008-05-14 01:44:27 -04:00
										 |  |  |        accessors inference macros locals shuffle arrays.lib  | 
					
						
							| 
									
										
										
										
											2008-05-21 16:54:02 -04:00
										 |  |  |        unix.types debugger io prettyprint ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-28 13:46:01 -05:00
										 |  |  | IN: unix | 
					
						
							| 
									
										
										
										
											2008-02-26 21:59:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | TYPEDEF: uint in_addr_t | 
					
						
							|  |  |  | TYPEDEF: uint socklen_t | 
					
						
							| 
									
										
										
										
											2008-02-04 13:03:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : PROT_NONE   0 ; inline
 | 
					
						
							|  |  |  | : PROT_READ   1 ; inline
 | 
					
						
							|  |  |  | : PROT_WRITE  2 ; inline
 | 
					
						
							|  |  |  | : PROT_EXEC   4 ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : MAP_FILE    0 ; inline
 | 
					
						
							|  |  |  | : MAP_SHARED  1 ; inline
 | 
					
						
							|  |  |  | : MAP_PRIVATE 2 ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : MAP_FAILED -1 <alien> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-19 20:00:06 -04:00
										 |  |  | : NGROUPS_MAX 16 ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-10 13:21:38 -04:00
										 |  |  | C-STRUCT: group | 
					
						
							|  |  |  |     { "char*" "gr_name" } | 
					
						
							|  |  |  |     { "char*" "gr_passwd" } | 
					
						
							|  |  |  |     { "int" "gr_gid" } | 
					
						
							|  |  |  |     { "char**" "gr_mem" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | LIBRARY: factor | 
					
						
							| 
									
										
										
										
											2008-05-12 18:11:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-21 15:53:11 -04:00
										 |  |  | FUNCTION: void clear_err_no ( ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-12 18:11:40 -04:00
										 |  |  | FUNCTION: int err_no ( ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-14 01:44:27 -04:00
										 |  |  | LIBRARY: libc | 
					
						
							| 
									
										
										
										
											2008-05-12 18:11:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-14 01:44:27 -04:00
										 |  |  | FUNCTION: char* strerror ( int errno ) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-21 16:54:02 -04:00
										 |  |  | ERROR: unix-error errno message ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-error error. | 
					
						
							|  |  |  |     "Unix system call failed:" print
 | 
					
						
							|  |  |  |     nl
 | 
					
						
							|  |  |  |     dup message>> write " (" write errno>> pprint ")" print ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (io-error) ( -- * ) err_no dup strerror unix-error ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : io-error ( n -- ) 0 < [ (io-error) ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: unix-system-call-error args errno message word ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix-system-call-error error. | 
					
						
							|  |  |  |     "Unix system call ``" write dup word>> pprint "'' failed:" print
 | 
					
						
							|  |  |  |     nl
 | 
					
						
							|  |  |  |     dup message>> write " (" write dup errno>> pprint ")" print
 | 
					
						
							|  |  |  |     nl
 | 
					
						
							|  |  |  |     "It was called with the following arguments:" print
 | 
					
						
							|  |  |  |     nl
 | 
					
						
							|  |  |  |     args>> stack. ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-14 01:44:27 -04:00
										 |  |  | MACRO:: unix-system-call ( quot -- )
 | 
					
						
							|  |  |  |     [let | n [ quot infer in>> ] | 
					
						
							|  |  |  |            word [ quot first ] | | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             n ndup quot call dup 0 < [ | 
					
						
							|  |  |  |                 drop
 | 
					
						
							|  |  |  |                 n narray | 
					
						
							| 
									
										
										
										
											2008-05-21 16:54:02 -04:00
										 |  |  |                 err_no dup strerror | 
					
						
							| 
									
										
										
										
											2008-05-14 01:44:27 -04:00
										 |  |  |                 word unix-system-call-error | 
					
						
							|  |  |  |             ] [ | 
					
						
							|  |  |  |                 n nnip | 
					
						
							|  |  |  |             ] if
 | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |     ] ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
 | 
					
						
							|  |  |  | FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
 | 
					
						
							|  |  |  | FUNCTION: int chdir ( char* path ) ;
 | 
					
						
							|  |  |  | FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
 | 
					
						
							|  |  |  | FUNCTION: int chroot ( char* path ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 23:59:42 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: int close ( int fd ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : close-file ( fd -- ) [ close ] unix-system-call drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
 | 
					
						
							|  |  |  | FUNCTION: int dup2 ( int oldd, int newd ) ;
 | 
					
						
							|  |  |  | ! FUNCTION: int dup ( int oldd ) ; | 
					
						
							| 
									
										
										
										
											2008-04-06 00:31:41 -04:00
										 |  |  | : _exit ( status -- * )
 | 
					
						
							|  |  |  |     #! We throw to give this a terminating stack effect. | 
					
						
							|  |  |  |     "int" f "_exit" { "int" } alien-invoke "Exit failed" throw ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int fchdir ( int fd ) ;
 | 
					
						
							|  |  |  | FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
 | 
					
						
							|  |  |  | FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
 | 
					
						
							|  |  |  | FUNCTION: int flock ( int fd, int operation ) ;
 | 
					
						
							|  |  |  | FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
 | 
					
						
							|  |  |  | FUNCTION: int futimes ( int id, timeval[2] times ) ;
 | 
					
						
							|  |  |  | FUNCTION: char* gai_strerror ( int ecode ) ;
 | 
					
						
							|  |  |  | FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
 | 
					
						
							| 
									
										
										
										
											2008-02-05 14:11:36 -05:00
										 |  |  | FUNCTION: char* getcwd ( char* buf, size_t size ) ;
 | 
					
						
							| 
									
										
										
										
											2008-02-14 18:56:47 -05:00
										 |  |  | FUNCTION: pid_t getpid ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int getdtablesize ;
 | 
					
						
							|  |  |  | FUNCTION: gid_t getegid ;
 | 
					
						
							|  |  |  | FUNCTION: uid_t geteuid ;
 | 
					
						
							|  |  |  | FUNCTION: gid_t getgid ;
 | 
					
						
							| 
									
										
										
										
											2008-05-10 13:21:38 -04:00
										 |  |  | FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
 | 
					
						
							|  |  |  | FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
 | 
					
						
							|  |  |  | FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
 | 
					
						
							|  |  |  | FUNCTION: int gethostname ( char* name, int len ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-12 19:18:14 -04:00
										 |  |  | FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 21:04:57 -04:00
										 |  |  | FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: uid_t getuid ;
 | 
					
						
							|  |  |  | FUNCTION: uint htonl ( uint n ) ;
 | 
					
						
							|  |  |  | FUNCTION: ushort htons ( ushort n ) ;
 | 
					
						
							|  |  |  | ! FUNCTION: int issetugid ; | 
					
						
							|  |  |  | FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
 | 
					
						
							|  |  |  | FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
 | 
					
						
							|  |  |  | FUNCTION: int listen ( int s, int backlog ) ;
 | 
					
						
							|  |  |  | FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
 | 
					
						
							|  |  |  | FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
 | 
					
						
							|  |  |  | FUNCTION: int munmap ( void* addr, size_t len ) ;
 | 
					
						
							|  |  |  | FUNCTION: uint ntohl ( uint n ) ;
 | 
					
						
							|  |  |  | FUNCTION: ushort ntohs ( ushort n ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-17 18:45:56 -04:00
										 |  |  | FUNCTION: int shutdown ( int fd, int how ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-08 07:13:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 18:11:40 -04:00
										 |  |  | FUNCTION: int open ( char* path, int flags, int prot ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : open-file ( path flags mode -- fd ) [ open ] unix-system-call ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C-STRUCT: utimbuf | 
					
						
							|  |  |  |     { "time_t" "actime"  } | 
					
						
							|  |  |  |     { "time_t" "modtime" } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FUNCTION: int utime ( char* path, utimebuf* buf ) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : touch ( filename -- ) f [ utime ] unix-system-call drop ;
 | 
					
						
							| 
									
										
										
										
											2008-05-08 07:13:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-12 18:11:40 -04:00
										 |  |  | : change-file-times ( filename access modification -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-12 18:40:49 -04:00
										 |  |  |     "utimebuf" <c-object> | 
					
						
							|  |  |  |     tuck set-utimbuf-modtime | 
					
						
							|  |  |  |     tuck set-utimbuf-actime | 
					
						
							|  |  |  |     [ utime ] unix-system-call drop ;
 | 
					
						
							| 
									
										
										
										
											2008-05-09 17:24:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int pclose ( void* file ) ;
 | 
					
						
							|  |  |  | FUNCTION: int pipe ( int* filedes ) ;
 | 
					
						
							|  |  |  | FUNCTION: void* popen ( char* command, char* type ) ;
 | 
					
						
							|  |  |  | FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:28:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-30 02:13:29 -04:00
										 |  |  | FUNCTION: ssize_t readlink ( char* path, char* buf, size_t bufsize ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:28:43 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : PATH_MAX 1024 ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : read-symbolic-link ( path -- path )
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:30:06 -04:00
										 |  |  |     PATH_MAX <byte-array> dup >r | 
					
						
							|  |  |  |     PATH_MAX | 
					
						
							|  |  |  |     [ readlink ] unix-system-call | 
					
						
							|  |  |  |     r> swap head-slice >string ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:28:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
 | 
					
						
							|  |  |  | FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
 | 
					
						
							|  |  |  | FUNCTION: int rename ( char* from, char* to ) ;
 | 
					
						
							|  |  |  | FUNCTION: int rmdir ( char* path ) ;
 | 
					
						
							|  |  |  | FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
 | 
					
						
							|  |  |  | FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setegid ( gid_t egid ) ;
 | 
					
						
							|  |  |  | FUNCTION: int seteuid ( uid_t euid ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setgid ( gid_t gid ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
 | 
					
						
							|  |  |  | FUNCTION: int setuid ( uid_t uid ) ;
 | 
					
						
							|  |  |  | FUNCTION: int socket ( int domain, int type, int protocol ) ;
 | 
					
						
							| 
									
										
										
										
											2008-03-30 02:13:29 -04:00
										 |  |  | FUNCTION: int symlink ( char* path1, char* path2 ) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int system ( char* command ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:40:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int unlink ( char* path ) ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:40:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-13 20:05:12 -04:00
										 |  |  | : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
 | 
					
						
							| 
									
										
										
										
											2008-05-13 19:40:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: int utimes ( char* path, timeval[2] times ) ;
 | 
					
						
							| 
									
										
										
										
											2007-11-04 14:42:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
 | 
					
						
							| 
									
										
										
										
											2007-12-28 21:46:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-04-02 21:33:36 -04:00
										 |  |  |     { [ os linux? ] [ "unix.linux" require ] } | 
					
						
							|  |  |  |     { [ os bsd? ] [ "unix.bsd" require ] } | 
					
						
							|  |  |  |     { [ os solaris? ] [ "unix.solaris" require ] } | 
					
						
							| 
									
										
										
										
											2008-02-06 21:04:09 -05:00
										 |  |  | } cond
 | 
					
						
							| 
									
										
										
										
											2008-02-26 21:14:09 -05:00
										 |  |  | 
 |