Overview
LibC is SerenityOS’s implementation of the C standard library, providing POSIX-compliant interfaces for system calls, I/O, memory management, and more. It serves as the foundation for both C and C++ programs running on SerenityOS.LibC wraps SerenityOS kernel syscalls and provides a familiar POSIX interface for application development.
Standard I/O
File Operations
Standard file I/O using FILE* streams.Standard Streams
Pre-defined file streams for standard I/O.FILE*
Standard input stream (file descriptor 0)
FILE*
Standard output stream (file descriptor 1)
FILE*
Standard error stream (file descriptor 2)
Formatted I/O
File System
Low-Level I/O
Direct file descriptor operations.Open Flags
Open Flags
O_RDONLY- Read-onlyO_WRONLY- Write-onlyO_RDWR- Read and writeO_CREAT- Create if doesn’t existO_TRUNC- Truncate to zero lengthO_APPEND- Append to endO_NONBLOCK- Non-blocking modeO_CLOEXEC- Close on exec
Directory Operations
File Metadata
Memory Management
Dynamic Allocation
Memory Safety: Always check malloc/calloc/realloc return values and free allocated memory to prevent leaks.
Memory Operations
String Handling
String Operations
Safe String Functions
Safe String Functions
Always use the “n” variants with explicit size limits:
Character Classification
Process Control
Process Management
Program Execution
Environment Variables
Threading
POSIX Threads
Mutexes
Time & Signals
Time Functions
Signal Handling
Networking
Sockets
Error Handling
errno and perror
Common errno Values
Common errno Values
ENOENT- No such file or directoryEACCES- Permission deniedENOMEM- Out of memoryEINVAL- Invalid argumentEIO- I/O errorEAGAIN- Try again (non-blocking I/O)EINTR- Interrupted system call
Best Practices
Check Return Values: Always check return values from system calls and library functions. Most return -1 or NULL on error.
Buffer Safety: Use bounded functions (strncpy, snprintf) to prevent buffer overflows.
Resource Cleanup: Always close file descriptors, free memory, and destroy mutexes when done.
Related APIs
- AK - Modern C++ alternatives
- LibCore - Higher-level abstractions
- System Calls - Kernel interface
