cheatah
Module

os

Python-like operating-system interface, built on std::filesystem. Includes the os.path path-manipulation submodule.

import os

os.getcwd()
os.makedirs("build/cache")
os.path.join("a", "b", "c")          # "a/b/c"
os.path.splitext("dir/file.purr")    # {"dir/file", ".purr"}

Functions

Working directory & process:

  • getcwd(), chdir(path) — read / change the cwd.

  • getpid(), cpu_count(), system(command) — process id, logical CPU count, run a shell command.

  • urandom(n)n cryptographically secure random bytes (from the OS CSPRNG).

Directories & files:

  • listdir(path=".") — entry basenames in a directory.

  • mkdir(path), makedirs(path) — create one / a directory tree.

  • rmdir(path), remove(path), rename(src, dst) — remove / move entries.

Environment:

  • getenv(name, fallback=""), setenv(name, value, overwrite=true).

os.path submodule:

  • join(first, ...) — join components with the platform separator.

  • exists(p), isfile(p), isdir(p) — path predicates.

  • basename(p), dirname(p), abspath(p), normpath(p) — path components.

  • getsize(p) — file size in bytes.

  • splitext(p) — split into {root, extension}.

Per-function docs (parameters, runtime complexity, heap behavior) are in os.hpp. Tested in ../tests/os_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).

Functions

fn std::string getcwd() source#

Current working directory.

Queries the process's current directory via std::filesystem::current_path and returns it as an absolute path string.

Returns

the absolute cwd.

Complexity

O(n) + a syscall.

Allocation

allocates the result string.

Compile-run testOsCompileRun.Getcwd
System testStdlibE2E.Os
Performance375 ns/call in cheatah · 332 ns/call in CPython 3.12.3 · ≈1.1× slower
fn void chdir(const std::string &path) source#

Change the working directory.

Sets the process's current directory; subsequent relative paths resolve against it. Throws if path does not exist or is not a directory.

Parameters
path

the target directory.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.Chdir
System testStdlibE2E.Os
Performancefilesystem/process syscall — not micro-benchmarked
fn std::vector< std::string > listdir(const std::string &path=".") source#

List a directory's entries (basenames only).

Iterates path and returns each entry's filename component (not a full path), in unspecified order; . and .. are not included. Throws if path does not exist or is not a directory.

Parameters
path

the directory (default .).

Returns

the entry names.

Complexity

O(entries) + syscalls.

Allocation

allocates a vector of strings.

Compile-run testOsCompileRun.Listdir
System testStdlibE2E.Os
Performancefilesystem/process syscall — not micro-benchmarked
fn void mkdir(const std::string &path) source#

Create a single directory.

Creates the leaf directory only; the parent must already exist (use makedirs to create missing parents). Does nothing if path already exists as a directory.

Parameters
path

the directory to create.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.Mkdir
System testStdlibE2E.Os
Performancefilesystem/process syscall — not micro-benchmarked
fn void makedirs(const std::string &path) source#

Create a directory and any missing parents.

Creates path along with every intermediate directory that does not yet exist. Succeeds without error if the full path already exists as a directory.

Parameters
path

the nested directory to create.

Complexity

O(depth) + syscalls.

Allocation

none.

Compile-run testOsCompileRun.Makedirs
Performancefilesystem/process syscall — not micro-benchmarked
fn void rmdir(const std::string &path) source#

Remove an (empty) directory.

Deletes a single, empty directory; throws if path is non-empty. A missing path is a no-op (no error). Note this is the same fs::remove used by remove(), so it will also delete a regular file at path.

Parameters
path

the directory to remove.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.Rmdir
Performancefilesystem/process syscall — not micro-benchmarked
fn bool remove(const std::string &path) source#

Remove a file or empty directory.

Deletes a single file or empty directory and returns whether anything was removed; a missing path returns false rather than throwing. Throws if path is a non-empty directory.

Parameters
path

the entry to remove.

Returns

true iff something was removed.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.Remove
Performancefilesystem/process syscall — not micro-benchmarked
fn void rename(const std::string &src, const std::string &dst) source#

Rename/move src to dst.

Moves or renames an entry; an existing dst is overwritten when permitted by the underlying fs::rename. Crossing filesystems or other failures throw.

Parameters
src

source path.

dst

destination path.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.Rename
System testStdlibE2E.Os
Performancefilesystem/process syscall — not micro-benchmarked
fn std::string getenv(const std::string &name, const std::string &fallback="") source#

Read an environment variable.

Returns fallback (default "") when the variable is unset; an empty string result therefore does not distinguish "unset" from "set to empty".

Parameters
name

the variable name.

fallback

returned when unset.

Returns

the value, or fallback.

Complexity

O(environment size) — std::getenv is a linear scan of the C library's environment table (no syscall).

Allocation

allocates the returned string.

Compile-run testOsCompileRun.Getenv
System testStdlibE2E.Os
Performance74.68 ns/call in cheatah · 365 ns/call in CPython 3.12.3 · ≈5× faster
fn void setenv(const std::string &name, const std::string &value, bool overwrite=true) source#

Set an environment variable.

When overwrite is false and the variable already exists, the existing value is kept; otherwise it is created or replaced. The change affects only this process and its future children.

Parameters
name

the variable name.

value

the value to set.

overwrite

replace an existing value when true.

Complexity

O(environment size) — the C library scans and updates its environment table (no syscall).

Allocation

may allocate inside the C library's environment table.

Compile-run testOsCompileRun.Setenv
System testStdlibE2E.Os
Performancefilesystem/process syscall — not micro-benchmarked
fn int getpid() source#

Process id.

Returns

the current process's pid.

Complexity

O(1) + a syscall.

Allocation

none.

Compile-run testOsCompileRun.Getpid
System testStdlibE2E.Os
Performance71.73 ns/call in cheatah · 145 ns/call in CPython 3.12.3 · ≈2.1× faster
fn unsigned cpu_count() source#

Logical CPU count.

Reports std::thread::hardware_concurrency(), the number of concurrent threads supported; the standard allows it to return 0 when the value cannot be determined, so callers should treat 0 as "unknown".

Returns

the number of hardware threads (0 if undetermined).

Complexity

O(1).

Allocation

none.

Compile-run testOsCompileRun.CpuCount
System testStdlibE2E.Os
Performance1480 ns/call in cheatah · 1639 ns/call in CPython 3.12.3 · ≈1.1× faster
fn int system(const std::string &command) source#

Run a shell command.

Passes command to the system shell via std::system and blocks until it finishes; the returned status is implementation-defined (on POSIX, a wait status, conventionally decoded so that 0 means success).

Parameters
command

the command line.

Returns

the command's exit status.

Complexity

O(1) here + the cost of the spawned process (fork/exec via the shell).

Allocation

none.

Warning

command is interpreted by the shell (quoting, expansion, ;/|) — never build it from untrusted input.

Compile-run testOsCompileRun.System
System testStdlibE2E.Os
Performancefilesystem/process syscall — not micro-benchmarked
fn std::string urandom(int n) source#

Cryptographically secure random bytes (like Python's os.urandom).

Reads n bytes from the operating system's CSPRNG — getentropy//dev/urandom on POSIX, BCryptGenRandom on Windows — suitable for keys and signatures. Unlike the random module (a deterministic, seedable PRNG), this is NOT reproducible and must not be seeded. Throws std::runtime_error if the OS source cannot be read (so a key is never built from non-random bytes), and std::invalid_argument for a negative n.

Parameters
n

the number of bytes to return (must be non-negative).

Returns

a string of n random bytes (may contain embedded NULs).

Complexity

O(n), plus one syscall per 256-byte chunk on POSIX (getentropy's per-call limit; a single BCryptGenRandom call on Windows).

Allocation

allocates the n-byte result.

Compile-run testOsCompileRun.Urandom
System testStdlibE2E.Os
fn std::string module_ext() source#

The loadable-module file extension for this platform.

A compiled cheatah program is a native loadable module run by the cheatah host; its file extension is .so on Linux/BSD, .dylib on macOS, and .dll on Windows. Tools that build or name modules (e.g. the biome package manager) use this instead of hardcoding .so, so the paths they print and generate are correct on every platform. The result includes the leading dot.

Returns

the platform module extension (e.g. ".so", ".dylib", ".dll").

Complexity

O(1).

Allocation

allocates the returned string.

Compile-run testOsCompileRun.ModuleExt
System testStdlibE2E.Os