PATHUTILS(3X) — Subroutines
NAME
pathbase, pathdir, pathcollapse − various file-path manipulation utilities
SYNOPSIS
#include <codelib/pathutils.h>
char ∗pathbase(char ∗src);
const char ∗pathbase(const char ∗src);
char ∗pathdir(const char ∗src, char ∗dst = NULL);
char ∗pathcollapse(const char ∗src, char ∗dst = NULL,
boolean show_dir = FALSE);
CC ... -lcodelibs
DESCRIPTION
These functions provide some handy ways to manage file-path names. With the exception of pathbase, all of the functions accept both a src and a dst argument. Src should be a pointer to the source path, and dst should be one of the following three values:
NULL Indicates that the function should allocate space for the result using malloc(3C). The user should free the returned value using free(3C).
src Indicates that the source path should be modified in place. The results of all pathutils(3X) functions will be shorter or the same length as the original value.
OTHER A pointer to a suitably sized buffer to hold the result.
pathbase
Pathbase returns a pointer into src which points to the base name (see basename(1)) of the path. The base name of a path is the portion of the string following the last ´/´ character. If the ´/´ character does not occur in src, then src will be returned. If src ends in a ´/´, a pointer to the trailing NULL (´\0´) character will be returned.
pathdir
Pathdir returns the directory portion of a path, which is the portion of a string up to the last ´/´ character (see dirname(1)). If there is no ´/´ in the path, then the string "." is returned for the current directory. If the path is ´/´, then the entire path is returned.
pathcollapse
Pathcollapse collapses src into its simplest form. This is done by removing redundant ´/´ characters and ´.´ components, as well as removing ´directory/..´ pairs, where possible. Trailing ´/´ characters are considered to be redundant.
This simplification is done without examining the filesystem, so removal of redundant HP−UX context-dependent file components is not done. Also, relative pathnames will remain relative, and absolute pathnames will remain absolute.
If the show_dir argument is TRUE, a redundant trailing ´/´ character will be appended to the resulting path if the original appeared to refer to a directory. Specifically, a trailing slash will be present if the original path ended in ´/´, or if it ended in a ´.´ or ´..´ component.
WARNING
Since pathcollapse does not examine the filesystem, an invalid path may inadvertently be made valid. For example, “/tmp/xxx/..” would become “/tmp” even if the directory “/tmp/xxx” didn’t exist. If this condition needs to be detected, simply check the original path using access(2) before simplifying.
NOTE
Pathbase provides the following interface for C and ANSI C since overloading isn’t supported:
char ∗pathbase(const char ∗src);
SEE ALSO
stringx(3X), access(2), cdf(4), basename(1), dirname(1).
NOTES
Supports HP-NLS 8-bit and 16-bit strings.
— codelibs — C++