mmap_readat(3)

πŸ“¦
version 0.34 Β· 2 versions
, in alpine 3.24, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu resolute, ubuntu stonking

✨ mmap_readat ✨

okay so bestie, this one lives in the Library Functions Manual and it's literally the "give me this whole file in memory, right now, no drama" function πŸ’…


🌸 NAME 🌸

mmap_readat β€” memory map a file for reading πŸ—ΊοΈβœ¨


πŸŽ€ SYNTAX πŸŽ€

#include <libowfat/mmap.h>

const char* mmap_readat(const char* filename,size_t* filesize,int dirfd);

πŸ¦‹ DESCRIPTION πŸ¦‹

so here's the vibe: mmap_readat opens filename for reading, maps the entire file into memory, then closes the file. after that it writes the length of the file into filesize and hands you back a pointer to the mapped file. boom. whole file, right there, zero scrolling. πŸ”₯

if filename is a relative path and dirfd is not AT_FDCWD (which is defined in fcntl.h, iykyk), then filename gets interpreted relative to dirfd β€” and dirfd must be an open directory. no cap, it has to be an actual open dir. πŸ™ˆ

the file gets unmapped by the operating system all on its own when the process terminates (she's independent like that πŸ’). but if you wanna do it manually, you can call munmap from <sys/mman.h> β€” main character energy, taking things into your own hands.

and if the file could not be opened or mapped? you get (void*)0 back. not a vibe, but that's the deal. 😭

πŸ‘‘ SEE ALSO πŸ‘‘


that's the whole page bestie, mwah πŸ’– from the Library Functions Manual with love β€” go map something iconic πŸŽ€βœ¨