Skip to main content

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.
  • O_RDONLY - Read-only
  • O_WRONLY - Write-only
  • O_RDWR - Read and write
  • O_CREAT - Create if doesn’t exist
  • O_TRUNC - Truncate to zero length
  • O_APPEND - Append to end
  • O_NONBLOCK - Non-blocking mode
  • O_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

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

  • ENOENT - No such file or directory
  • EACCES - Permission denied
  • ENOMEM - Out of memory
  • EINVAL - Invalid argument
  • EIO - I/O error
  • EAGAIN - 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.