text
stringlengths
0
601k
let find_proc_id fun_name proc = try Hashtbl . find popen_processes proc with Not_found -> raise ( Unix_error ( EBADF , fun_name , " " ) )
let remove_proc_id proc = Hashtbl . remove popen_processes proc
let process_in_pid inchan = find_proc_id " process_in_pid " ( Process_in inchan )
let process_out_pid outchan = find_proc_id " process_out_pid " ( Process_out outchan )
let process_pid ( inchan , outchan ) = find_proc_id " process_pid " ( Process ( inchan , outchan ) )
let process_full_pid ( inchan , outchan , errchan ) = find_proc_id " process_full_pid " ( Process_full ( inchan , outchan , errchan ) )
let close_process_in inchan = let proc = Process_in inchan in let pid = find_proc_id " close_process_in " proc in remove_proc_id proc ; close_in inchan ; snd ( waitpid_non_intr pid )
let close_process_out outchan = let proc = Process_out outchan in let pid = find_proc_id " close_process_out " proc in remove_proc_id proc ; begin try close_out outchan with Sys_error _ -> ( ) end ; snd ( waitpid_non_intr pid )
let close_process ( inchan , outchan ) = let proc = Process ( inchan , outchan ) in let pid = find_proc_id " close_process " proc in remove_proc_id proc ; close_in inchan ; begin try close_out outchan with Sys_error _ -> ( ) end ; snd ( waitpid_non_intr pid )
let close_process_full ( inchan , outchan , errchan ) = let proc = Process_full ( inchan , outchan , errchan ) in let pid = find_proc_id " close_process_full " proc in remove_proc_id proc ; close_in inchan ; begin try close_out outchan with Sys_error _ -> ( ) end ; close_in errchan ; snd ( waitpid_non_intr pid )
let open_connection sockaddr = let sock = socket ~ cloexec : true ( domain_of_sockaddr sockaddr ) SOCK_STREAM 0 in try connect sock sockaddr ; ( in_channel_of_descr sock , out_channel_of_descr sock ) with exn -> close sock ; raise exn
let shutdown_connection inchan = shutdown ( descr_of_in_channel inchan ) SHUTDOWN_SEND
let rec accept_non_intr s = try accept ~ cloexec : true s with Unix_error ( EINTR , _ , _ ) -> accept_non_intr s
let establish_server server_fun sockaddr = let sock = socket ~ cloexec : true ( domain_of_sockaddr sockaddr ) SOCK_STREAM 0 in setsockopt sock SO_REUSEADDR true ; bind sock sockaddr ; listen sock 5 ; while true do let ( s , _caller ) = accept_non_intr sock in match fork ( ) with 0 -> if fork ( ) <> 0 then _exit 0 ; close sock ; let inchan = in_channel_of_descr s in let outchan = out_channel_of_descr s in server_fun inchan outchan ; exit 0 | id -> close s ; ignore ( waitpid_non_intr id ) done
Unix . file_descr -> ( char , Bigarray . int8_unsigned_elt , Bigarray . c_layout ) Bigarray . Array1 . t -> int result = " unix_util_read " Unix . file_descr -> ( char , Bigarray . int8_unsigned_elt , Bigarray . c_layout ) Bigarray . Array1 . t -> int result = " unix_util_write " = " unix_util_int_of_file_descr " = " unix_util_file_descr_of_int "
type statvfs = { f_bsize : int64 ; f_frsize : int64 ; f_blocks : int64 ; f_bfree : int64 ; f_bavail : int64 ; f_files : int64 ; f_ffree : int64 ; f_favail : int64 ; f_fsid : int64 ; f_flag : int64 ; f_namemax : int64 ; }
let statvfs path = match statvfs_noexn path with | Ok res -> res | Bad err -> raise ( Unix . Unix_error ( err , " statvfs " , " " ) )
let read fd buf = match read_noexn fd buf with | Ok res -> res | Bad err -> raise ( Unix . Unix_error ( err , " read " , " " ) )
let write fd buf = match write_noexn fd buf with | Ok res -> res | Bad err -> raise ( Unix . Unix_error ( err , " read " , " " ) )
let _ = startup ( ) ; at_exit cleanup
type error = E2BIG | EACCES | EAGAIN | EBADF | EBUSY | ECHILD | EDEADLK | EDOM | EEXIST | EFAULT | EFBIG | EINTR | EINVAL | EIO | EISDIR | EMFILE | EMLINK | ENAMETOOLONG | ENFILE | ENODEV | ENOENT | ENOEXEC | ENOLCK | ENOMEM | ENOSPC | ENOSYS | ENOTDIR | ENOTEMPTY | ENOTTY | ENXIO | EPERM | EPIPE | ERANGE | EROFS | ESPIPE | ESRCH | EXDEV | EWOULDBLOCK | EINPROGRESS | EALREADY | ENOTSOCK | EDESTADDRREQ | EMSGSIZE | EPROTOTYPE | ENOPROTOOPT | EPROTONOSUPPORT | ESOCKTNOSUPPORT | EOPNOTSUPP | EPFNOSUPPORT | EAFNOSUPPORT | EADDRINUSE | EADDRNOTAVAIL | ENETDOWN | ENETUNREACH | ENETRESET | ECONNABORTED | ECONNRESET | ENOBUFS | EISCONN | ENOTCONN | ESHUTDOWN | ETOOMANYREFS | ETIMEDOUT | ECONNREFUSED | EHOSTDOWN | EHOSTUNREACH | ELOOP | EOVERFLOW | EUNKNOWNERR of int
let _ = Callback . register_exception " Unix . Unix_error " ( Unix_error ( E2BIG , " " , " " ) )
let ( ) = Printexc . register_printer ( function | Unix_error ( e , s , s ' ) -> let msg = match e with | E2BIG -> " E2BIG " | EACCES -> " EACCES " | EAGAIN -> " EAGAIN " | EBADF -> " EBADF " | EBUSY -> " EBUSY " | ECHILD -> " ECHILD " | EDEADLK -> " EDEADLK " | EDOM -> " EDOM " | EEXIST -> " EEXIST " | EFAULT -> " EFAULT " | EFBIG -> " EFBIG " | EINTR -> " EINTR " | EINVAL -> " EINVAL " | EIO -> " EIO " | EISDIR -> " EISDIR " | EMFILE -> " EMFILE " | EMLINK -> " EMLINK " | ENAMETOOLONG -> " ENAMETOOLONG " | ENFILE -> " ENFILE " | ENODEV -> " ENODEV " | ENOENT -> " ENOENT " | ENOEXEC -> " ENOEXEC " | ENOLCK -> " ENOLCK " | ENOMEM -> " ENOMEM " | ENOSPC -> " ENOSPC " | ENOSYS -> " ENOSYS " | ENOTDIR -> " ENOTDIR " | ENOTEMPTY -> " ENOTEMPTY " | ENOTTY -> " ENOTTY " | ENXIO -> " ENXIO " | EPERM -> " EPERM " | EPIPE -> " EPIPE " | ERANGE -> " ERANGE " | EROFS -> " EROFS " | ESPIPE -> " ESPIPE " | ESRCH -> " ESRCH " | EXDEV -> " EXDEV " | EWOULDBLOCK -> " EWOULDBLOCK " | EINPROGRESS -> " EINPROGRESS " | EALREADY -> " EALREADY " | ENOTSOCK -> " ENOTSOCK " | EDESTADDRREQ -> " EDESTADDRREQ " | EMSGSIZE -> " EMSGSIZE " | EPROTOTYPE -> " EPROTOTYPE " | ENOPROTOOPT -> " ENOPROTOOPT " | EPROTONOSUPPORT -> " EPROTONOSUPPORT " | ESOCKTNOSUPPORT -> " ESOCKTNOSUPPORT " | EOPNOTSUPP -> " EOPNOTSUPP " | EPFNOSUPPORT -> " EPFNOSUPPORT " | EAFNOSUPPORT -> " EAFNOSUPPORT " | EADDRINUSE -> " EADDRINUSE " | EADDRNOTAVAIL -> " EADDRNOTAVAIL " | ENETDOWN -> " ENETDOWN " | ENETUNREACH -> " ENETUNREACH " | ENETRESET -> " ENETRESET " | ECONNABORTED -> " ECONNABORTED " | ECONNRESET -> " ECONNRESET " | ENOBUFS -> " ENOBUFS " | EISCONN -> " EISCONN " | ENOTCONN -> " ENOTCONN " | ESHUTDOWN -> " ESHUTDOWN " | ETOOMANYREFS -> " ETOOMANYREFS " | ETIMEDOUT -> " ETIMEDOUT " | ECONNREFUSED -> " ECONNREFUSED " | EHOSTDOWN -> " EHOSTDOWN " | EHOSTUNREACH -> " EHOSTUNREACH " | ELOOP -> " ELOOP " | EOVERFLOW -> " EOVERFLOW " | EUNKNOWNERR x -> Printf . sprintf " EUNKNOWNERR % d " x in Some ( Printf . sprintf " Unix . Unix_error ( Unix . % s , % S , % S ) " msg s s ' ) | _ -> None )
let handle_unix_error f arg = try f arg with Unix_error ( err , fun_name , arg ) -> prerr_string Sys . argv . ( 0 ) ; prerr_string " : " " ; \ prerr_string fun_name ; prerr_string " " \ failed " ; if String . length arg > 0 then begin prerr_string " on " " ; \ prerr_string arg ; prerr_string " " " \ end ; prerr_string " : " ; prerr_endline ( error_message err ) ; exit 2
type process_status = WEXITED of int | WSIGNALED of int | WSTOPPED of int
type wait_flag = WNOHANG | WUNTRACED
let maybe_quote f = if String . contains f ' ' || String . contains f ' " ' \ || String . contains f ' \ t ' || f = " " then Filename . quote f else f string -> string array -> string array -> ' a = " unix_execve " string -> string array -> string array -> ' a = " unix_execvpe "
let execv prog args = sys_execv prog ( Array . map maybe_quote args )
let execve prog args env = sys_execve prog ( Array . map maybe_quote args ) env
let execvp prog args = sys_execvp prog ( Array . map maybe_quote args )
let execvpe prog args env = sys_execvpe prog ( Array . map maybe_quote args ) env = " win_waitpid "
let fork ( ) = invalid_arg " Unix . fork not implemented "
let wait ( ) = invalid_arg " Unix . wait not implemented "
let getppid ( ) = invalid_arg " Unix . getppid not implemented "
let nice _ = invalid_arg " Unix . nice not implemented "
let stdin = filedescr_of_fd 0
let stdout = filedescr_of_fd 1
let stderr = filedescr_of_fd 2
type open_flag = O_RDONLY | O_WRONLY | O_RDWR | O_NONBLOCK | O_APPEND | O_CREAT | O_TRUNC | O_EXCL | O_NOCTTY | O_DSYNC | O_SYNC | O_RSYNC | O_SHARE_DELETE | O_CLOEXEC | O_KEEPEXEC
type file_perm = int = " unix_open " = " unix_read " = " unix_write " = " unix_single_write "
let read fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . read " else unsafe_read fd buf ofs len
let write fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . write " else unsafe_write fd buf ofs len
let single_write fd buf ofs len = if ofs < 0 || len < 0 || ofs > Bytes . length buf - len then invalid_arg " Unix . single_write " else unsafe_single_write fd buf ofs len
let write_substring fd buf ofs len = write fd ( Bytes . unsafe_of_string buf ) ofs len
let single_write_substring fd buf ofs len = single_write fd ( Bytes . unsafe_of_string buf ) ofs len = " win_inchannel_of_filedescr " = " win_outchannel_of_filedescr " = " win_filedescr_of_channel " = " win_filedescr_of_channel "
type seek_command = SEEK_SET | SEEK_CUR | SEEK_END
type file_kind = S_REG | S_DIR | S_CHR | S_BLK | S_LNK | S_FIFO | S_SOCK
type stats = { st_dev : int ; st_ino : int ; st_kind : file_kind ; st_perm : file_perm ; st_nlink : int ; st_uid : int ; st_gid : int ; st_rdev : int ; st_size : int ; st_atime : float ; st_mtime : float ; st_ctime : float }
let realpath p = let cleanup p = if String . length p <= 4 then p else if p . [ 0 ] = ' ' \\ && p . [ 1 ] = ' ' \\ && p . [ 2 ] = ' ' ? && p . [ 3 ] = ' ' \\ then ( String . sub p 4 ( String . length p - 4 ) ) else p in try cleanup ( realpath p ) with | ( Unix_error ( EACCES , _ , _ ) ) as e -> try let dir = cleanup ( realpath ( Filename . dirname p ) ) in Filename . concat dir ( Filename . basename p ) with _ -> raise e
module LargeFile = struct external lseek : file_descr -> int64 -> seek_command -> int64 = " unix_lseek_64 " external truncate : string -> int64 -> unit = " unix_truncate_64 " external ftruncate : file_descr -> int64 -> unit = " unix_ftruncate_64 " type stats = { st_dev : int ; st_ino : int ; st_kind : file_kind ; st_perm : file_perm ; st_nlink : int ; st_uid : int ; st_gid : int ; st_rdev : int ; st_size : int64 ; st_atime : float ; st_mtime : float ; st_ctime : float ; } external stat : string -> stats = " unix_stat_64 " external lstat : string -> stats = " unix_lstat_64 " external fstat : file_descr -> stats = " unix_fstat_64 " end file_descr -> ( ' a , ' b ) Stdlib . Bigarray . kind -> ' c Stdlib . Bigarray . layout -> bool -> int array -> int64 -> ( ' a , ' b , ' c ) Stdlib . Bigarray . Genarray . t = " caml_unix_map_file_bytecode " " caml_unix_map_file "
let map_file fd ( ? pos = 0L ) kind layout shared dims = map_internal fd kind layout shared dims pos
type access_permission = R_OK | W_OK | X_OK | F_OK
let fchmod _fd _perm = invalid_arg " Unix . fchmod not implemented "
let chown _file _perm = invalid_arg " Unix . chown not implemented "
let fchown _fd _perm = invalid_arg " Unix . fchown not implemented "
let umask _msk = invalid_arg " Unix . umask not implemented " ? cloexec : bool -> file_descr -> file_descr -> unit = " unix_dup2 "
let chroot _ = invalid_arg " Unix . chroot not implemented "
type dir_entry = Dir_empty | Dir_read of string | Dir_toread
type dir_handle = { dirname : string ; mutable handle : int ; mutable entry_read : dir_entry }
let opendir dirname = try let ( first_entry , handle ) = findfirst ( Filename . concat dirname " . " ) ** in { dirname = dirname ; handle = handle ; entry_read = Dir_read first_entry } with End_of_file -> { dirname = dirname ; handle = 0 ; entry_read = Dir_empty }
let readdir d = match d . entry_read with Dir_empty -> raise End_of_file | Dir_read name -> d . entry_read <- Dir_toread ; name | Dir_toread -> findnext d . handle
let closedir d = match d . entry_read with Dir_empty -> ( ) | _ -> win_findclose d . handle
let rewinddir d = closedir d ; try let ( first_entry , handle ) = findfirst ( d . dirname ^ " . " ) \\** in d . handle <- handle ; d . entry_read <- Dir_read first_entry with End_of_file -> d . handle <- 0 ; d . entry_read <- Dir_empty ? cloexec : bool -> unit -> file_descr * file_descr = " unix_pipe "
let mkfifo _name _perm = invalid_arg " Unix . mkfifo not implemented "
let normalize_slashes path = if String . length path >= 4 && path . [ 0 ] = ' ' \\ && path . [ 1 ] = ' ' \\ && path . [ 2 ] = ' ' ? && path . [ 3 ] = ' ' \\ then path else String . init ( String . length path ) ( fun i -> match path . [ i ] with ' ' / -> ' ' \\ | c -> c )
let symlink ? to_dir source dest = let to_dir = match to_dir with Some to_dir -> to_dir | None -> try LargeFile . ( ( stat source ) . st_kind = S_DIR ) with _ -> false in let source = normalize_slashes source in symlink_stub to_dir source dest
type lock_command = F_ULOCK | F_LOCK | F_TLOCK | F_TEST | F_RLOCK | F_TRLOCK
let kill pid signo = if signo <> Sys . sigkill then invalid_arg " Unix . kill " else if not ( terminate_process pid ) then raise ( Unix_error ( ESRCH , " kill " , " " ) )
type sigprocmask_command = SIG_SETMASK | SIG_BLOCK | SIG_UNBLOCK
let sigprocmask _cmd _sigs = invalid_arg " Unix . sigprocmask not implemented "
let sigpending ( ) = invalid_arg " Unix . sigpending not implemented "
let sigsuspend _sigs = invalid_arg " Unix . sigsuspend not implemented "
let pause ( ) = invalid_arg " Unix . pause not implemented "
type process_times = { tms_utime : float ; tms_stime : float ; tms_cutime : float ; tms_cstime : float }
type tm = { tm_sec : int ; tm_min : int ; tm_hour : int ; tm_mday : int ; tm_mon : int ; tm_year : int ; tm_wday : int ; tm_yday : int ; tm_isdst : bool } " unix_time " " unix_time_unboxed " [ @@ noalloc ] " unix_gettimeofday " " unix_gettimeofday_unboxed " [ @@ noalloc ]
let alarm _n = invalid_arg " Unix . alarm not implemented "
let sleep n = sleepf ( float n )
type interval_timer = ITIMER_REAL | ITIMER_VIRTUAL | ITIMER_PROF
type interval_timer_status = { it_interval : float ; it_value : float }
let getitimer _it = invalid_arg " Unix . getitimer not implemented "
let setitimer _it _tm = invalid_arg " Unix . setitimer not implemented "
let getuid ( ) = 1
let setuid _id = invalid_arg " Unix . setuid not implemented "
let getgid ( ) = 1
let setgid _id = invalid_arg " Unix . setgid not implemented "
let getgroups ( ) = [ | 1 ] |
let setgroups _ = invalid_arg " Unix . setgroups not implemented "
let initgroups _ _ = invalid_arg " Unix . initgroups not implemented "
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 }
let getlogin ( ) = try Sys . getenv " USERNAME " with Not_found -> " "
let getpwnam _x = raise Not_found
let is_inet6_addr s = String . length s = 16 = " unix_inet_addr_of_string " = " unix_string_of_inet_addr "
let inet_addr_any = inet_addr_of_string " 0 . 0 . 0 . 0 "
let inet_addr_loopback = inet_addr_of_string " 127 . 0 . 0 . 1 "
let inet6_addr_any = try inet_addr_of_string " " :: with Failure _ -> inet_addr_any
let inet6_addr_loopback = try inet_addr_of_string " :: 1 " with Failure _ -> inet_addr_loopback
type socket_domain = PF_UNIX | PF_INET | PF_INET6
type socket_type = SOCK_STREAM | SOCK_DGRAM | SOCK_RAW | SOCK_SEQPACKET
type sockaddr = ADDR_UNIX of string | ADDR_INET of inet_addr * int