std.os

Environment, platform, host, and signal information.

Importuse std.os

Types

struct Signals#

A POSIX signal subscription from std.os.signals.

An opaque handle: only std.os can produce one, and its contents are not reachable from moon source.

_handle: NativeHandle

The native resource.

Functions

extern fn entropy(count: int) -> [int]#

Returns n fresh CSPRNG bytes (0..255); the maximum request is 1 MiB.

  • count: How many bytes to draw; at most 1 MiB.
println(std.os.entropy(16).len())
println(std.os.getcwd())
println(std.os.temp_dir())
println(std.os.platform() + "/" + std.os.arch())
println(std.os.hostname())
println(std.os.time_ms())
extern fn getcwd() -> string#

Returns the current working directory.

extern fn executable_path() -> Result<string, Error>#

Returns the absolute path of the running executable, independently of the working directory and command-line spelling. A compiled moon application can resolve installed assets relative to this path. In an embedded host, this identifies the host executable. Reports an I/O error when unavailable.

extern fn time_ms() -> int#

Returns wall-clock Unix time in milliseconds.

extern fn getenv(name: string) -> string#

Returns an environment variable, or an empty string when absent.

  • name: Variable to read.
std.os.setenv("MOON_MODE", "tour")
println(std.os.has_env("MOON_MODE"))
println(std.os.getenv("MOON_MODE"))
println(std.os.getenv_or("MISSING", "development"))
std.os.unsetenv("MOON_MODE")
extern fn getenv_or(name: string, default: string) -> string#

Returns an environment variable or the supplied fallback.

  • name: Variable to read.
  • default: Value returned when the variable is unset.
extern fn has_env(name: string) -> bool#

Reports whether an environment variable exists.

  • name: Variable to test for.
extern fn setenv(name: string, value: string) -> bool#

Sets an environment variable, returning false on failure.

  • name: Variable to set.
  • value: Value to store.
extern fn unsetenv(name: string) -> bool#

Removes an environment variable, returning false on failure.

  • name: Variable to remove.
extern fn platform() -> string#

Returns the target operating-system name.

extern fn arch() -> string#

Returns the target architecture name.

extern fn hostname() -> string#

Returns the local host name.

extern fn temp_dir() -> string#

Returns the platform temporary-directory path.

extern fn signals(names: [string]) -> Signals#

Subscribes to POSIX signals by name (int, term, hup, usr1, usr2); every active subscription for a name receives each delivery. A second SIGINT while subscriptions exist still hard-kills.

  • names: Signal names to subscribe to: int, term, hup, usr1, usr2.
async fn wait_for_shutdown() {
  let subscription = std.os.signals(["int", "term"])
  let signal = await subscription.next()
  println("received " + signal)
  subscription.close()
}

async fn main() { await wait_for_shutdown() }
extern fn next(s: Signals) -> Future<string>#

Returns a future for the next delivered signal name; resolves with an empty string once the subscription is closed.

  • s: Subscription to take the next signal from.
extern fn close(s: Signals)#

Removes this subscriber and wakes its pending next calls. The previous process disposition is restored after the final subscriber closes. Repeated close calls are no-ops.

  • s: Subscription to end, restoring prior dispositions when it was last.