## ## OSSP path - Filesystem Path Manipulation ## Copyright (c) 2002-2003 Ralf S. Engelschall ## Copyright (c) 2002-2003 The OSSP Project ## Copyright (c) 2002-2003 Cable & Wireless Deutschland ## ## This file is part of OSSP path, a filesystem path manipulation library ## which can be found at http://www.ossp.org/pkg/lib/path/. ## ## Permission to use, copy, modify, and distribute this software for ## any purpose with or without fee is hereby granted, provided that ## the above copyright notice and this permission notice appear in all ## copies. ## ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR ## 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. ## ## path.pod: manual page ## =pod =head1 NAME B - Filesystem Path Manipulation =head1 SYNOPSIS #include "path.h" char *B(char *I, const size_t I, const char *I, const char *I); char *B(char *I, const size_t I, const char *I, const char *I); char *B(char *I, const size_t I, const char *I); char *B(char *I, const size_t I, const char *I); char *B(char *I, const size_t I, const char *I); =head1 DESCRIPTION B is a filesystem path manipulation library. It allows one to convert between absolute and relative paths and to resolve symbolic links in a path. =head1 APPLICATION PROGRAMMING INTERFACE (API) The following Application Programming Interface (API) is provided by B: =over 4 =item char *B(char *I, const size_t I, const char *I, const char *I); This function make a relative path name from an absolute path name I based on a reference directory I. The generated relative path name is stored into the caller supplied buffer at I, but never more than I bytes are written. The resulting path name may include symbolic links. The function doesn't check whether or not any path exists. The function returns a pointer to I (the relative path name) on success, and C on error. The function may fail with the following errors and set the external variable C to indicate the particular error: C (the I directory isn't an absolute path name or the I argument is zero) or C (the I argument is greater than zero but smaller than the length of the pathname plus 1). =item char *B(char *I, const size_t I, const char *I, const char *I); This function make an absolute path name from a relative path name I based on a reference directory I. The generated absolute path name is stored into the caller supplied buffer at I, but never more than I bytes are written. The resulting path name may include symbolic links. The function doesn't check whether or not any path exists. The function returns a pointer to I (the absolute path name) on success, and C on error. The function may fail with the following errors and set the external variable C to indicate the particular error: C (the I directory isn't an absolute path name or the I argument is zero) or C (the I argument is greater than zero but smaller than the length of the pathname plus 1). =item char *B(char *I, const size_t I, const char *I); This function resolves a path name I into the corresponding canonicalized absolute pathname. The generated absolute path name is stored into the caller supplied buffer at I, but never more than I bytes are written. The function is capable to resolve symbolic links, extra "C" characters and references to "C" and "C". The function returns a pointer to I (the resolved path name) on success, and C on error. The function may fail with an error and set the external variable C for any of the errors specified for the library functions chdir(2), close(2), fchdir(2), lstat(2), open(2), readlink(2) and getcwd(3). =item char *B(char *I, const size_t I, const char *I); This function extracts the directory filename part from I and stores it into the caller supplied buffer at I, but never more than I bytes are written. The function returns a pointer to I on success, and C on error. =item char *B(char *I, const size_t I, const char *I); This function extracts the base filename part from I and stores it into the caller supplied buffer at I, but never more than I bytes are written. The function returns a pointer to I on success, and C on error. =back =head1 EXAMPLE The code char result[MAXPATHLEN]; char *path = path_abs2rel(result, sizeof(result), "/usr/src/sys", "/usr/local/lib"); yields: path == "../../src/sys" Similarly, path1 = path_abs2rel(result, sizeof(result), "/usr/src/sys", "/usr"); path2 = path_abs2rel(result, sizeof(result), "/usr/src/sys", "/usr/src/sys"); yields: path1 == "src/sys" path2 == "." And char *path = path_rel2abs(result, sizeof(result), "../../src/sys", "/usr/local/lib"); yields: path == "/usr/src/sys" Similarly, path1 = rel2abs(result, sizeof(result), "src/sys", "/usr"); path2 = rel2abs(result, sizeof(result), ".", "/usr/src/sys"); yields: path1 == "/usr/src/sys" path2 == "/usr/src/sys" =head1 BUGS If the base directory includes symbolic links, the abs2rel(3) function produces the wrong path. For example, if 'C' is a symbolic link to 'C', char *path = path_abs2rel(result, sizeof(result), "/usr/local/lib", "/sys"); yields: path == "../usr/local/lib" /* It's wrong!! */ You should convert the base directory into a real path in advance. path = path_abs2rel(result, sizeof(result), "/sys/kern", path_resolve(result2, sizeof(result2), "/sys")); This then yields: path == "../../../sys/kern" /* It's correct but ... */ That is correct, but a little redundant. If you wish get the simple answer 'C', do the following. path = path_abs2rel(r0, sizeof(r0), path_resolve(r1, sizeof(r1), "/sys/kern"), path_resolve(r2, sizeof(r2), "/sys")); The path_resolve(3) function assures correct result, but don't forget that path_resolve(3) requires that all but the last component of the path exist. =head1 SEE ALSO =head1 AUTHORS The path_abs2rel(3) and path_rel2abs(3) function was originally written in December 1997 by I for his I project. The path_resolve(3) function is derived from FreeBSD's realpath(3) function which was written in February 1994 by I. The work off of this code and the B library as a whole was written in January 2000 by I for use in the B project. =cut