buffer_init(3)

πŸ“¦
version 0.34 Β· 2 versions
  • 0.34 Β· alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.30 Β· fedora 43, fedora 44, ubuntu jammy
, in alpine 3.24, debian bookworm, debian trixie, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

🌸 buffer_init(3) β€” from the Library Functions Manual, bestie πŸ’–

okay so like, this is a page from the Library Functions Manual and it's all about buffer_init(3) and honestly? it's giving "let me set up your lil buffer for you" energy and I'm obsessed. sit down, grab a boba, we're doing this together πŸ§‹βœ¨


✨ NAME ✨

buffer_init β€” initialize buffer structure

like. that's the whole job. it sets up a buffer. slay πŸ’…


🌈 SYNTAX 🌈

#include <libowfat/buffer.h>

void buffer_init(buffer &b,
                ssize_t (*op)(int,char*,size_t),
                int fd, char* y, size_t ylen);

that's the whole thing, no cap. five arguments, one function, infinite possibilities πŸ¦‹


πŸ’– DESCRIPTION πŸ’–

so what does buffer_init actually do? bestie, it gets b all ready to store a string in y[0], y[1], ..., all the way to y[ylen-1]. and the vibe check at the start? the string is empty. nothing in there. a blank canvas. main character energy from the jump 🌸

it also preps b so it knows which read/write operation to use β€” that's the one you pass in as op β€” and which file descriptor it's vibing with, that's fd 🫢

and listen, if op, fd, y, and ylen are all compile-time constants, you can skip the function call entirely and do it statically like this:

buffer b = BUFFER_INIT(op,fd,y,ylen);

iconic. static init. no runtime call needed, we love a shortcut πŸ’œ

oh and also? you can call buffer_init again whenever you want. at any time. no permission slip required. BUT β€” and this is the important part β€” doing that discards the currently buffered string. gone. poof. so don't do it casually unless you meant to yeet that data into the void πŸ’€

no fr, that last bit matters. re-initializing = your buffered string is toast.

πŸŽ€ EXAMPLE πŸŽ€

here's a whole lil program showing it off, and yes it's exactly this, character for character:

#include <libowfat/buffer.h>
#include <libowfat/open.h>

char buf[4096];
int fd=open_read("/etc/services");
buffer input;

if (fd>=0) {
  char x;
  buffer_init(&input,read,fd,buf,sizeof buf);
  while (buffer_get(&input,&x,1)==1) {
    buffer_put(buffer_1,&x,1);
    if (x=='\n') break;
  }
  buffer_flush(buffer_1);
}

look at that. reads from /etc/services one char at a time until it hits a newline and then bails. so cute, so efficient, so real 🌟


πŸ‘‘ SEE ALSO πŸ‘‘

go read those too, they're the supporting cast and they ate πŸ”₯


and that's the tea, bestie πŸ’… this was buffer_init(3) straight out of the Library Functions Manual, and yeah β€” the page signs off as buffer_init(3) too, so consider this your cute lil footer: same name, same manual, forever matching πŸŽ€βœ¨ bye ily, go init a buffer responsibly πŸ¦‹