os

tape
import os from "os";
import io from "io";

for (a in os.args()) {
    io.println(a);                      // the program first, then its arguments
}
let home: ?string = os.env("HOME");
if (os.PLATFORM == os.Platform.Win64) { io.println("on Windows"); }

os is the running program and what surrounds it. It is a capability module.

Arguments

FunctionReturns
arg_count() -> i64The number of arguments, the program’s name included
arg(i: i64) -> ?[]const u8Argument i, or null when there is none
arg_list() -> []const []const u8All of them as one slice
args() -> ArgIterAn iterator for for (a in os.args())

Argument 0 is the program: the executable when you tape build it, the .tape file under tape run. Pass arguments to a program run in the VM after --:

plaintext
tape run tool.tape -- input.txt --verbose

The returned views stay valid for the whole run.

Environment

FunctionDoes
env(name) -> ?stringThe variable’s value, or null when it isn’t set
set_env(name, value) -> void or io.IoErrorSet it for this process and the processes it starts

An empty value is "", not null. A name can’t be empty or contain = (set_env reports InvalidInput).

The process

FunctionDoes
exit(code: i64)End the program now with code
cwd() -> string or io.IoErrorThe working directory
exe_path() -> string or io.IoErrorThe running executable’s absolute path, symlinks resolved (on Windows the path it started from; under tape run, tape’s own)
exe_dir() -> string or io.IoErrorThe directory exe_path() is in: where a program finds the files it ships with when it is double-clicked, or ../Resources from it in a macOS .app
chdir(path) -> void or io.IoErrorChange the working directory
pid() -> i64The process id
cpu_count() -> i64How many logical processors (hardware threads) the program may use, at least 1
random_bytes(dst: []u8) -> void or io.IoErrorFill dst from the OS’s secure random source

exit stops at once: defer blocks don’t run and buffered writers aren’t flushed, so close your BufWriters first. Returning from main is the usual way to finish.

random_bytes is for seeds, keys and nonces:

tape
var seed: [8]u8;
os.random_bytes(seed[0..8]) or { return 1; };

Platform

tape
pub enum Platform : u8 { Win64 = 0; Linux = 1; MacOS = 2; Uusi = 3; }
pub enum Arch : u8 { X64 = 0; Arm64 = 1; }
pub const PLATFORM: Platform;
pub const ARCH: Arch;

PLATFORM and ARCH are the platform the program was compiled for: the --target of tape build, or the machine itself under tape run. They are constants, so code that tests them costs nothing at run time.

Deprecated

args_count() still works and will be removed in a later release; use arg_count(). arg(i) used to return a C string (*u8); it now returns a ?[]const u8 view, so cstr.as_view(os.arg(i)) becomes os.arg(i)!.

Platforms

All of os works on Windows, Linux and macOS. On Uusi (Leivos) the arguments are the ones the program was started with (argument 0 its name; none for a program started without them). There is no environment there yet: env is null, pid() is -1, and set_env, cwd and chdir, exe_path and exe_dir are Unsupported. random_bytes asks the kernel (its random system call).

Last modified: