Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ziglang/zig/llms.txt
Use this file to discover all available pages before exploring further.
OS Interface
Thestd.os module provides thin wrappers around OS-specific APIs with the following goals:
- Convert “errno”-style error codes into Zig errors
- Provide slice-based APIs alongside null-terminated APIs
- Cross-platform abstraction for POSIX-like systems
- Use libc when linked, otherwise use direct syscalls
Platform Modules
linux
windows
wasi
Other Platforms
Environment Variables
environ
main().
Warning: The value will be undefined when using zig build-lib.
argv
main().
Availability: Not available on WASI or Windows without libc. Use std.process.argsAlloc for cross-platform code.
File Descriptor Operations
getFdPath
File descriptor to query
Buffer to store the path
Slice of the buffer containing the canonical path
- Windows: WTF-8 encoded
- Other: Opaque byte sequence
- ✅ Windows, macOS, Linux, FreeBSD, illumos, serenity
- ✅ DragonFly BSD 6.0+, NetBSD 10.0+
- ❌ WASI
Uses
fcntl with F.GETPATH commandReads symlink from
/proc/self/fd/{fd}Uses
fcntl with F.KINFO to get kinfo_file structureUses Win32 API
GetFinalPathNameByHandleisGetFdPathSupportedOnTarget
getFdPath is supported on a target OS.
Target operating system
true if getFdPath is supported on the targetFile Statistics
fstat_wasi
File descriptor
File statistics structure
FstatError
Platform Detection
Thestd.os module is primarily used through std.posix, which provides a more portable interface. For platform-specific functionality:
Windows Specific
Linux Specific
Error Handling
Thestd.os module converts OS error codes to Zig errors:
System Information
Getting System Paths
Platform Constants
Each platform module provides OS-specific constants:Usage Patterns
Cross-Platform File Operations
Platform-Specific Code
Error Code Conversion
Design Philosophy
Thestd.os module follows these principles:
- Thin Wrappers: Minimal abstraction over OS APIs
- Error Translation: Convert OS error codes to Zig errors
- Type Safety: Use Zig’s type system for safer APIs
- Cross-Platform: Abstract common operations while allowing platform-specific code
- Libc Optional: Work with or without libc when possible
Best Practices
-
Prefer std.posix: Use
std.posixfor portable POSIX operations instead ofstd.osdirectly. -
Platform checks: Use
builtin.os.tagfor compile-time platform detection. - Error handling: Always handle platform-specific errors appropriately.
-
Direct syscalls: On Linux without libc, use
std.os.linuxfor direct syscalls. -
Windows APIs: Access Windows-specific functionality through
std.os.windows. -
Avoid getFdPath: Only use
getFdPathwhen absolutely necessary, as it’s often a code smell.
Related Modules
std.posix- Portable POSIX interface (recommended)std.fs- File system operationsstd.process- Process managementstd.Thread- Threading support