Skip to content

Env

Source: std/env.mw.

Command-line arguments and environment variables. Call env_init from main first:

@export fn main(argc: i32, argv: rawptr) -> i32 {
env_init(argc, argv);
... arg_get(1) ... env_get("HOME") ...

The environment block is read from the stack layout every System V style entry point provides (envp follows argv), so this works on Linux and macOS without libc. Windows keeps its environment elsewhere: there env_get returns null.

Name Type Value Notes
ENV_ARGC i64 0
ENV_ARGV i64 0
ENV_OK i64 0
fn env_init(argc: i32, argv: rawptr)
fn arg_count() -> i64
fn arg_get(i: i64) -> *u8

The i-th argument (0 is the program name), or null when out of range.

fn env_block() -> *i64
fn env_count() -> i64

Number of “NAME=value” entries.

fn env_at(i: i64) -> *u8

The i-th “NAME=value” entry, or null.

fn env_get(name: *u8) -> *u8

Value of the variable (a pointer into the environment block), or null.

fn env_get_or(name: *u8, fallback: *u8) -> *u8

Like env_get but returns fallback when the variable is not set.