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
| Name | Linux | Windows | Meaning |
|---|---|---|---|
UTSNAME_FIELD_SIZE | 65 | 65 | |
UTSNAME_TOTAL_SIZE | 390 | 390 | |
UTSNAME_OFF_SYSNAME | 0 | 0 | |
UTSNAME_OFF_NODENAME | 65 | 65 | |
UTSNAME_OFF_RELEASE | 130 | 130 | |
UTSNAME_OFF_VERSION | 195 | 195 | |
X_OK | 1 | 1 | |
UTSNAME_OFF_MACHINE | 260 | 260 | |
UTSNAME_OFF_DOMAINNAME | 325 | 325 | |
OS_PATH_MAX | 4096 | 4096 |
Classes
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 longFunctions
Fills a SysInfo; returns 0 on success.
Number of command-line arguments, argv[0] included.
The sysname field, e.g. Linux.
The nodename field.
The kernel release, e.g. 6.1.0-kali9-amd64.
The kernel version string with build date.
The hardware architecture, e.g. x86_64.
Raw sysinfo(2) into a SysInfo.
Raw getcwd(2) into a caller-provided buffer of size bytes.
Changes the current directory. Returns 0 on success.
Raw uname(2) into a buffer of at least UTSNAME_TOTAL_SIZE bytes.
Raw readlink(2): writes the target of a symlink into buf without a terminator; returns its length.
Name of the package manager found on the system (apt, dnf, pacman, ...), by probing their executables.
The argument at index (0 = program name).
The value of an environment variable, "" if not set. Exact, case-sensitive name match.
The current working directory as a new string.
Runs uname and returns the field at offset (see the UTSNAME_OFF_* constants).
Resolves a path relative to the current directory into an absolute one.
Absolute path of the running executable (from /proc/self/exe).