The unix library 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:
cslc -custom other options unix.cma other files -cclib -lunix
cslopt other options unix.cmxa other files -cclib -lunix
For interactive use of the unix library, do:
cslmktop -custom -o mytop unix.cma -cclib -lunix
./mytop
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
val error_message : error -> string
val handle_unix_error : ('a -> 'b) -> 'a -> 'b
val environment : unit -> string array
type process_status =
WEXITED of int
| WSIGNALED of int * bool
| WSTOPPED of int
type wait_flag =
WNOHANG
| WUNTRACED
val execv : string -> string array -> unit
val execve : string -> string array -> string array -> unit
val execvp : string -> string array -> unit
val fork : unit -> int
val wait : unit -> int * process_status
val waitpid : wait_flag list -> int -> int * process_status
val system : string -> process_status
val getpid : unit -> int
val getppid : unit -> int
val nice : int -> int
type file_descr
val stdin : file_descr val stdout : file_descr val 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
val openfile : string -> open_flag list -> file_perm -> file_descr
val close : file_descr -> unit
val read : file_descr -> string -> int -> int -> int
val write : file_descr -> string -> int -> int -> int
val in_channel_of_descr : file_descr -> in_channel
val out_channel_of_descr : file_descr -> out_channel
val descr_of_in_channel : in_channel -> file_descr
val descr_of_out_channel : out_channel -> file_descr
type seek_command =
SEEK_SET
| SEEK_CUR
| SEEK_END
val lseek : file_descr -> int -> seek_command -> int
val truncate : string -> int -> unit
val 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 *)
val stat : string -> stats
val lstat : string -> stats
val fstat : file_descr -> stats
val unlink : string -> unit
val rename : string -> string -> unit
val link : string -> string -> unit
type access_permission =
R_OK (* Read permission *)
| W_OK (* Write permission *)
| X_OK (* Execution permission *)
| F_OK (* File exists *)
val chmod : string -> file_perm -> unit
val fchmod : file_descr -> file_perm -> unit
val chown : string -> int -> int -> unit
val fchown : file_descr -> int -> int -> unit
val umask : int -> int
val access : string -> access_permission list -> unit
val fcntl_int : file_descr -> int -> int -> int
val fcntl_ptr : file_descr -> int -> string -> int
val mkdir : string -> file_perm -> unit
val rmdir : string -> unit
val chdir : string -> unit
val getcwd : unit -> string
type dir_handle
val opendir : string -> dir_handle
val readdir : dir_handle -> string
val rewinddir : dir_handle -> unit
val closedir : dir_handle -> unit
val pipe : unit -> file_descr * file_descr
val dup : file_descr -> file_descr
val dup2 : file_descr -> file_descr -> unit
val open_process_in: string -> in_channel val open_process_out: string -> out_channel val open_process: string -> in_channel * out_channel
val close_process_in: in_channel -> process_status val close_process_out: out_channel -> process_status val close_process: in_channel * out_channel -> process_status
val symlink : string -> string -> unit
val readlink : string -> string
val mkfifo : string -> file_perm -> unit
val ioctl_int : file_descr -> int -> int -> int
val ioctl_ptr : file_descr -> int -> string -> int
val 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 *)
val lockf : file_descr -> lock_command -> int -> unit
val kill : int -> int -> unit
val 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 *)
val time : unit -> int
val gettimeofday : unit -> float
val gmtime : int -> tm
val localtime : int -> tm
val alarm : int -> int
val sleep : int -> unit
val times : unit -> process_times =
"unix_times_bytecode" "unix_times_native"
val utimes : string -> int -> int -> unit
val getuid : unit -> int
val geteuid : unit -> int
val setuid : int -> unit
val getgid : unit -> int
val getegid : unit -> int
val setgid : int -> unit
val getgroups : unit -> int array
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 array }
val getlogin : unit -> string
val getpwnam : string -> passwd_entry
val getgrnam : string -> group_entry
val getpwuid : int -> passwd_entry
val getgrgid : int -> group_entry
type inet_addr
val inet_addr_of_string : string -> inet_addr
val 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
val socket : socket_domain -> socket_type -> int -> file_descr
val socketpair :
socket_domain -> socket_type -> int -> file_descr * file_descr
val accept : file_descr -> file_descr * sockaddr
val bind : file_descr -> sockaddr -> unit
val connect : file_descr -> sockaddr -> unit
val listen : file_descr -> int -> unit
val shutdown : file_descr -> shutdown_command -> unit
val getsockname : file_descr -> sockaddr
val getpeername : file_descr -> sockaddr
val recv : file_descr -> string -> int -> int -> msg_flag list -> int
val recvfrom :
file_descr -> string -> int -> int -> msg_flag list -> int * sockaddr
val send : file_descr -> string -> int -> int -> msg_flag list -> int
val sendto :
file_descr -> string -> int -> int -> msg_flag list -> sockaddr -> int
= "unix_sendto" "unix_sendto_native"
val open_connection : sockaddr -> in_channel * out_channel
val shutdown_connection : in_channel -> unit
val establish_server : (in_channel -> out_channel -> 'a) -> sockaddr -> unit
type host_entry =
{ h_name : string;
h_aliases : string array;
h_addrtype : socket_domain;
h_addr_list : inet_addr array }
type protocol_entry =
{ p_name : string;
p_aliases : string array;
p_proto : int }
type service_entry =
{ s_name : string;
s_aliases : string array;
s_port : int;
s_proto : string }
val gethostname : unit -> string
val gethostbyname : string -> host_entry
val gethostbyaddr : inet_addr -> host_entry
val getprotobyname : string -> protocol_entry
val getprotobynumber : int -> protocol_entry
val getservbyname : string -> string -> service_entry
val 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). *)
}
val tcgetattr: file_descr -> terminal_io
type setattr_when = TCSANOW | TCSADRAIN | TCSAFLUSH
val tcsetattr: file_descr -> setattr_when -> terminal_io -> unit
val tcsendbreak: file_descr -> int -> unit
val tcdrain: file_descr -> unit
type flush_queue = TCIFLUSH | TCOFLUSH | TCIOFLUSH val tcflush: file_descr -> flush_queue -> unit
type flow_action = TCOOFF | TCOON | TCIOFF | TCION val tcflow: file_descr -> flow_action -> unit