package core_unix

  1. Overview
  2. Docs
Unix-specific portions of Core

Install

Dune Dependency

Authors

Maintainers

Sources

v0.14.0.tar.gz
md5=f9a74834f239874286d84ec99d75e5fa
sha512=d020db759cde35c0e9d9919dee2c0ea5bb5b7a5ee75515be922d816f28eb9f74dba37e6e424a636e362eab5120b2c1e672f4e5ba798f2dac7974c0e135f80faf

doc/core_unix.sys_unix/Sys_unix/index.html

Module Sys_unixSource

include module type of struct include Core.Sys end
Sourceval get_argv : unit -> string array

The command line arguments given to the process. The first element is the command name used to invoke the program. The following elements are the command-line arguments given to the program.

get_argv is a function because the external function caml_sys_modify_argv can replace the array starting in OCaml 4.09.

Sourceval argv : string array

A single result from get_argv (). This value is indefinitely deprecated. It is kept for compatibility with Caml.Sys.

  • deprecated [since 2019-08] Use [Sys.get_argv] instead, which has the correct behavior when [caml_sys_modify_argv] is called.
Sourceval executable_name : string

The name of the file containing the executable currently running.

For all of the following functions, ?follow_symlinks defaults to true.

Sourceval file_exists : ?follow_symlinks:bool -> string -> [ `Yes | `No | `Unknown ]

file_exists ~follow_symlinks path

Test whether the file in path exists on the file system. If follow_symlinks is true and path is a symlink the result concerns the target of the symlink.

`Unknown is returned for files for which we cannot successfully determine whether they are on the system or not (e.g. files in directories to which we do not have read permission).

Sourceval file_exists_exn : ?follow_symlinks:bool -> string -> bool

Same as file_exists but blows up on `Unknown

Sourceval is_directory : ?follow_symlinks:bool -> string -> [ `Yes | `No | `Unknown ]

Returns `Yes if the file exists and is a directory

Sourceval is_file : ?follow_symlinks:bool -> string -> [ `Yes | `No | `Unknown ]

Returns `Yes if the file exists and is a regular file

Sourceval is_directory_exn : ?follow_symlinks:bool -> string -> bool
Sourceval is_file_exn : ?follow_symlinks:bool -> string -> bool
Sourceval remove : string -> unit

Remove the given file name from the file system.

Sourceval rename : string -> string -> unit

Rename a file. The first argument is the old name and the second is the new name. If there is already another file under the new name, rename may replace it, or raise an exception, depending on your operating system.

Sourceval getenv : string -> string option

Return the value associated to a variable in the process environment unless the process has special privileges. Return None if the variable is unbound or the process has special privileges.

Sourceval getenv_exn : string -> string
Sourceval unsafe_getenv : string -> string option

Return the value associated to a variable in the process environment.

Unlike getenv, this function returns the value even if the process has special privileges. It is considered unsafe because the programmer of a setuid or setgid program must be careful to avoid using maliciously crafted environment variables in the search path for executables, the locations for temporary files or logs, and the like.

Sourceval unsafe_getenv_exn : string -> string
Sourceval command : string -> int

Execute the given shell command and return its exit code.

Sourceval command_exn : string -> unit

command_exn command runs command and then raises an exception if it returns with nonzero exit status.

Sourceval quote : string -> string

quote s quotes the string in a format suitable for the shell of the current system (e.g. suitable for command). On Unix, this function only quotes as necessary, which makes its output more legible than Filename.quote.

WARNING: This may not work with some shells, but should work with sh, bash, and zsh.

Sourceval chdir : string -> unit

Change the current working directory of the process.

Sourceval getcwd : unit -> string

Return the current working directory of the process.

Sourceval readdir : string -> string array

Return the names of all files present in the given directory. Names denoting the current directory and the parent directory ("." and ".." in Unix) are not returned. Each string in the result is a file name rather than a complete path. There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Sourceval fold_dir : init:'acc -> f:('acc -> string -> 'acc) -> string -> 'acc

Call readdir, and fold over the elements of the array.

  • raises Sys_error

    _ if readdir fails. As with readdir, "." and ".." are not returned raises the same exception than opendir and closedir.

Sourceval ls_dir : string -> string list

Same as readdir, but return a list rather than an array.

Sourceval interactive : bool Caml.ref

This reference is initially set to false in standalone programs and to true if the code is being executed under the interactive toplevel system ocaml.

Sourceval os_type : string

Operating system currently executing the Caml program. One of

  • "Unix" (for all Unix versions, including Linux and Mac OS X),
  • "Win32" (for MS-Windows, OCaml compiled with MSVC++ or Mingw),
  • "Cygwin" (for MS-Windows, OCaml compiled with Cygwin).
Sourceval word_size : int

Size of one word on the machine currently executing the Caml program, in bits: 32 or 64.

Sourceval int_size : int

Size of an int. It is 31 bits (resp. 63 bits) when using the OCaml compiler on a 32 bits (resp. 64 bits) platform. It may differ for other compilers, e.g. it is 32 bits when compiling to JavaScript. This is the same as Core.Int.size_in_bits.

  • since

4.03.0

Sourceval big_endian : bool

Whether the machine currently executing the Caml program is big-endian.

Sourceexception Break

Exception raised on interactive interrupt if Sys.catch_break is on.

Sourceval catch_break : bool -> unit

Warning: this function clobbers the Signal.int (SIGINT) handler. SIGINT is the signal that's sent to your program when you hit CTRL-C.

Warning: catch_break uses deep ocaml runtime magic to raise Sys.Break inside of the main execution context. Consider explicitly handling Signal.int instead. If all you want to do is terminate on CTRL-C you don't have to do any special setup, that's the default behavior.

catch_break governs whether interactive interrupt (ctrl-C) terminates the program or raises the Break exception. Call catch_break true to enable raising Break, and catch_break false to let the system terminate the program on user interrupt.

Sourceval ocaml_version : string

ocaml_version is the version of Objective Caml. It is a string of the form "major.minor[.patchlevel][+additional-info]", where major, minor, and patchlevel are integers, and additional-info is an arbitrary string. The [.patchlevel] and [+additional-info] parts may be absent.

Sourceval execution_mode : unit -> [ `Bytecode | `Native ]

execution_mode tests whether the code being executed was compiled natively or to bytecode.

Sourceval c_int_size : unit -> int

c_int_size returns the number of bits in a C int, as specified in header files. Note that this can be different from word_size and Nativeint.num_bits. For example, Linux x86-64 should have word_size = 64, but c_int_size () = 32.

Sourceval home_directory : unit -> string

Return the home directory, using the HOME environment variable if that is defined, and if not, using the effective user's information in the Unix password database.

Optimization
Sourceval opaque_identity : 'a -> 'a

For the purposes of optimization, opaque_identity behaves like an unknown (and thus possibly side-effecting) function. At runtime, opaque_identity disappears altogether. A typical use of this function is to prevent pure computations from being optimized away in benchmarking loops. For example:

  for _round = 1 to 100_000 do
    ignore (Sys.opaque_identity (my_pure_computation ()))
  done
Sourceval override_argv : string array -> unit

override_argv new_argv makes subsequent calls to get_argv return new_argv.

Prior to OCaml version 4.09, this function has two noteworthy behaviors:

  • it may raise if the length of new_argv is greater than the length of argv before the call;
  • it re-uses and mutates the previous argv value instead of using the new one; and
  • it even mutates its length, which can be observed by inspecting the array returned by an earlier call to get_argv.
OCaml

Innovation. Community. Security.