Opinionated public domain C snippets and helpers, bundled as a single-file library.
Select a few modules: from the list below or run bundle -l
to list the
available modules.
For instance: fail
and logging
:
~/git/libr/bundle --output=r.h fail logging
Then hello.c
can check for errors and do logging:
#define LIBR_IMPLEMENTATION
#include "r.h"
int main(int argc, char* argv[])
{
if(argc != 2) {
failwith("incorrect number of arguments: %d != 2", argc);
}
info("hello %s world!", argv[1]);
int r = close(0);
CHECK(r, "close(%d)", 0);
void* buf = malloc(4096); CHECK_MALLOC(buf);
return 0;
}
which when built and run:
gcc -DLOG_LEVEL=LOG_INFO -o hello hello.c
./hello libr
says hello:
20230906T105013Z:1530871:main:hello.c:10 hello libr world!
- base32: implementation of the RFC4648 Base32 encoding
.h .c .test.c reference - char: helpers to interpret ASCII characters
.h - crc: implementation of CRC16-ccitt
.h .c .test.c wiki - devnull: /dev/null as a filedescriptor
.h .c man - env: getenv wrappers
.h .c man - fail: abort-based error handling
.h .c man - file: read file line-by-line or as a whole
.h .c - hex: hexadecimal binary encoding
.h .c .test.c - hexdump: hexdump troubleshooting utility
.h .c - landlock: Linux landlock syscall wrappers and helpers
.h .c reference man - logging: compiletime eliminated printf-style logging
.h .c - lua: Lua error checking and stack helpers
.h .c reference - mark: realtime frequency calculation
.h .c - no_new_privs: prctl(PR_SET_NO_NEW_PRIVS) helper
.h .c reference man - nonblock: helper to set/unset O_NONBLOCK flag
.h .c man - now: current time in ISO8601 format
.h .c wiki - path: path mangling
.h .c .test.c - rlimit: apply strict rlimits
.h .c man - seccomp: seccomp syscall wrapper
.h .c reference man - sha1: SHA-1 implementation
.h .c .test.c wiki reference - sha2: SHA-2 implementation
.h .c .test.c wiki reference - stopwatch: pausable realtime frequency calculation
.h .c - str: string helpers
.h .c .test.c - test: test utilities
.h .c - util: LENGTH, LIT, MAX, MIN macros
.h - uv: uv error-checking
.h reference - xdg: XDG base directories
.h .c reference - xorshift: implementation of xorshift64 and xorshift128+
.h .c .test.c wiki