xref: /freebsd/lib/libpathconv/abs2rel.3 (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 August 7, 2022
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.Ft "char *"
34.Fn abs2rel "const char *path" "const char *base" "char *result" "size_t size"
35.Sh DESCRIPTION
36The
37.Fn abs2rel
38function makes a relative path name from an absolute path name
39.Fa path
40based on a directory
41.Fa base
42and copies the resulting path name into the memory referenced by
43.Fa result .
44The
45.Fa result
46argument must refer to a buffer capable of storing at least
47.Fa size
48characters.
49
50The resulting path name may include symbolic links.
51The
52.Fn abs2rel
53function doesn't check whether or not any path exists.
54.Sh "RETURN VALUES"
55The
56.Fn abs2rel
57function returns relative path name on success.
58If an error occurs,
59it returns
60.Dv NULL .
61.Sh EXAMPLES
62    char result[MAXPATHLEN];
63    char *path = abs2rel("/usr/src/sys", "/usr/local/lib", result, MAXPATHLEN);
64
65yields:
66
67    path == "../../src/sys"
68
69Similarly,
70
71    path1 = abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN);
72    path2 = abs2rel("/usr/src/sys", "/usr/src/sys", result, MAXPATHLEN);
73
74yields:
75
76    path1 == "src/sys"
77    path2 == "."
78.Sh ERRORS
79The
80.Fn abs2rel
81function may fail and set the external variable
82.Va errno
83to indicate the error.
84.Bl -tag -width Er
85.It Bq Er EINVAL
86The
87.Fa base
88directory isn't an absolute path name or the
89.Fa size
90argument is zero.
91.It Bq Er ERANGE
92The
93.Fa size
94argument is greater than zero but smaller than the length of the pathname plus 1.
95
96.El
97.Sh SEE ALSO
98.Xr rel2abs 3
99.Sh AUTHORS
100.An Shigio Yamaguchi (shigio@tamacom.com)
101.Sh BUGS
102If the
103.Fa base
104directory includes symbolic links,
105the
106.Fn abs2rel
107function produces the wrong path.
108For example, if '/sys' is a symbolic link to '/usr/src/sys',
109
110    char *path = abs2rel("/usr/local/lib", "/sys", result, MAXPATHLEN);
111
112yields:
113
114    path == "../usr/local/lib"         /* It's wrong!! */
115
116You should convert the base directory into a real path in advance.
117.Pp
118
119    path = abs2rel("/sys/kern", realpath("/sys", resolvedname), result, MAXPATHLEN);
120
121yields:
122
123    path == "../../../sys/kern"        /* It's correct but ... */
124
125That is correct, but a little redundant.
126If you wish get the simple answer 'kern', do the following.
127
128    path = abs2rel(realpath("/sys/kern", r1), realpath("/sys", r2),
129								result, MAXPATHLEN);
130
131The
132.Fn realpath
133function assures correct result, but don't forget that
134.Fn realpath
135requires that all but the last component of the path exist.
136