Skip to content
systemcoding.dev

Tag

posix

44 posts

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Linux and FreeBSD logos.
Systems Programming

Print All Environment Variables Using environ

The global environ is the NULL-terminated array behind getenv. Loop over it to list every NAME=value entry, split entries safely and avoid the stale envp trap.

7 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

Read Environment Variables in C with getenv

getenv returns a pointer to a variable's value, or NULL when it is unset. Check for NULL, tell empty from unset, copy what you keep and parse numbers safely.

8 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

Process Lines Efficiently with getline

Read lines of any length in C with getline, reuse one growing buffer, strip the newline, spot read errors and handle files without a final newline.

7 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Rust and FreeBSD logos.
Systems Programming

Write Errors to stderr with perror and err

Report failures from a C tool the Unix way: perror, strerror and the err and warn family, with errno saved correctly and messages sent to stderr.

6 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Rust and Linux logos.
Systems Programming

Read from stdin or a File Argument Like cat

Write a cat-style filter in C that reads the files named on the command line, uses standard input when there are none, and treats a lone hyphen as stdin.

7 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, FreeBSD and Rust logos.
Systems Programming

Exit Codes and Conventions for Unix Tools

What exit status 0, 1, 2, 126, 127 and 128 plus a signal number mean on Linux, how a shell reads them, and which codes your own command-line tool should return.

7 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, Rust and Linux logos.
Systems Programming

Parse Command-Line Options with getopt

Learn how getopt walks argv in C, what optind, optarg and opterr mean, how a colon marks an option value, and how to handle errors and bundled flags.

8 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Rust logos.
Systems Programming

Limit a Process's Resources with setrlimit

Cap open files, CPU time, memory and core dumps for a Linux process with setrlimit, understand soft and hard limits, and read the results in /proc.

7 min read
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

Redirect stdout to a File with dup2

Redirect stdout to a file in C by opening it and calling dup2 onto descriptor 1. Includes saving and restoring the terminal and the fork and exec case.

7 min read