make_relative_filename(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

πŸ’– make_relative_filename β€” the Allegro manual, but make it make sense πŸ’–

okay bestie, this one lives in the Allegro game programming library, and it's genuinely so useful once you get the vibe. no cap. 🫢


✨ NAME ✨

make_relative_filename β€” she tries to make a relative filename out of an absolute path and an absolute filename. part of the Allegro game programming library, periodt. πŸ’…


🌸 SYNOPSIS 🌸

#include <allegro.h>

char *make_relative_filename(char *dest, const char *path, const char
*filename, int size);

like. look at her. iconic signature fr fr. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so what she's doing is: you hand her an absolute path and an absolute filename, and she attempts to cook up a relative filename for you, storing at most size bytes into the dest buffer. that's the whole assignment. she ate. πŸ”₯

⚠️ two things you NEED to know before you go off, bestie: - she won't work if the paths aren't canonical under the current platform β€” go check canonicalize_filename() for that, iykyk - dest cannot be used as the input value for path or filename. do not do it. it's giving chaos. 😭

here's the example, verbatim, don't touch her:

char base[] = "/long/absolute/path/program.exe";
char user_input[] = "/nice/and/short.txt";
...
make_relative_filename(buf, base, user_input, sizeof(buf));
/* Under Unix buf would contain:
   ../../../nice/and/short.txt */

the way that ../../../nice/and/short.txt is literally a glow-up from that giant absolute path... we love a transformation arc. βœ¨πŸ‘‘


πŸ’œ RETURN VALUE πŸ’œ

she returns a copy of the dest parameter when she succeeds. if she fails, you get NULL. 🫠

and lowkey the failure case is messy β€” for example, under DOS, if one path starts with "C:\" and another starts with "A:\", bestie there is simply no relative path between those two worlds. two different drives, two different universes. πŸ’€


🌈 SEE ALSO 🌈


that's the whole page, bestie β€” no notes, she's perfect. this little love letter comes straight out of the Allegro manual, version 4.4.3, and the page you just read is make_relative_filename(3). go forth and compute, queen. πŸ’–βœ¨πŸŒΈπŸ¦‹πŸ‘‘πŸ’…

πŸ“¦ where this page comes from