ABS2REL(3)
π 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: theresultargument must refer to a buffer that can hold at leastsizecharacters. 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 thesizeargument is zero. like... one of those two, pick your struggle π[ERANGE]β thesizeargument 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
- Copyright (c) 1997 Shigio Yamaguchi. All rights reserved
- Copyright (c) 1999 Tama Communications Corporation. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms