libstd/os

Process arguments and environment, current directory, uname fields, the executable path, system information.

#include <libstd/os>

Includes libstd/string, libstd/file. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/sysos and wrapper/windows/sysos (see Linux and Windows). Links the shared library ntdll.dll (FFI).

Example

#include <libstd/stdio>
#include <libstd/os>

i = 0
while i < arg_count():
    print("argv[", i, "] = ", arg_at(i), "\n")
    i++
print(getenv("HOME"), "\n")
print(os_name(), " ", machine_arch(), " on ", hostname(), "\n")
print(cwd(), "\n")

Constants

NameLinuxWindowsMeaning
UTSNAME_FIELD_SIZE6565
UTSNAME_TOTAL_SIZE390390
UTSNAME_OFF_SYSNAME00
UTSNAME_OFF_NODENAME6565
UTSNAME_OFF_RELEASE130130
UTSNAME_OFF_VERSION195195
X_OK11
UTSNAME_OFF_MACHINE260260
UTSNAME_OFF_DOMAINNAME325325
OS_PATH_MAX40964096

Classes

class
SysInfo

The struct sysinfo of the kernel: uptime, loads, memory totals, process count.

    uptime long
    load1 long
    load2 long
    load3 long
    total_ram long
    free_ram long
    sharedram long
    bufferram long
    totalswap long
    freeswap long
    procs short
    pad short
    pad_align int
    totalhigh long
    freehigh long
    mem_unit int
    __pad0 long
    __pad1 long
    __pad2 long
    __pad3 long
    __pad4 long
    __pad5 long
    __pad6 long
    __pad7 long

Functions

def sysinfo(sysinfo_i: SysInfo) -> int

Fills a SysInfo; returns 0 on success.

def arg_count() -> int

Number of command-line arguments, argv[0] included.

def os_name() -> str

The sysname field, e.g. Linux.

def hostname() -> str

The nodename field.

def os_release() -> str

The kernel release, e.g. 6.1.0-kali9-amd64.

def kernel_version() -> str

The kernel version string with build date.

def machine_arch() -> str

The hardware architecture, e.g. x86_64.

def run_capture(cmd: str) -> str
(no description yet)
def abs_path_node_exe() -> str
(no description yet)
def sys_sysinfo(sysinfo_i: SysInfo) -> int

Raw sysinfo(2) into a SysInfo.

def sys_getcwd(buf: str, size: int) -> int

Raw getcwd(2) into a caller-provided buffer of size bytes.

def sys_chdir(path: str) -> int

Changes the current directory. Returns 0 on success.

def sys_uname(buf: str) -> int

Raw uname(2) into a buffer of at least UTSNAME_TOTAL_SIZE bytes.

def sys_readlink(path: str, buf: str, size: int) -> int

Raw readlink(2): writes the target of a symlink into buf without a terminator; returns its length.

def sys_pipe(fds: str) -> int
(no description yet)
def package_manager_name() -> str

Name of the package manager found on the system (apt, dnf, pacman, ...), by probing their executables.

def which(name: str) -> str
(no description yet)
def arg_at(index: int) -> str

The argument at index (0 = program name).

def getenv(name: str) -> str

The value of an environment variable, "" if not set. Exact, case-sensitive name match.

def cwd() -> str

The current working directory as a new string.

def uname_field(offset: int) -> str

Runs uname and returns the field at offset (see the UTSNAME_OFF_* constants).

def to_absolute_path(path: str) -> str

Resolves a path relative to the current directory into an absolute one.

def exe_path() -> str

Absolute path of the running executable (from /proc/self/exe).