xref: /freebsd/lib/libpathconv/abs2rel.c (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
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 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "pathconv.h" /* prototypes */
30 /*
31  * abs2rel: convert an absolute path name into relative.
32  *
33  *	i)	path	absolute path
34  *	i)	base	base directory (must be absolute path)
35  *	o)	result	result buffer
36  *	i)	size	size of result buffer
37  *	r)		!= NULL: relative path
38  *			== NULL: error
39  */
40 char *
41 abs2rel(const char *path, const char *base, char *result, const size_t size)
42 {
43 	const char *pp, *bp, *branch;
44 	/*
45 	 * endp points the last position which is safe in the result buffer.
46 	 */
47 	const char *endp = result + size - 1;
48 	char *rp;
49 
50 	if (*path != '/') {
51 		if (strlen(path) >= size)
52 			goto erange;
53 		strcpy(result, path);
54 		goto finish;
55 	} else if (*base != '/' || !size) {
56 		errno = EINVAL;
57 		return (NULL);
58 	} else if (size == 1)
59 		goto erange;
60 	/*
61 	 * seek to branched point.
62 	 */
63 	branch = path;
64 	for (pp = path, bp = base; *pp && *bp && *pp == *bp; pp++, bp++)
65 		if (*pp == '/')
66 			branch = pp;
67 	if ((*pp == 0 || (*pp == '/' && *(pp + 1) == 0)) &&
68 	    (*bp == 0 || (*bp == '/' && *(bp + 1) == 0))) {
69 		rp = result;
70 		*rp++ = '.';
71 		if (*pp == '/' || *(pp - 1) == '/')
72 			*rp++ = '/';
73 		if (rp > endp)
74 			goto erange;
75 		*rp = 0;
76 		goto finish;
77 	}
78 	if ((*pp == 0 && *bp == '/') || (*pp == '/' && *bp == 0))
79 		branch = pp;
80 	/*
81 	 * up to root.
82 	 */
83 	rp = result;
84 	for (bp = base + (branch - path); *bp; bp++)
85 		if (*bp == '/' && *(bp + 1) != 0) {
86 			if (rp + 3 > endp)
87 				goto erange;
88 			*rp++ = '.';
89 			*rp++ = '.';
90 			*rp++ = '/';
91 		}
92 	if (rp > endp)
93 		goto erange;
94 	*rp = 0;
95 	/*
96 	 * down to leaf.
97 	 */
98 	if (*branch) {
99 		if (rp + strlen(branch + 1) > endp)
100 			goto erange;
101 		strcpy(rp, branch + 1);
102 	} else
103 		*--rp = 0;
104 finish:
105 	return result;
106 erange:
107 	errno = ERANGE;
108 	return (NULL);
109 }
110