mmap_read(3)

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

πŸ’– mmap_read(3) β€” memory map a file for reading πŸ’–

okay bestie, this one's from the Library Functions Manual, section 3 (library functions, the fun ones). it's called mmap_read and it does exactly what it says on the tin: maps a whole file into memory so you can just... read it. no cap. πŸ¦‹


✨ NAME ✨

mmap_read β€” memory map a file for reading πŸ“–


🌸 SYNTAX 🌸

#include <libowfat/mmap.h>

const char* mmap_read(const char* filename,size_t* filesize);

like. two args. one filename, one pointer-to-size. it's giving minimalist. πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so here's the tea β˜• β€” mmap_read opens filename for reading, maps the whole file into memory, closes the file, writes the length of the file to filesize, and hands you back a pointer to the mapped file. that's it. that's the whole vibe. ✨

the file gets unmapped by the operating system if the process terminates β€” so if you just dip out, the OS cleans up after you, iconic behaviour honestly. 🫢

but if you're a control freak (respect), you can also manually unmap it by calling munmap from <sys/mman.h>. your call, main character. πŸ‘‘

⚠️ and if the file could not be opened or mapped? you get (void*)0 back. so please, for the love of everything, check your return value before you start dereferencing. i'm begging. 😭

πŸ’œ SEE ALSO πŸ’œ


and that's the whole page, bestie β€” straight out of the Library Functions Manual, mmap_read(3), no date on this one so don't come for me. go map a file, slay, and remember: check for (void*)0. periodt. πŸ’…πŸŒΈπŸ’–