1.\" 2.\" Copyright (c) 1997 Shigio Yamaguchi. All rights reserved. 3.\" Copyright (c) 1999 Tama Communications Corporation. All rights reserved. 4.\" 5.\" Redistribution and use in source and binary forms, with or without 6.\" modification, are permitted provided that the following conditions 7.\" are met: 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in the 12.\" documentation and/or other materials provided with the distribution. 13.\" 14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24.\" SUCH DAMAGE. 25.\" 26.Dd December 13, 2025 27.Dt ABS2REL 3 28.Os 29.Sh NAME 30.Nm abs2rel 31.Nd make a relative path name from an absolute path name 32.Sh SYNOPSIS 33.Lb libpathconv 34.Ft "char *" 35.Fn abs2rel "const char *path" "const char *base" "char *result" "size_t size" 36.Sh DESCRIPTION 37The 38.Fn abs2rel 39function makes a relative path name from an absolute path name 40.Fa path 41based on a directory 42.Fa base 43and copies the resulting path name into the memory referenced by 44.Fa result . 45The 46.Fa result 47argument must refer to a buffer capable of storing at least 48.Fa size 49characters. 50 51The resulting path name may include symbolic links. 52The 53.Fn abs2rel 54function doesn't check whether or not any path exists. 55.Sh "RETURN VALUES" 56The 57.Fn abs2rel 58function returns relative path name on success. 59If an error occurs, 60it returns 61.Dv NULL . 62.Sh EXAMPLES 63 char result[MAXPATHLEN]; 64 char *path = abs2rel("/usr/src/sys", "/usr/local/lib", result, MAXPATHLEN); 65 66yields: 67 68 path == "../../src/sys" 69 70Similarly, 71 72 path1 = abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN); 73 path2 = abs2rel("/usr/src/sys", "/usr/src/sys", result, MAXPATHLEN); 74 75yields: 76 77 path1 == "src/sys" 78 path2 == "." 79.Sh ERRORS 80The 81.Fn abs2rel 82function may fail and set the external variable 83.Va errno 84to indicate the error. 85.Bl -tag -width Er 86.It Bq Er EINVAL 87The 88.Fa base 89directory isn't an absolute path name or the 90.Fa size 91argument is zero. 92.It Bq Er ERANGE 93The 94.Fa size 95argument is greater than zero but smaller than the length of the pathname plus 1. 96 97.El 98.Sh SEE ALSO 99.Xr rel2abs 3 100.Sh AUTHORS 101.An Shigio Yamaguchi (shigio@tamacom.com) 102.Sh BUGS 103If the 104.Fa base 105directory includes symbolic links, 106the 107.Fn abs2rel 108function produces the wrong path. 109For example, if '/sys' is a symbolic link to '/usr/src/sys', 110 111 char *path = abs2rel("/usr/local/lib", "/sys", result, MAXPATHLEN); 112 113yields: 114 115 path == "../usr/local/lib" /* It's wrong!! */ 116 117You should convert the base directory into a real path in advance. 118.Pp 119 120 path = abs2rel("/sys/kern", realpath("/sys", resolvedname), result, MAXPATHLEN); 121 122yields: 123 124 path == "../../../sys/kern" /* It's correct but ... */ 125 126That is correct, but a little redundant. 127If you wish get the simple answer 'kern', do the following. 128 129 path = abs2rel(realpath("/sys/kern", r1), realpath("/sys", r2), 130 result, MAXPATHLEN); 131 132The 133.Fn realpath 134function assures correct result, but don't forget that 135.Fn realpath 136requires that all but the last component of the path exist. 137