Thread Reader
Emily

Emily
@the_aiju

Sep 30, 2022
7 tweets
Twitter

C programmers be like: i want a function with a short name and a looong body

java programmers will have a method called retrieveBlockFromCentralBlockAndCacheContents which just calls another method somewhere, c programmers will have a function called getblk and it's 1000 lines of triply nested for loops and fiddly logic
calling out past emily btw the function is too long to screenshot lol
/*
 * getblk returns the address of a block in a file
 * given its relative address blk
 * the address are returned in *r
 * mode has to be one of:
 * - GBREAD: this block will only be read
 * - GBWRITE: this block will be written, but don't create it
 *            if it doesn't exist
 * - GBCREATE: this block will be written, create it if necessary
 * - GBOVERWR: like GBCREATE, but return an empty block if a dump
 *             would be necessary
 * return value is 1 if the block existed, -1 on error
 * a return value of 0 means the block did not exist
 * this is only an error in case of GBREAD and GBWRITE
 */

int
getblk(Fs *fs, FLoc *L, Buf *bd, uvlong blk, uvlong *r, int mode)
{
	uvlong k, l;
	uvlong *loc;
	int i, j, rc, prc;
	Buf *b;
	Dentry *d;

	b = bd;
	d = getdent(L, b);
	if(d == nil){
		dprint("getblk: dirent gone\n");
		return -1;
	}
	if(blk < NDIRECT){
		loc = &d->db[blk];
		goto found;
	}
	blk -= NDIRECT;
	l = 1;
	for(i = 0; i < NINDIRECT; i++){
		l *= OFFPERBLK;
		if(blk
it's from the filesystem hjfs that i wrote for plan 9 getblk() was one of the worst functions i had ever written at the time, it took ages to get all the cornercases right, it ended up a dumping ground for a lot of functionality, it's really the core of the fs
it's wild that at the time i thought i had it all figured out and now i look back on it and all i can say is lmao :)
there was a reason why it's a single function rather than multiple but i think if C supported functions within functions it would have been much easier to write and i wouldn't have gotten myself tangled up in so much mess tbh
maybe in hindsight writing multiple functions and copy-pasting the common parts would have been a better idea though
Missing some tweets in this thread? Or failed to load images or videos? You can try to .