aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJulian Elischer <julian@FreeBSD.org>2016-11-23 07:57:52 +0000
committerJulian Elischer <julian@FreeBSD.org>2016-11-23 07:57:52 +0000
commit0f7f3352c8bc463607912e2463d13e52d44a4cae (patch)
tree38191f707228c23dd2475b7cf265cf329b392128 /lib
parentfb6216bd43a3231aa6c97a033c736727ad893f5d (diff)
downloadsrc-0f7f3352c8bc463607912e2463d13e52d44a4cae.tar.gz
src-0f7f3352c8bc463607912e2463d13e52d44a4cae.zip
This little BSD licensed library has been kicking around for years.
It allows one to trivially convert an absolute path to a relative path and the reverse. The test programs themselves are very useful in scripts but the real use comes shortly with the -r and -a arguments to ln. These are sometimes known as the --relative and --absolute flags and can force a symlink to be relative when you only have an absolue path. Another place these are sometimes used is to add -a and -r args to 'realpath'. Incredibly useful in Makefiles. I was going to just add the files in with 'ln' but a library makes more sense. The test programs may come out in their own right some day for scripting. released under a BSD 2-clause: * Copyright (c) 1997 Shigio Yamaguchi. All rights reserved. * Copyright (c) 1999 Tama Communications Corporation. All rights reserved. The test directry does not conform to any framework. Not connected to build. doc people may want to play with the manual pages. Obtained from: https://www.tamacom.com/pathconvert.html Shigio Yamaguchi. MFC after: 1 month Relnotes: yes Sponsored by: Panzura, Tama Communications Corporation
Notes
Notes: svn path=/head/; revision=309035
Diffstat (limited to 'lib')
-rw-r--r--lib/libpathconv/Makefile20
-rw-r--r--lib/libpathconv/abs2rel.3136
-rw-r--r--lib/libpathconv/abs2rel.c111
-rw-r--r--lib/libpathconv/pathconv.h34
-rw-r--r--lib/libpathconv/rel2abs.398
-rw-r--r--lib/libpathconv/rel2abs.c132
-rw-r--r--lib/libpathconv/tests/Makefile10
-rw-r--r--lib/libpathconv/tests/abs2rel.c60
-rw-r--r--lib/libpathconv/tests/rel2abs.c59
-rwxr-xr-xlib/libpathconv/tests/test.pl146
10 files changed, 806 insertions, 0 deletions
diff --git a/lib/libpathconv/Makefile b/lib/libpathconv/Makefile
new file mode 100644
index 000000000000..6d7f5dce0e56
--- /dev/null
+++ b/lib/libpathconv/Makefile
@@ -0,0 +1,20 @@
+# $FreeBSD$
+
+.include <src.opts.mk>
+
+PACKAGE=lib${LIB}
+LIB= pathconv
+SHLIB_MAJOR= 1
+MAN= rel2abs.3 abs2rel.3
+INCS= pathconv.h
+SRCS= abs2rel.c rel2abs.c
+
+
+#VERSION_DEF= ${.CURDIR}/../libc/Versions.def
+#SYMBOL_MAPS= ${.CURDIR}/Symbol.map
+
+.if ${MK_TESTS} != "no"
+SUBDIR+= tests
+.endif
+
+.include <bsd.lib.mk>
diff --git a/lib/libpathconv/abs2rel.3 b/lib/libpathconv/abs2rel.3
new file mode 100644
index 000000000000..4d46d8a79b26
--- /dev/null
+++ b/lib/libpathconv/abs2rel.3
@@ -0,0 +1,136 @@
+.\"
+.\" Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+.\" Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd Dec 15, 1997"
+.Dt ABS2REL 3
+.Os
+.Sh NAME
+.Nm abs2rel
+.Nd make a relative path name from an absolute path name
+.Sh SYNOPSIS
+.Ft "char *"
+.Fn abs2rel "const char *path" "const char *base" "char *result" "size_t size"
+.Sh DESCRIPTION
+The
+.Fn abs2rel
+function makes a relative path name from an absolute path name
+.Fa path
+based on a directory
+.Fa base
+and copies the resulting path name into the memory referenced by
+.Fa result .
+The
+.Fa result
+argument must refer to a buffer capable of storing at least
+.Fa size
+characters.
+
+The resulting path name may include symbolic links.
+The
+.Fn abs2rel
+function doesn't check whether or not any path exists.
+.Sh "RETURN VALUES"
+The
+.Fn abs2rel
+function returns relative path name on success.
+If an error occurs,
+it returns
+.Dv NULL .
+.Sh ERRORS
+The
+.Fn abs2rel
+function may fail and set the external variable
+.Va errno
+to indicate the error.
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The
+.Fa base
+directory isn't an absolute path name or the
+.Fa size
+argument is zero.
+.It Bq Er ERANGE
+The
+.Fa size
+argument is greater than zero but smaller than the length of the pathname plus 1.
+.Sh EXAMPLE
+ char result[MAXPATHLEN];
+ char *path = abs2rel("/usr/src/sys", "/usr/local/lib", result, MAXPATHLEN);
+
+yields:
+
+ path == "../../src/sys"
+
+Similarly,
+
+ path1 = abs2rel("/usr/src/sys", "/usr", result, MAXPATHLEN);
+ path2 = abs2rel("/usr/src/sys", "/usr/src/sys", result, MAXPATHLEN);
+
+yields:
+
+ path1 == "src/sys"
+ path2 == "."
+
+.Sh BUGS
+If the
+.Fa base
+directory includes symbolic links,
+the
+.Fn abs2rel
+function produces the wrong path.
+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!! */
+
+You should convert the base directory into a real path in advance.
+.Pp
+
+ path = abs2rel("/sys/kern", realpath("/sys", resolvedname), result, MAXPATHLEN);
+
+yields:
+
+ path == "../../../sys/kern" /* It's correct but ... */
+
+That is correct, but a little redundant. If you wish get the simple
+answer 'kern', do the following.
+
+ path = abs2rel(realpath("/sys/kern", r1), realpath("/sys", r2),
+ result, MAXPATHLEN);
+
+The
+.Fn realpath
+function assures correct result, but don't forget that
+.Fn realpath
+requires that all but the last component of the path exist.
+.Sh "SEE ALSO"
+.Xr rel2abs 3
+.Sh AUTHORS
+Shigio Yamaguchi (shigio@tamacom.com)
diff --git a/lib/libpathconv/abs2rel.c b/lib/libpathconv/abs2rel.c
new file mode 100644
index 000000000000..f8ec712d70d5
--- /dev/null
+++ b/lib/libpathconv/abs2rel.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+ * Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "pathconv.h" /* prototypes */
+/*
+ * abs2rel: convert an absolute path name into relative.
+ *
+ * i) path absolute path
+ * i) base base directory (must be absolute path)
+ * o) result result buffer
+ * i) size size of result buffer
+ * r) != NULL: relative path
+ * == NULL: error
+ */
+char *
+abs2rel(const char *path, const char *base, char *result, const size_t size)
+{
+ const char *pp, *bp, *branch;
+ /*
+ * endp points the last position which is safe in the result buffer.
+ */
+ const char *endp = result + size - 1;
+ char *rp;
+
+ if (*path != '/') {
+ if (strlen(path) >= size)
+ goto erange;
+ strcpy(result, path);
+ goto finish;
+ } else if (*base != '/' || !size) {
+ errno = EINVAL;
+ return (NULL);
+ } else if (size == 1)
+ goto erange;
+ /*
+ * seek to branched point.
+ */
+ branch = path;
+ for (pp = path, bp = base; *pp && *bp && *pp == *bp; pp++, bp++)
+ if (*pp == '/')
+ branch = pp;
+ if ((*pp == 0 || (*pp == '/' && *(pp + 1) == 0)) &&
+ (*bp == 0 || (*bp == '/' && *(bp + 1) == 0))) {
+ rp = result;
+ *rp++ = '.';
+ if (*pp == '/' || *(pp - 1) == '/')
+ *rp++ = '/';
+ if (rp > endp)
+ goto erange;
+ *rp = 0;
+ goto finish;
+ }
+ if ((*pp == 0 && *bp == '/') || (*pp == '/' && *bp == 0))
+ branch = pp;
+ /*
+ * up to root.
+ */
+ rp = result;
+ for (bp = base + (branch - path); *bp; bp++)
+ if (*bp == '/' && *(bp + 1) != 0) {
+ if (rp + 3 > endp)
+ goto erange;
+ *rp++ = '.';
+ *rp++ = '.';
+ *rp++ = '/';
+ }
+ if (rp > endp)
+ goto erange;
+ *rp = 0;
+ /*
+ * down to leaf.
+ */
+ if (*branch) {
+ if (rp + strlen(branch + 1) > endp)
+ goto erange;
+ strcpy(rp, branch + 1);
+ } else
+ *--rp = 0;
+finish:
+ return result;
+erange:
+ errno = ERANGE;
+ return (NULL);
+}
diff --git a/lib/libpathconv/pathconv.h b/lib/libpathconv/pathconv.h
new file mode 100644
index 000000000000..a3327240a948
--- /dev/null
+++ b/lib/libpathconv/pathconv.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+ * Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+#ifndef _PATHCONV_H
+#define _PATHCONV_H
+char * rel2abs(const char *path, const char *base, char *result, const size_t size);
+char * abs2rel(const char *path, const char *base, char *result, const size_t size);
+
+#endif /*_PATHCONV_H */
+
diff --git a/lib/libpathconv/rel2abs.3 b/lib/libpathconv/rel2abs.3
new file mode 100644
index 000000000000..aee8039cf914
--- /dev/null
+++ b/lib/libpathconv/rel2abs.3
@@ -0,0 +1,98 @@
+.\"
+.\" Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+.\" Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd Dec 3, 1997"
+.Dt REL2ABS 3
+.Os
+.Sh NAME
+.Nm rel2abs
+.Nd make an absolute path name from a relative path name
+.Sh SYNOPSIS
+.Ft "char *"
+.Fn rel2abs "const char *path" "const char *base" "char *result" "size_t size"
+.Sh DESCRIPTION
+The
+.Fn rel2abs
+function makes an absolute path name from a relative path name
+.Fa path
+based on a directory
+.Fa base
+and copies the resulting path name into the memory referenced by
+.Fa result .
+The
+.Fa result
+argument must refer to a buffer capable of storing at least
+.Fa size
+character
+
+The resulting path name may include symbolic links.
+.Fn abs2rel
+doesn't check whether or not any path exists.
+.Sh "RETURN VALUES"
+The
+.Fn rel2abs
+function returns absolute path name on success.
+If an error occurs, it returns
+.Dv NULL .
+.Sh ERRORS
+The
+.Fn rel2abs
+function may fail and set the external variable
+.Va errno
+to indicate the error.
+.Bl -tag -width Er
+.It Bq Er EINVAL
+The
+.Fa base
+directory isn't an absolute path name or the
+.Fa size
+argument is zero.
+.It Bq Er ERANGE
+The
+.Fa size
+argument is greater than zero but smaller than the length of the pathname plus 1
+.Sh EXAMPLE
+ char result[MAXPATHLEN];
+ char *path = rel2abs("../../src/sys", "/usr/local/lib", result, MAXPATHLEN);
+
+yields:
+
+ path == "/usr/src/sys"
+
+Similarly,
+
+ path1 = rel2abs("src/sys", "/usr", result, MAXPATHLEN);
+ path2 = rel2abs(".", "/usr/src/sys", result, MAXPATHLEN);
+
+yields:
+
+ path1 == "/usr/src/sys"
+ path2 == "/usr/src/sys"
+.Sh "SEE ALSO"
+.Xr abs2rel 3
+.Sh AUTHORS
+Shigio Yamaguchi (shigio@tamacom.com)
diff --git a/lib/libpathconv/rel2abs.c b/lib/libpathconv/rel2abs.c
new file mode 100644
index 000000000000..ac1e898ca22e
--- /dev/null
+++ b/lib/libpathconv/rel2abs.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+ * Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include "pathconv.h" /* prototypes */
+/*
+ * rel2abs: convert an relative path name into absolute.
+ *
+ * i) path relative path
+ * i) base base directory (must be absolute path)
+ * o) result result buffer
+ * i) size size of result buffer
+ * r) != NULL: absolute path
+ * == NULL: error
+ */
+char *
+rel2abs(const char *path, const char *base, char *result, const size_t size)
+{
+ const char *pp, *bp;
+ /*
+ * endp points the last position which is safe in the result buffer.
+ */
+ const char *endp = result + size - 1;
+ char *rp;
+ size_t length;
+
+ if (*path == '/') {
+ if (strlen(path) >= size)
+ goto erange;
+ strcpy(result, path);
+ goto finish;
+ } else if (*base != '/' || !size) {
+ errno = EINVAL;
+ return (NULL);
+ } else if (size == 1)
+ goto erange;
+
+ length = strlen(base);
+
+ if (!strcmp(path, ".") || !strcmp(path, "./")) {
+ if (length >= size)
+ goto erange;
+ strcpy(result, base);
+ /*
+ * rp points the last char.
+ */
+ rp = result + length - 1;
+ /*
+ * remove the last '/'.
+ */
+ if (*rp == '/') {
+ if (length > 1)
+ *rp = 0;
+ } else
+ rp++;
+ /* rp point NULL char */
+ if (*++path == '/') {
+ /*
+ * Append '/' to the tail of path name.
+ */
+ *rp++ = '/';
+ if (rp > endp)
+ goto erange;
+ *rp = 0;
+ }
+ goto finish;
+ }
+ bp = base + length;
+ if (*(bp - 1) == '/')
+ --bp;
+ /*
+ * up to root.
+ */
+ for (pp = path; *pp && *pp == '.'; ) {
+ if (!strncmp(pp, "../", 3)) {
+ pp += 3;
+ while (bp > base && *--bp != '/')
+ ;
+ } else if (!strncmp(pp, "./", 2)) {
+ pp += 2;
+ } else if (!strncmp(pp, "..\0", 3)) {
+ pp += 2;
+ while (bp > base && *--bp != '/')
+ ;
+ } else
+ break;
+ }
+ /*
+ * down to leaf.
+ */
+ length = bp - base;
+ if (length >= size)
+ goto erange;
+ strncpy(result, base, length);
+ rp = result + length;
+ if (*pp || *(pp - 1) == '/' || length == 0)
+ *rp++ = '/';
+ if (rp + strlen(pp) > endp)
+ goto erange;
+ strcpy(rp, pp);
+finish:
+ return result;
+erange:
+ errno = ERANGE;
+ return (NULL);
+}
diff --git a/lib/libpathconv/tests/Makefile b/lib/libpathconv/tests/Makefile
new file mode 100644
index 000000000000..c7696c2d6c5d
--- /dev/null
+++ b/lib/libpathconv/tests/Makefile
@@ -0,0 +1,10 @@
+# $FreeBSD$
+
+TAP_TESTS_C+= abs2rel
+TAP_TESTS_C+= rel2abs
+
+#LIBADD+= pathconv
+#LDADD+= -L .. -lpathconv
+LDADD+= ../libpathconv.a
+
+.include <bsd.test.mk>
diff --git a/lib/libpathconv/tests/abs2rel.c b/lib/libpathconv/tests/abs2rel.c
new file mode 100644
index 000000000000..f0a0a0d8a9c3
--- /dev/null
+++ b/lib/libpathconv/tests/abs2rel.c
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+ * Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/param.h>
+#include <errno.h>
+#include "../pathconv.h"
+
+int
+main(int argc, char *argv[])
+{
+ char result[MAXPATHLEN];
+ char cwd[MAXPATHLEN];
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: abs2rel path [base]\n");
+ exit(1);
+ }
+ if (argc == 2) {
+ if (!getcwd(cwd, MAXPATHLEN)) {
+ fprintf(stderr, "cannot get current directory.\n");
+ exit(1);
+ }
+ } else
+ strcpy(cwd, argv[2]);
+
+ if (abs2rel(argv[1], cwd, result, MAXPATHLEN)) {
+ printf("%s\n", result);
+ } else
+ printf("ERROR\n");
+ exit(0);
+}
diff --git a/lib/libpathconv/tests/rel2abs.c b/lib/libpathconv/tests/rel2abs.c
new file mode 100644
index 000000000000..90624bbebafb
--- /dev/null
+++ b/lib/libpathconv/tests/rel2abs.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+ * Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/param.h>
+#include <errno.h>
+#include "../pathconv.h"
+
+int
+main(int argc, char *argv[])
+{
+ char result[MAXPATHLEN];
+ char cwd[MAXPATHLEN];
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: rel2abs path [base]\n");
+ exit(1);
+ }
+ if (argc == 2) {
+ if (!getcwd(cwd, MAXPATHLEN)) {
+ fprintf(stderr, "cannot get current directory.\n");
+ exit(1);
+ }
+ } else
+ strcpy(cwd, argv[2]);
+
+ if (rel2abs(argv[1], cwd, result, MAXPATHLEN)) {
+ printf("%s\n", result);
+ } else
+ printf("ERROR\n");
+ exit(0);
+}
diff --git a/lib/libpathconv/tests/test.pl b/lib/libpathconv/tests/test.pl
new file mode 100755
index 000000000000..7be2ab82eac4
--- /dev/null
+++ b/lib/libpathconv/tests/test.pl
@@ -0,0 +1,146 @@
+#!/usr/bin/perl
+#
+# Copyright (c) 1997 Shigio Yamaguchi. All rights reserved.
+# Copyright (c) 1999 Tama Communications Corporation. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+#
+
+#
+# Test script for abs2rel(3) and rel2abs(3).
+#
+$logfile = 'err';
+#
+# target base directory result
+# --------------------------------------
+@abs2rel = (
+ '. / .',
+ 'a/b/c / a/b/c',
+ 'a/b/c /a a/b/c',
+ '/a/b/c a ERROR',
+);
+@rel2abs = (
+ '. / /',
+ './ / /',
+ '/a/b/c / /a/b/c',
+ '/a/b/c /a /a/b/c',
+ 'a/b/c a ERROR',
+ '.. /a /',
+ '../ /a /',
+ '../.. /a /',
+ '../../ /a /',
+ '../../.. /a /',
+ '../../../ /a /',
+ '../b /a /b',
+ '../b/ /a /b/',
+ '../../b /a /b',
+ '../../b/ /a /b/',
+ '../../../b /a /b',
+ '../../../b/ /a /b/',
+ '../b/c /a /b/c',
+ '../b/c/ /a /b/c/',
+ '../../b/c /a /b/c',
+ '../../b/c/ /a /b/c/',
+ '../../../b/c /a /b/c',
+ '../../../b/c/ /a /b/c/',
+);
+@common = (
+ '/a/b/c /a/b/c .',
+ '/a/b/c /a/b/ c',
+ '/a/b/c /a/b c',
+ '/a/b/c /a/ b/c',
+ '/a/b/c /a b/c',
+ '/a/b/c / a/b/c',
+ '/a/b/c /a/b/c .',
+ '/a/b/c /a/b/c/ .',
+ '/a/b/c/ /a/b/c ./',
+ '/a/b/ /a/b/c ../',
+ '/a/b /a/b/c ..',
+ '/a/ /a/b/c ../../',
+ '/a /a/b/c ../..',
+ '/ /a/b/c ../../../',
+ '/a/b/c /a/b/z ../c',
+ '/a/b/c /a/y/z ../../b/c',
+ '/a/b/c /x/y/z ../../../a/b/c',
+);
+print "TEST start ";
+open(LOG, ">$logfile") || die("cannot open log file '$logfile'.\n");
+$cnt = 0;
+$progname = 'abs2rel';
+foreach (@abs2rel) {
+ @d = split;
+ chop($result = `./$progname $d[0] $d[1]`);
+ if ($d[2] eq $result) {
+ print '.';
+ } else {
+ print 'X';
+ print LOG "$progname $d[0] $d[1] -> $result (It should be '$d[2]')\n";
+ $cnt++;
+ }
+}
+foreach (@common) {
+ @d = split;
+ chop($result = `./$progname $d[0] $d[1]`);
+ if ($d[2] eq $result) {
+ print '.';
+ } else {
+ print 'X';
+ print LOG "$progname $d[0] $d[1] -> $result (It should be '$d[2]')\n";
+ $cnt++;
+ }
+}
+$progname = 'rel2abs';
+foreach (@rel2abs) {
+ @d = split;
+ chop($result = `./$progname $d[0] $d[1]`);
+ if ($d[2] eq $result) {
+ print '.';
+ } else {
+ print 'X';
+ print LOG "$progname $d[0] $d[1] -> $result (It should be '$d[2]')\n";
+ $cnt++;
+ }
+}
+foreach (@common) {
+ @d = split;
+ chop($result = `./$progname $d[2] $d[1]`);
+ if ($d[0] eq $result) {
+ print '.';
+ } else {
+ print 'X';
+ print LOG "$progname $d[2] $d[1] -> $result (It should be '$d[0]')\n";
+ $cnt++;
+ }
+}
+close(LOG);
+if ($cnt == 0) {
+ print " COMPLETED.\n";
+} else {
+ print " $cnt errors detected.\n";
+ open(LOG, $logfile) || die("log file not found.\n");
+ while (<LOG>) {
+ print;
+ }
+ close(LOG);
+}