The unix library (distributed in contrib/libunix) makes many Unix system calls and system-related library functions available to Caml Light programs. This chapter describes briefly the functions provided. Refer to sections 2 and 3 of the Unix manual for more details on the behavior of these functions.
Not all functions are provided by all Unix variants. If some functions are not available, they will raise Invalid_arg when called.
Programs that use the unix library must be linked in ``custom runtime'' mode, as follows:
camlc -custom other options unix.zo other files -lunixFor interactive use of the unix library, run camllight camlunix.
type error = ENOERR | EPERM (* Not owner *) | ENOENT (* No such file or directory *) | ESRCH (* No such process *) | EINTR (* Interrupted system call *) | EIO (* I/O error *) | ENXIO (* No such device or address *) | E2BIG (* Arg list too long *) | ENOEXEC (* Exec format error *) | EBADF (* Bad file number *) | ECHILD (* No children *) | EAGAIN (* No more processes *) | ENOMEM (* Not enough core *) | EACCES (* Permission denied *) | EFAULT (* Bad address *) | ENOTBLK (* Block device required *) | EBUSY (* Mount device busy *) | EEXIST (* File exists *) | EXDEV (* Cross-device link *) | ENODEV (* No such device *) | ENOTDIR (* Not a directory*) | EISDIR (* Is a directory *) | EINVAL (* Invalid argument *) | ENFILE (* File table overflow *) | EMFILE (* Too many open files *) | ENOTTY (* Not a typewriter *) | ETXTBSY (* Text file busy *) | EFBIG (* File too large *) | ENOSPC (* No space left on device *) | ESPIPE (* Illegal seek *) | EROFS (* Read-only file system *) | EMLINK (* Too many links *) | EPIPE (* Broken pipe *) | EDOM (* Argument too large *) | ERANGE (* Result too large *) | EWOULDBLOCK (* Operation would block *) | EINPROGRESS (* Operation now in progress *) | EALREADY (* Operation already in progress *) | ENOTSOCK (* Socket operation on non-socket *) | EDESTADDRREQ (* Destination address required *) | EMSGSIZE (* Message too long *) | EPROTOTYPE (* Protocol wrong type for socket *) | ENOPROTOOPT (* Protocol not available *) | EPROTONOSUPPORT (* Protocol not supported *) | ESOCKTNOSUPPORT (* Socket type not supported *) | EOPNOTSUPP (* Operation not supported on socket *) | EPFNOSUPPORT (* Protocol family not supported *) | EAFNOSUPPORT (* Address family not supported by protocol family *) | EADDRINUSE (* Address already in use *) | EADDRNOTAVAIL (* Can't assign requested address *) | ENETDOWN (* Network is down *) | ENETUNREACH (* Network is unreachable *) | ENETRESET (* Network dropped connection on reset *) | ECONNABORTED (* Software caused connection abort *) | ECONNRESET (* Connection reset by peer *) | ENOBUFS (* No buffer space available *) | EISCONN (* Socket is already connected *) | ENOTCONN (* Socket is not connected *) | ESHUTDOWN (* Can't send after socket shutdown *) | ETOOMANYREFS (* Too many references: can't splice *) | ETIMEDOUT (* Connection timed out *) | ECONNREFUSED (* Connection refused *) | ELOOP (* Too many levels of symbolic links *) | ENAMETOOLONG (* File name too long *) | EHOSTDOWN (* Host is down *) | EHOSTUNREACH (* No route to host *) | ENOTEMPTY (* Directory not empty *) | EPROCLIM (* Too many processes *) | EUSERS (* Too many users *) | EDQUOT (* Disc quota exceeded *) | ESTALE (* Stale NFS file handle *) | EREMOTE (* Too many levels of remote in path *) | EIDRM (* Identifier removed *) | EDEADLK (* Deadlock condition. *) | ENOLCK (* No record locks available. *) | ENOSYS (* Function not implemented *) | EUNKNOWNERR
exception Unix_error of error * string * string
value error_message : error -> string
value handle_unix_error : ('a -> 'b) -> 'a -> 'b
value environment : unit -> string vect
type process_status = WEXITED of int | WSIGNALED of int * bool | WSTOPPED of int
type wait_flag = WNOHANG | WUNTRACED
value execv : string -> string vect -> unit
value execve : string -> string vect -> string vect -> unit
value execvp : string -> string vect -> unit
value fork : unit -> int
value wait : unit -> int * process_status
value waitopt : wait_flag list -> int * process_status
value waitpid : wait_flag list -> int -> int * process_status
value system : string -> process_status
value getpid : unit -> int
value getppid : unit -> int
value nice : int -> int
type file_descr
value stdin : file_descr value stdout : file_descr value stderr : file_descr
type open_flag = O_RDONLY (* Open for reading *) | O_WRONLY (* Open for writing *) | O_RDWR (* Open for reading and writing *) | O_NDELAY (* Open in non-blocking mode *) | O_APPEND (* Open for append *) | O_CREAT (* Create if nonexistent *) | O_TRUNC (* Truncate to 0 length if existing *) | O_EXCL (* Fail if existing *)
type file_perm == int
value open : string -> open_flag list -> file_perm -> file_descr
value close : file_descr -> unit
value read : file_descr -> string -> int -> int -> int
value write : file_descr -> string -> int -> int -> int
value in_channel_of_descr : file_descr -> in_channel
value out_channel_of_descr : file_descr -> out_channel
value descr_of_in_channel : in_channel -> file_descr
value descr_of_out_channel : out_channel -> file_descr
type seek_command = SEEK_SET | SEEK_CUR | SEEK_END
value lseek : file_descr -> int -> seek_command -> int
value truncate : string -> int -> unit
value ftruncate : file_descr -> int -> unit
type file_kind = S_REG (* Regular file *) | S_DIR (* Directory *) | S_CHR (* Character device *) | S_BLK (* Block device *) | S_LNK (* Symbolic link *) | S_FIFO (* Named pipe *) | S_SOCK (* Socket *) type stats = { st_dev : int; (* Device number *) st_ino : int; (* Inode number *) st_kind : file_kind; (* Kind of the file *) st_perm : file_perm; (* Access rights *) st_nlink : int; (* Number of links *) st_uid : int; (* User id of the owner *) st_gid : int; (* Group id of the owner *) st_rdev : int; (* Device minor number *) st_size : int; (* Size in bytes *) st_atime : int; (* Last access time *) st_mtime : int; (* Last modification time *) st_ctime : int } (* Last status change time *)
value stat : string -> stats
value lstat : string -> stats
value fstat : file_descr -> stats
value unlink : string -> unit
value rename : string -> string -> unit
value link : string -> string -> unit
type access_permission = R_OK (* Read permission *) | W_OK (* Write permission *) | X_OK (* Execution permission *) | F_OK (* File exists *)
value chmod : string -> file_perm -> unit
value fchmod : file_descr -> file_perm -> unit
value chown : string -> int -> int -> unit
value fchown : file_descr -> int -> int -> unit
value umask : int -> int
value access : string -> access_permission list -> unit
value fcntl_int : file_descr -> int -> int -> int
value fcntl_ptr : file_descr -> int -> string -> int
value mkdir : string -> file_perm -> unit
value rmdir : string -> unit
value chdir : string -> unit
value getcwd : unit -> string
type dir_handle
value opendir : string -> dir_handle
value readdir : dir_handle -> string
value rewinddir : dir_handle -> unit
value closedir : dir_handle -> unit
value pipe : unit -> file_descr * file_descr
value dup : file_descr -> file_descr
value dup2 : file_descr -> file_descr -> unit
value open_process_in: string -> in_channel value open_process_out: string -> out_channel value open_process: string -> in_channel * out_channel
value close_process_in: in_channel -> process_status value close_process_out: out_channel -> process_status value close_process: in_channel * out_channel -> process_status
value symlink : string -> string -> unit
value readlink : string -> string
value mkfifo : string -> file_perm -> unit
value ioctl_int : file_descr -> int -> int -> int
value ioctl_ptr : file_descr -> int -> string -> int
value select : file_descr list -> file_descr list -> file_descr list -> float -> file_descr list * file_descr list * file_descr list
type lock_command = F_ULOCK (* Unlock a region *) | F_LOCK (* Lock a region, and block if already locked *) | F_TLOCK (* Lock a region, or fail if already locked *) | F_TEST (* Test a region for other process' locks *)
value lockf : file_descr -> lock_command -> int -> unit
type signal = SIGHUP (* hangup *) | SIGINT (* interrupt *) | SIGQUIT (* quit *) | SIGILL (* illegal instruction (not reset when caught) *) | SIGTRAP (* trace trap (not reset when caught) *) | SIGABRT (* used by abort *) | SIGEMT (* EMT instruction *) | SIGFPE (* floating point exception *) | SIGKILL (* kill (cannot be caught or ignored) *) | SIGBUS (* bus error *) | SIGSEGV (* segmentation violation *) | SIGSYS (* bad argument to system call *) | SIGPIPE (* write on a pipe with no one to read it *) | SIGALRM (* alarm clock *) | SIGTERM (* software termination signal from kill *) | SIGURG (* urgent condition on IO channel *) | SIGSTOP (* sendable stop signal not from tty *) | SIGTSTP (* stop signal from tty *) | SIGCONT (* continue a stopped process *) | SIGCHLD (* to parent on child stop or exit *) | SIGIO (* input/output possible signal *) | SIGXCPU (* exceeded CPU time limit *) | SIGXFSZ (* exceeded file size limit *) | SIGVTALRM (* virtual time alarm *) | SIGPROF (* profiling time alarm *) | SIGWINCH (* window changed *) | SIGLOST (* resource lost (eg, record-lock lost) *) | SIGUSR1 (* user defined signal 1 *) | SIGUSR2 (* user defined signal 2 *)
type signal_handler = Signal_default (* Default behavior for the signal *) | Signal_ignore (* Ignore the signal *) | Signal_handle of (unit -> unit) (* Call the given function when the signal occurs. *)
value kill : int -> signal -> unit
value signal : signal -> signal_handler -> unit
value pause : unit -> unit
type process_times = { tms_utime : float; (* User time for the process *) tms_stime : float; (* System time for the process *) tms_cutime : float; (* User time for the children processes *) tms_cstime : float } (* System time for the children processes *)
type tm = { tm_sec : int; (* Seconds 0..59 *) tm_min : int; (* Minutes 0..59 *) tm_hour : int; (* Hours 0..23 *) tm_mday : int; (* Day of month 1..31 *) tm_mon : int; (* Month of year 0..11 *) tm_year : int; (* Year - 1900 *) tm_wday : int; (* Day of week (Sunday is 0) *) tm_yday : int; (* Day of year 0..365 *) tm_isdst : bool } (* Daylight time savings in effect *)
value time : unit -> int
value gettimeofday : unit -> float
value gmtime : int -> tm
value localtime : int -> tm
value alarm : int -> int
value sleep : int -> unit
value times : unit -> process_times
value utimes : string -> int -> int -> unit
value getuid : unit -> int
value geteuid : unit -> int
value setuid : int -> unit
value getgid : unit -> int
value getegid : unit -> int
value setgid : int -> unit
value getgroups : unit -> int vect
type passwd_entry = { pw_name : string; pw_passwd : string; pw_uid : int; pw_gid : int; pw_gecos : string; pw_dir : string; pw_shell : string }
type group_entry = { gr_name : string; gr_passwd : string; gr_gid : int; gr_mem : string vect }
value getlogin : unit -> string
value getpwnam : string -> passwd_entry
value getgrnam : string -> group_entry
value getpwuid : int -> passwd_entry
value getgrgid : int -> group_entry
type inet_addr
value inet_addr_of_string : string -> inet_addr value string_of_inet_addr : inet_addr -> string
type socket_domain = PF_UNIX (* Unix domain *) | PF_INET (* Internet domain *)
type socket_type = SOCK_STREAM (* Stream socket *) | SOCK_DGRAM (* Datagram socket *) | SOCK_RAW (* Raw socket *) | SOCK_SEQPACKET (* Sequenced packets socket *)
type sockaddr = ADDR_UNIX of string | ADDR_INET of inet_addr * int
type shutdown_command = SHUTDOWN_RECEIVE (* Close for receiving *) | SHUTDOWN_SEND (* Close for sending *) | SHUTDOWN_ALL (* Close both *)
type msg_flag = MSG_OOB | MSG_DONTROUTE | MSG_PEEK
value socket : socket_domain -> socket_type -> int -> file_descr
value socketpair : socket_domain -> socket_type -> int -> file_descr * file_descr
value accept : file_descr -> file_descr * sockaddr
value bind : file_descr -> sockaddr -> unit
value connect : file_descr -> sockaddr -> unit
value listen : file_descr -> int -> unit
value shutdown : file_descr -> shutdown_command -> unit
value getsockname : file_descr -> sockaddr
value getpeername : file_descr -> sockaddr
value recv : file_descr -> string -> int -> int -> msg_flag list -> int value recvfrom : file_descr -> string -> int -> int -> msg_flag list -> int * sockaddr
value send : file_descr -> string -> int -> int -> msg_flag list -> int value sendto : file_descr -> string -> int -> int -> msg_flag list -> sockaddr -> int
value open_connection : sockaddr -> in_channel * out_channel
value shutdown_connection : in_channel -> unit
value establish_server : (in_channel -> out_channel -> unit) -> sockaddr -> unit
type host_entry = { h_name : string; h_aliases : string vect; h_addrtype : socket_domain; h_addr_list : inet_addr vect }
type protocol_entry = { p_name : string; p_aliases : string vect; p_proto : int }
type service_entry = { s_name : string; s_aliases : string vect; s_port : int; s_proto : string }
value gethostname : unit -> string
value gethostbyname : string -> host_entry
value gethostbyaddr : inet_addr -> host_entry
value getprotobyname : string -> protocol_entry
value getprotobynumber : int -> protocol_entry
value getservbyname : string -> string -> service_entry
value getservbyport : int -> string -> service_entry
type terminal_io = {
mutable c_ignbrk: bool; (* Ignore the break condition. *) mutable c_brkint: bool; (* Signal interrupt on break condition. *) mutable c_ignpar: bool; (* Ignore characters with parity errors. *) mutable c_parmrk: bool; (* Mark parity errors. *) mutable c_inpck: bool; (* Enable parity check on input. *) mutable c_istrip: bool; (* Strip 8th bit on input characters. *) mutable c_inlcr: bool; (* Map NL to CR on input. *) mutable c_igncr: bool; (* Ignore CR on input. *) mutable c_icrnl: bool; (* Map CR to NL on input. *) mutable c_ixon: bool; (* Recognize XON/XOFF characters on input. *) mutable c_ixoff: bool; (* Emit XON/XOFF chars to control input flow. *)
mutable c_opost: bool; (* Enable output processing. *)
mutable c_obaud: int; (* Output baud rate (0 means close connection).*) mutable c_ibaud: int; (* Input baud rate. *) mutable c_csize: int; (* Number of bits per character (5-8). *) mutable c_cstopb: int; (* Number of stop bits (1-2). *) mutable c_cread: bool; (* Reception is enabled. *) mutable c_parenb: bool; (* Enable parity generation and detection. *) mutable c_parodd: bool; (* Specify odd parity instead of even. *) mutable c_hupcl: bool; (* Hang up on last close. *) mutable c_clocal: bool; (* Ignore modem status lines. *)
mutable c_isig: bool; (* Generate signal on INTR, QUIT, SUSP. *) mutable c_icanon: bool; (* Enable canonical processing (line buffering and editing) *) mutable c_noflsh: bool; (* Disable flush after INTR, QUIT, SUSP. *) mutable c_echo: bool; (* Echo input characters. *) mutable c_echoe: bool; (* Echo ERASE (to erase previous character). *) mutable c_echok: bool; (* Echo KILL (to erase the current line). *) mutable c_echonl: bool; (* Echo NL even if c_echo is not set. *)
mutable c_vintr: char; (* Interrupt character (usually ctrl-C). *) mutable c_vquit: char; (* Quit character (usually ctrl-\). *) mutable c_verase: char; (* Erase character (usually DEL or ctrl-H). *) mutable c_vkill: char; (* Kill line character (usually ctrl-U). *) mutable c_veof: char; (* End-of-file character (usually ctrl-D). *) mutable c_veol: char; (* Alternate end-of-line char. (usually none). *) mutable c_vmin: int; (* Minimum number of characters to read before the read request is satisfied. *) mutable c_vtime: int; (* Maximum read wait (in 0.1s units). *) mutable c_vstart: char; (* Start character (usually ctrl-Q). *) mutable c_vstop: char (* Stop character (usually ctrl-S). *) } value tcgetattr: file_descr -> terminal_io
type setattr_when = TCSANOW | TCSADRAIN | TCSAFLUSH value tcsetattr: file_descr -> setattr_when -> terminal_io -> unit
value tcsendbreak: file_descr -> int -> unit
value tcdrain: file_descr -> unit
type flush_queue = TCIFLUSH | TCOFLUSH | TCIOFLUSH value tcflush: file_descr -> flush_queue -> unit
type flow_action = TCOOFF | TCOON | TCIOFF | TCION value tcflow: file_descr -> flow_action -> unit