cdb_find(3)

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

πŸ’– NAME πŸ’–

okay so bestie, welcome to cdb_find β€” the function that goes and looks up a key in a constant database. that's the whole vibe. πŸŽ€

this one lives in the Library Functions Manual, section 3, and honestly? it ate. ✨


🌸 SYNTAX 🌸

first, you gotta pull in the headers like the little icons they are:

#include <libowfat/cdb.h>
#include <libowfat/uint32.h>

and here are the four function besties you're working with:

int cdb_find(struct cdb *c, char *key, size_t keylen);
int cdb_datalen(struct cdb *c);
int cdb_datapos(struct cdb *c);
int cdb_findnext(struct cdb *c, char *key, size_t keylen);

πŸ‘‘ DESCRIPTION πŸ‘‘

so cdb_find looks for key, periodt. here's the tea on what it hands back:

  • if cdb_find returns 0, the database does not contain that key. so like… stop. that's it. πŸ™ˆ
  • if cdb_find returns -1, there was a read error β€” abort. no cap, something went wrong. πŸ’€

cdb_datalen returns the length of the data associated with the key. use it to allocate a pointer p with enough space to hold the data. like, you need a spot big enough to put the data in, and this tells you how big. πŸ’…

cdb_datapos returns the position of the data inside the file. use it as the argument to cdb_read to retrieve the data. that's the where, and cdb_read is the how. πŸ¦‹

alsooo β€” heads up β€” there may be several records under a single key. not just one. so if you wanna keep going, use cdb_findnext to find the next record under this key. it's giving "there's more where that came from." πŸ”₯


✨ EXAMPLE ✨

here's the little snippet, copied exactly because we do not touch the code, bestie:

static struct c;

if (cdb_find(&c,key,strlen(key)>0) {
  char *buf=alloca(cdb_datalen(&c));
  cdb_read(&c,buf,cdb_datalen(&c),cdb_datapos(&c));
  write(1,buf,cdb_datalen(&c));
}
not me double-checking that strlen(key)>0 is literally in there. it is. leave it. 🫢

πŸŽ€ SEE ALSO πŸŽ€

go give these icons a read too, iykyk:


and that's the whole page, bestie!! πŸ’œ you just read cdb_find(3) in all its glory β€” same page name, top to bottom, no notes. slay, go look up some keys. πŸŒˆβ­πŸ“ 🧚