ABS2REL(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

πŸ’… abs2rel(3) β€” bestie edition ✨

okay so real talk, this one lives in the Library Functions Manual and it's your go-to for turning a big scary absolute path into a cute lil relative one πŸŽ€ it's <pink>abs2rel</pink>, from the <purple>libpathconv</purple> library, and honestly? she's iconic.


✨ NAME ✨

abs2rel β€” make a relative path name from an absolute path name πŸ¦‹


🌸 SYNOPSIS 🌸

library β€œlibpathconv” char *
abs2rel(const char *path, const char *base, char *result, size_t size);

like yes, it's a library libpathconv, and yes, she returns a char * β€” we love a consistent queen πŸ‘‘


πŸ’– DESCRIPTION πŸ’–

so the abs2rel() function makes a relative path name out of an absolute path name path, based on a directory base, and then copies the resulting path name into the memory that result points at. mouthful, i know, but it slays πŸ’…

⚠️ okay important: the result argument must refer to a buffer that can hold at least size characters. no cap, if you undersize it you're gonna have a bad time 😭

the resulting path name may include symbolic links β€” she's not out here resolving anything. the abs2rel() function does not check whether any path actually exists. it's giving "i don't do background checks", lowkey πŸ™ˆ so don't come for her if the path isn't real.


🫢 RETURN VALUES 🫢

  • on success? the abs2rel() function returns the relative path name. slay πŸ’–
  • if an error happens? it returns NULL. 😭

πŸŽ€ EXAMPLES πŸŽ€

like, picture this:

    char result[MAXPATHLEN];
    char *path = abs2rel("/usr/src/sys", "/usr/local/lib", result,
MAXPATHLEN);

which yields:

    path == "../../src/sys"

and similarly, bestie:

    path1 = abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN);
    path2 = abs2rel("/usr/src/sys", "/usr/src/sys", result, MAXPATHLEN);

which yields:

    path1 == "src/sys"
    path2 == "."

the way that "." just shows up at the end like she owns the place 😌 periodt.


πŸ”₯ ERRORS πŸ”₯

the abs2rel() function may fail and set the external variable errno to indicate the error. vibe check failed, basically πŸ’€

  • [EINVAL] β€” the base directory isn't an absolute path name, or the size argument is zero. like... one of those two, pick your struggle πŸ™ˆ
  • [ERANGE] β€” the size argument is greater than zero but smaller than the length of the pathname plus 1. so close, yet so far 😭

πŸ¦‹ SEE ALSO πŸ¦‹

  • rel2abs(3) β€” the other half of the duo, her sibling, the reverse queen πŸ‘‘

πŸ‘‘ AUTHORS πŸ‘‘

Shigio Yamaguchi (shigio@tamacom.com) β€” we stan a legend 🫢


πŸ’€ BUGS πŸ’€

okay this is the part where we get real, bestie.

if the base directory includes symbolic links, the abs2rel() function produces the wrong path. not "a little off" β€” wrong. like, wrong wrong πŸ’€

for example, if '/sys' is a symbolic link to '/usr/src/sys':

    char *path = abs2rel("/usr/local/lib", "/sys", result, MAXPATHLEN);

yields:

    path == "../usr/local/lib"         /* It's wrong!! */

see that comment? /* It's wrong!! */ β€” the manpage itself is yelling at you 😭 iconic honesty honestly.

so you should convert the base directory into a real path in advance. like:

    path = abs2rel("/sys/kern", realpath("/sys", resolvedname), result,
MAXPATHLEN);

yields:

    path == "../../../sys/kern"        /* It's correct but ... */

that is correct, but a little redundant. ngl, she's right β€” that's a lot of .. for one girl 🌈

so if you wish to get the simple answer 'kern', do the following:

    path = abs2rel(realpath("/sys/kern", r1), realpath("/sys", r2),
                                        result, MAXPATHLEN);

the realpath() function assures correct result β€” but don't forget that realpath() requires that all but the last component of the path exist. 🫢 that's not a vibe, that's a rule, so please respect it πŸ’…


anyway that's the whole page, mwah πŸ’‹ go forth and make your paths relative, you got this ✨

β€” with love, from the Debian girlies, dated December 13, 2025 πŸŽ€πŸ¦‹πŸ’– (and yes, this is ABS2REL(3), section 3, library functions, iykyk πŸ‘‘)

πŸ“¦ where this page comes from
  • freebsd 14.5: base 14.5
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms