From owner-svn-src-head@freebsd.org Wed Nov 23 07:57:54 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8F018C50A1A; Wed, 23 Nov 2016 07:57:54 +0000 (UTC) (envelope-from julian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 3ED4A36A; Wed, 23 Nov 2016 07:57:54 +0000 (UTC) (envelope-from julian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id uAN7vrEJ008898; Wed, 23 Nov 2016 07:57:53 GMT (envelope-from julian@FreeBSD.org) Received: (from julian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id uAN7vqmC008888; Wed, 23 Nov 2016 07:57:52 GMT (envelope-from julian@FreeBSD.org) Message-Id: <201611230757.uAN7vqmC008888@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: julian set sender to julian@FreeBSD.org using -f From: Julian Elischer Date: Wed, 23 Nov 2016 07:57:52 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r309035 - in head/lib/libpathconv: . tests X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 23 Nov 2016 07:57:54 -0000 Author: julian Date: Wed Nov 23 07:57:52 2016 New Revision: 309035 URL: https://svnweb.freebsd.org/changeset/base/309035 Log: 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 Added: head/lib/libpathconv/ head/lib/libpathconv/Makefile (contents, props changed) head/lib/libpathconv/abs2rel.3 (contents, props changed) head/lib/libpathconv/abs2rel.c (contents, props changed) head/lib/libpathconv/pathconv.h (contents, props changed) head/lib/libpathconv/rel2abs.3 (contents, props changed) head/lib/libpathconv/rel2abs.c (contents, props changed) head/lib/libpathconv/tests/ head/lib/libpathconv/tests/Makefile (contents, props changed) head/lib/libpathconv/tests/abs2rel.c (contents, props changed) head/lib/libpathconv/tests/rel2abs.c (contents, props changed) head/lib/libpathconv/tests/test.pl (contents, props changed) Added: head/lib/libpathconv/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/Makefile Wed Nov 23 07:57:52 2016 (r309035) @@ -0,0 +1,20 @@ +# $FreeBSD$ + +.include + +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 Added: head/lib/libpathconv/abs2rel.3 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/abs2rel.3 Wed Nov 23 07:57:52 2016 (r309035) @@ -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) Added: head/lib/libpathconv/abs2rel.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/abs2rel.c Wed Nov 23 07:57:52 2016 (r309035) @@ -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 +#include +#include +#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); +} Added: head/lib/libpathconv/pathconv.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/pathconv.h Wed Nov 23 07:57:52 2016 (r309035) @@ -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 */ + Added: head/lib/libpathconv/rel2abs.3 ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/rel2abs.3 Wed Nov 23 07:57:52 2016 (r309035) @@ -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) Added: head/lib/libpathconv/rel2abs.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/rel2abs.c Wed Nov 23 07:57:52 2016 (r309035) @@ -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 +#include +#include +#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); +} Added: head/lib/libpathconv/tests/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/tests/Makefile Wed Nov 23 07:57:52 2016 (r309035) @@ -0,0 +1,10 @@ +# $FreeBSD$ + +TAP_TESTS_C+= abs2rel +TAP_TESTS_C+= rel2abs + +#LIBADD+= pathconv +#LDADD+= -L .. -lpathconv +LDADD+= ../libpathconv.a + +.include Added: head/lib/libpathconv/tests/abs2rel.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/tests/abs2rel.c Wed Nov 23 07:57:52 2016 (r309035) @@ -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 +#include +#include +#include +#include +#include +#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); +} Added: head/lib/libpathconv/tests/rel2abs.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/tests/rel2abs.c Wed Nov 23 07:57:52 2016 (r309035) @@ -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 +#include +#include +#include +#include +#include +#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); +} Added: head/lib/libpathconv/tests/test.pl ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libpathconv/tests/test.pl Wed Nov 23 07:57:52 2016 (r309035) @@ -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 () { + print; + } + close(LOG); +}