The unix library makes many Unix system calls and system-related library functions available to Objective Caml 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.
ocamlc -custom other options unix.cma other files -cclib -lunix ocamlopt other options unix.cmxa other files -cclib -lunixFor interactive use of the unix library, do:
ocamlmktop -custom -o mytop unix.cma -cclib -lunix ./mytop
Programs that use the unix library must be linked in ``custom runtime'' mode, as follows:
ocamlc -custom other options unix.cma other files \ %CAMLLIB%/libunix.lib wsock32.lib ocamlopt other options unix.cmxa other files \ %CAMLLIB%/libunix.lib wsock32.libFor interactive use of the unix library, do:
ocamlmktop -custom -o mytop.exe unix.cma \ %CAMLLIB%/libunix.lib wsock32.lib mytop.exe
type error =
E2BIG (* Argument list too long *) | EACCES (* Permission denied *) | EAGAIN (* Resource temporarily unavailable; try again *) | EBADF (* Bad file descriptor *) | EBUSY (* Resource unavailable *) | ECHILD (* No child process *) | EDEADLK (* Resource deadlock would occur *) | EDOM (* Domain error for math functions, etc. *) | EEXIST (* File exists *) | EFAULT (* Bad address *) | EFBIG (* File too large *) | EINTR (* Function interrupted by signal *) | EINVAL (* Invalid argument *) | EIO (* Hardware I/O error *) | EISDIR (* Is a directory *) | EMFILE (* Too many open files by the process *) | EMLINK (* Too many links *) | ENAMETOOLONG (* Filename too long *) | ENFILE (* Too many open files in the system *) | ENODEV (* No such device *) | ENOENT (* No such file or directory *) | ENOEXEC (* Not an executable file *) | ENOLCK (* No locks available *) | ENOMEM (* Not enough memory *) | ENOSPC (* No space left on device *) | ENOSYS (* Function not supported *) | ENOTDIR (* Not a directory *) | ENOTEMPTY (* Directory not empty *) | ENOTTY (* Inappropriate I/O control operation *) | ENXIO (* No such device or address *) | EPERM (* Operation not permitted *) | EPIPE (* Broken pipe *) | ERANGE (* Result too large *) | EROFS (* Read-only file system *) | ESPIPE (* Invalid seek e.g. on a pipe *) | ESRCH (* No such process *) | EXDEV (* Invalid link *)
| 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 *) | EHOSTDOWN (* Host is down *) | EHOSTUNREACH (* No route to host *) | ELOOP (* Too many levels of symbolic links *)
| EUNKNOWNERR (* Unknown error *)
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 | 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 execvpe : string -> string array -> 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_NONBLOCK (* 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 dup : file_descr -> file_descr
val dup2 : file_descr -> file_descr -> unit
val set_nonblock : file_descr -> unit val clear_nonblock : file_descr -> unit
val set_close_on_exec : file_descr -> unit val clear_close_on_exec : file_descr -> unit
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 mkfifo : string -> file_perm -> unit
val create_process : string -> string array -> file_descr -> file_descr -> file_descr -> int
val create_process_env : string -> string array -> string array -> file_descr -> file_descr -> file_descr -> int
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 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 mktime : tm -> 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
type interval_timer = ITIMER_REAL | ITIMER_VIRTUAL | ITIMER_PROF
type interval_timer_status = { it_interval: float; (* Period *) it_value: float } (* Current value of the timer *)
val getitimer: interval_timer -> interval_timer_status
val setitimer: interval_timer -> interval_timer_status -> interval_timer_status
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
val inet_addr_any : inet_addr
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
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
type shutdown_command = SHUTDOWN_RECEIVE (* Close for receiving *) | SHUTDOWN_SEND (* Close for sending *) | SHUTDOWN_ALL (* Close both *)
val shutdown : file_descr -> shutdown_command -> unit
val getsockname : file_descr -> sockaddr
val getpeername : file_descr -> sockaddr
type msg_flag = MSG_OOB | MSG_DONTROUTE | MSG_PEEK
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
type socket_option = SO_DEBUG (* Record debugging information *) | SO_BROADCAST (* Permit sending of broadcast messages *) | SO_REUSEADDR (* Allow reuse of local addresses for bind *) | SO_KEEPALIVE (* Keep connection active *) | SO_DONTROUTE (* Bypass the standard routing algorithms *) | SO_OOBINLINE (* Leave out-of-band data in line *)
val getsockopt : file_descr -> socket_option -> bool
val setsockopt : file_descr -> socket_option -> bool -> unit
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
val setsid : unit -> int
Functions | Comment |
---|---|
fork | not implemented, use create_process or threads |
wait | not implemented, use waitpid |
waitpid | can only wait for a given PID, not any child process |
getppid | not implemented (meaningless under Windows) |
nice | not implemented |
truncate, ftruncate | not implemented |
lstat, fstat | not implemented |
link, symlink, readlink | not implemented (no links under Windows) |
chmod, fchmod | not implemented |
chown, fchown | not implemented (make no sense on a DOS file system) |
umask | not implemented |
set_nonblock, clear_nonblock | implemented as dummy functions; use threads instead of non-blocking I/O |
rewinddir | not implemented; re-open the directory instead |
mkfifo | not implemented |
ioctl_int, ioctl_ptr | not implemented (no ioctl in Windows) |
select | implemented, but works only for sockets; use threads if you need to wait on other kinds of file descriptors |
lockf | not implemented |
kill, pause | not implemented (no inter-process signals in Windows) |
alarm, times | not implemented |
getitimer, setitimer | not implemented |
getuid, getgid | always return 1 |
getgid, getegid, getgroups | not implemented |
setuid, setgid | not implemented |
getpwnam, getpwuid | always raise Not_found |
getgrnam, getgrgid | always raise Not_found |
type socket_domain | the domain PF_UNIX is not supported; PF_INET is fully supported |
establish_server | not implemented; use threads |
terminal functions (tc*) | not implemented |