Stdlib.Sys(3)

🌸 Stdlib.Sys 🌸

okay bestie, welcome to your OCaml library era β€” this is the <pink>Stdlib.Sys</pink> page, i.e. the section 3 manual for the Sys module. it's giving "everything your program needs to know about the world it's living in" and honestly? iconic. πŸ’…


✨ Module ✨

πŸŽ€ Module Sys

: (module Stdlib__Sys)

like, that's the whole vibe. it's a module. it's Stdlib__Sys. moving on, bestie, because there's a LOT. 🫢


πŸ’– Documentation πŸ’–

  • val argv : string array β€” the command-line arguments the process got, fr fr. element zero is the command name used to invoke the program, and everything after that is the actual command-line args. main character is argv.(0), no cap. ⭐
  • val executable_name : string β€” the name of the file containing the executable that's currently running. it may be absolute or relative to the current directory β€” depends on the platform and on whether the program was compiled to bytecode or to a native executable. so like… don't assume, okay? πŸ™ˆ
  • val runtime_executable : string β€” the name of the file containing the runtime that's currently running. for native code this is always Sys.executable_name, but in bytecode it may be ocamlrun, for example. this name may be absolute or relative to the current directory depending on the platform (Linux, Windows and macOS should all return absolute paths, slay). πŸ’«
    • (Since 5.5)
  • val file_exists : string -> bool β€” test if a file with the given name exists. that's it. that's the whole function. vibe check passed. βœ…
  • val is_directory : string -> bool β€” returns true if the given name refers to a directory, false if it refers to another kind of file. πŸ—‚οΈ
    • (Since 3.10)
    • Raises Sys_error if no file exists with the given name. 😭
  • val is_regular_file : string -> bool β€” returns true if the given name refers to a regular file, false if it refers to another kind of file. πŸ“
    • (Since 5.1)
    • Raises Sys_error if no file exists with the given name. 😭
  • val remove : string -> unit β€” remove the given file name from the file system. gone. deleted. not me being the one to do it though. πŸ’€
  • val rename : string -> string -> unit β€” rename a file or directory. rename oldpath newpath renames the file or directory called oldpath, giving it newpath as its new name, moving it between (parent) directories if needed. if a file named newpath already exists, its contents will be replaced with those of oldpath. depending on the operating system, the metadata (permissions, owner, etc.) of newpath can either be preserved or be replaced by those of oldpath β€” so like, platform-dependent, bestie, be aware. πŸ”₯
    • (Since 4.06 concerning the "replace existing file" behavior)
  • val getenv : string -> string β€” return the value associated to a variable in the process environment. 🌈
    • Raises Not_found if the variable is unbound. 😭
  • val getenv_opt : string -> string option β€” same energy, but returns the value associated to a variable in the process environment or None if the variable is unbound. no exceptions, just options. we love a safe queen. πŸ‘‘
    • (Since 4.05)
  • val command : string -> int β€” execute the given shell command and return its exit code. πŸš€

the argument of Sys.command is generally the name of a command followed by zero, one or several arguments, separated by whitespace. the given argument is interpreted by a shell: either the Windows shell cmd.exe for the Win32 ports of OCaml, or the POSIX shell sh for other ports. it can contain shell builtin commands such as echo, and also special characters such as file redirections > and <, which will be honored by the shell. ✨

conversely, whitespace or special shell characters occurring in command names or in their arguments must be quoted or escaped so that the shell does not interpret them. the quoting rules vary between the POSIX shell and the Windows shell. Filename.quote_command performs the appropriate quoting given a command name, a list of arguments, and optional file redirections. bestie, use it. 😀

  • val time : unit -> float β€” return the processor time, in seconds, used by the program since the beginning of execution. ⏱️
  • val chdir : string -> unit β€” change the current working directory of the process. we're moving, girlies. πŸ¦‹
  • val mkdir : string -> int -> unit β€” create a directory with the given permissions. πŸ“
    • (Since 4.12)
  • val rmdir : string -> unit β€” remove an empty directory. oh and it has to be empty, okay? don't come for me. πŸ’…
    • (Since 4.12)
  • val getcwd : unit -> string β€” return the current working directory of the process. where are we? who are we? 🌸
  • val 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. so like… don't sort-shame. 😭
    • (Since 4.12)
  • val io_buffer_size : int β€” size of C buffers used by the runtime system and IO primitives of the unix library. primitives that read from or write to values of type string or bytes generally use an intermediate buffer of this size to avoid holding the domain lock. iconic efficiency behavior. 🧚
    • (Since 5.4)
  • val interactive : bool 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. πŸŽ€
⚠️ Alert unsynchronized_access. the interactive status is a mutable global state. be careful out there. πŸ’€
  • val os_type : string β€” the operating system currently executing the OCaml program. one of:
    • "Unix" (for all Unix versions, including Linux and Mac OS X), 🌈
    • "Win32" (for MS-Windows, OCaml compiled with MSVC++ or MinGW-w64), πŸͺŸ
    • "Cygwin" (for MS-Windows, OCaml compiled with Cygwin). 🐧
  • type backend_type = β€” the moment you've been waiting for, a whole type:
type backend_type =
 | Native
 | Bytecode
 | Other of string

currently, the official distribution only supports Native and Bytecode, but it can be other backends with alternative compilers, for example, javascript. πŸ¦‹

  • (Since 4.04)
  • val backend_type : backend_type β€” backend type currently executing the OCaml program. πŸ‘‘
    • (Since 4.04)
  • val unix : bool β€” true if Sys.os_type = "Unix". 🐚
    • (Since 4.01)
  • val win32 : bool β€” true if Sys.os_type = "Win32". πŸͺŸ
    • (Since 4.01)
  • val cygwin : bool β€” true if Sys.os_type = "Cygwin". 🐧
    • (Since 4.01)
  • val word_size : int β€” size of one word on the machine currently executing the OCaml program, in bits: 32 or 64. ⚑
  • val int_size : int β€” size of int, in bits. it is 31 (resp. 63) when using OCaml on a 32-bit (resp. 64-bit) platform. it may differ for other implementations, e.g. it can be 32 bits when compiling to JavaScript. 🀯
    • (Since 4.03)
  • val big_endian : bool β€” whether the machine currently executing the Caml program is big-endian. 🐘
    • (Since 4.00)
  • val max_string_length : int β€” maximum length of strings and byte sequences. πŸ“
  • val max_array_length : int β€” maximum length of a normal array (i.e. any array whose elements are not of type float). the maximum length of a float array is max_floatarray_length if OCaml was configured with --enable-flat-float-array and max_array_length if configured with --disable-flat-float-array. yes, this is real, yes you need both numbers. 🌸
  • val max_floatarray_length : int β€” maximum length of a floatarray. this is also the maximum length of a float array when OCaml is configured with --enable-flat-float-array. πŸ“
  • val runtime_variant : unit -> string β€” return the name of the runtime variant the program is running on. this is normally the argument given to -runtime-variant at compile time, but for byte-code it can be changed after compilation. πŸ’«
    • (Since 4.03)
  • val runtime_parameters : unit -> string β€” return the value of the runtime parameters, in the same format as the contents of the OCAMLRUNPARAM environment variable. πŸ”§
    • (Since 4.03)
  • val poll_actions : unit -> unit β€” run any pending runtime actions, such as minor collections, major GC slices, signal handlers, finalizers, or memprof callbacks. like a little coffee break for the runtime, except productive. β˜•
    • (Since 5.3)

πŸ¦‹ Signal handling πŸ¦‹

  • type signal = int β€” the type for signal numbers. πŸ””

negative numbers are used by OCaml to provide a platform-independent number for signals recognised by OCaml. positive numbers are always the platform-dependent value for a given signal. the function Sys.signal_of_int converts known platform-dependent numbers to independent ones, and Sys.signal_to_int does the reverse. iykyk. πŸ’…

  • (Since 5.4)
  • type signal_behavior = β€” what to do when receiving a signal:
type signal_behavior =
 | Signal_default
 | Signal_ignore
 | Signal_handle of (signal -> unit)
  • Signal_default: take the default behavior (usually: abort the program). 😭 dramatic but fair.
  • Signal_ignore: ignore the signal. not me ignoring my problems. πŸ™ˆ
  • Signal_handle f: call function f, giving it the signal number as an argument. 🫢
  • val signal : signal -> signal_behavior -> signal_behavior β€” set the behavior of the system on receipt of a given signal. the first argument is the signal number. return the behavior previously associated with the signal. if the signal number is invalid (or not available on your system), an Invalid_argument exception is raised. 🚨

if a platform-dependent signal number is used, it will be converted to a platform-independent signal using Sys.signal_of_int before calling the handler. ✨

  • val set_signal : signal -> signal_behavior -> unit β€” same as Sys.signal but the return value is ignored. lowkey the "i don't care what you were before, we're changing" version. πŸ’…

🌈 Signal numbers for the standard POSIX signals. 🌈

  • val sigabrt : signal β€” abnormal termination πŸ’€
  • val sigalrm : signal β€” timeout ⏰
  • val sigfpe : signal β€” arithmetic exception βž—
  • val sighup : signal β€” hangup on controlling terminal πŸ“΄
  • val sigill : signal β€” invalid hardware instruction ⚠️
  • val sigint : signal β€” interactive interrupt (ctrl-C) ⌨️
  • val sigkill : signal β€” termination (cannot be ignored) ☠️
  • val sigpipe : signal β€” broken pipe 🚰
  • val sigquit : signal β€” interactive termination πŸ›‘
  • val sigsegv : signal β€” invalid memory reference πŸ’₯
  • val sigterm : signal β€” termination πŸ”š
  • val sigusr1 : signal β€” application-defined signal 1 πŸŽ€
  • val sigusr2 : signal β€” application-defined signal 2 πŸŽ€
  • val sigchld : signal β€” child process terminated πŸ‘Ά
  • val sigcont : signal β€” continue ▢️
  • val sigstop : signal β€” stop (cannot be caught or ignored) πŸ›‘
  • val sigtstp : signal β€” interactive stop ⏸️
  • val sigttin : signal β€” terminal read from background process πŸ“–
  • val sigttou : signal β€” terminal write from background process ✍️
  • val sigvtalrm : signal β€” timeout in virtual time ⏳
  • val sigprof : signal β€” profiling interrupt πŸ“Š
  • val sigbus : signal β€” bus error 🚌
    • (Since 4.03)
  • val sigpoll : signal β€” pollable event πŸ“‘
    • (Since 4.03)
  • val sigsys : signal β€” bad argument to routine πŸ™ƒ
    • (Since 4.03)
  • val sigtrap : signal β€” trace/breakpoint trap πŸͺ€
    • (Since 4.03)
  • val sigurg : signal β€” urgent condition on socket 🚨
    • (Since 4.03)
  • val sigxcpu : signal β€” timeout in cpu time πŸ–₯️
    • (Since 4.03)
  • val sigxfsz : signal β€” file size limit exceeded πŸ“¦
    • (Since 4.03)
  • val sigio : signal β€” I/O is possible on a descriptor πŸ“¬
    • (Since 5.4)
  • val sigwinch : signal β€” window size change πŸͺŸ
    • (Since 5.4)
  • val signal_to_string : signal -> string β€” signal_to_string formats an OCaml signal as a C POSIX constant or "SIG(%d)" for platform-dependent signal numbers. πŸŽ€
πŸŽ€ (finishing the one above, bestie) β€” Since 5.4. Raises Invalid_argument for unrecognised negative numbers.
  • val signal_of_int : int -> signal β€” signal_of_int n turns a platform-dependent signal number n into an OCaml signal number ✨
    • For a positive n, that's just n itself β€” unless OCaml doesn't have a platform-independent signal number for n, in which case, well, it does its best.
    • Since 5.4
    • Raises Invalid_argument if n is negative. No negative signal numbers for you πŸ™…β€β™€οΈ
  • val signal_to_int : signal -> int β€” the other direction, babes: signal_to_int n converts an OCaml signal number n into a platform-dependent signal number.
    • For a positive n this is literally n itself. Iconic, effortless, no notes πŸ’…
    • Since 5.4
    • Raises Invalid_argument for unrecognised negative numbers.
  • exception Break β€” the exception that gets raised on an interactive interrupt, but only if Sys.catch_break is enabled. She's conditional, she's dramatic 😭
  • val catch_break : bool -> unit β€” catch_break is the switch that decides whether an interactive interrupt (Ctrl+C) kills your program or raises the Break exception instead 🫢
    • Call catch_break true to enable raising Break.
    • Call catch_break false to let the system terminate the program on a user interrupt.
    • Inside multi-threaded programs, the Break exception will arise in any one of the active threads β€” and it'll keep arising on further interactive interrupts until all threads are terminated πŸ’€ Use signal masks from Thread.sigmask to direct the interrupt towards a specific thread, iykyk.
  • val ocaml_version : string β€” ocaml_version is the version of OCaml, as a string shaped exactly like "major.minor[.patchlevel][(+|~)additional-info]" 🌸
    • major, minor, and patchlevel are integers.
    • additional-info is an arbitrary string.
    • The [.patchlevel] part was absent before version 3.08.0, and became mandatory from 3.08.0 onwards. Glow-up era ✨
    • The [(+|~)additional-info] part may be absent.
  • val development_version : bool β€” true if this is a development version, false otherwise. A little vibe check on your compiler πŸ¦‹
    • Since 4.14
  • type extra_prefix = πŸŽ€
    • | Plus
    • | Tilde
    • Since 4.14
  • type extra_info = extra_prefix * string β€” a prefix plus a string, that's the whole deal, no cap.
    • Since 4.14
  • type ocaml_release_info = {
    • major : int ;
    • minor : int ;
    • patchlevel : int ;
    • extra : extra_info option ;
    • }
    • Since 4.14
  • val ocaml_release : ocaml_release_info β€” ocaml_release is the version of OCaml, but make it structured πŸ‘‘ (all the same info as ocaml_version, just sliced up into fields).
    • Since 4.14
  • val enable_runtime_warnings : bool -> unit β€” controls whether the OCaml runtime system can emit warnings on stderr πŸ”₯
    • Currently, the only supported warning is the one triggered when a channel created by open_* functions is finalized without being closed. (Yes, she noticed. She always notices. πŸ™ˆ)
    • Runtime warnings are disabled by default.
    • Since 4.03
⚠️ Alert unsynchronized_access. The status of runtime warnings is a mutable global state. So like… be normal about it in multithreaded code, bestie 😭
  • val runtime_warnings_enabled : unit -> bool β€” returns whether runtime warnings are currently enabled. The vibe check for the setting above πŸ’–
    • Since 4.03
⚠️ Alert unsynchronized_access. The status of runtime warnings is a mutable global state. Same warning, same energy.

⚑ Optimization ⚑

  • val opaque_identity : 'a -> 'a β€” for the purposes of optimization, opaque_identity behaves like an unknown function, and thus a possibly side-effecting one. Sneaky on purpose πŸ’…
    • At runtime, opaque_identity disappears altogether. Poof. Gone. ✨
    • However, it does prevent the argument from being garbage collected until the location where the call would have occurred.
    • 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
  • Since 4.03
  • module Immediate64 : sig end β€” that's the whole declaration. An empty signature. She's mysterious, she contains multitudes, she says nothing else in this page 🌈

And that's the tea, bestie β€” that's the whole page, front to back, nothing skipped πŸ«ΆπŸ’œ This little beauty is part of the standard library docs, rendered by OCamldoc, dated 2026-06-22, filed under Stdlib.Sys(3). Go forth and signal responsibly πŸ’…πŸŽ€

πŸ“¦ where this page comes from