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