pathsplit, adirname, abasename \- split a path into directory name and base name
#include <pathfns.h>
int pathsplit(const char *path, char **basenamep, char **dirnamep);
char *adirname(const char *path);
char *abasename(const char *path);
pathsplit splits a path into two components: the directory name and the base name. These are copied into buffers allocated via malloc(3), a null terminator added, and returned via the basenamep and dirnamep pointers. Either or both of these may be a null pointer.
adirname is similar, except that it just returns the directory name. Similarly abasename returns the base name.
In all cases the transformation is entirely textual; the filesystem is not consulted, so it is irrelevant whether any of the files and directories involved actually exist.
The directory name is the directory containing the file named path; the base name is the name of the file within that directory. If path is the empty string then both are defined to be ".". If path is a null pointer then the behaviour is undefined.
If the return value is 0 then the operation succeeded. If basenamep is not a null pointer then *basenamep will point to a newly allocated string containing the base name of the file. If dirnamep is not a null pointer then *dirnamep will point to a newly allocated string containing the name of the directory.
If the return value is -1 then an error occurred and errno will be set appropriately (see below).
No other values are returned.
If the return value is not a null pointer then the operation succeeded and the return value is a pointer to a newly allocated string containing the result.
If the return value is a null pointer then an error occurred and errno will be set appropriately (see below).
These functions propagate any errno value set by malloc(3), which would typically be:
ENOMEM |
Insufficient memory. |
dirname(3) and basename(3) perform similar operations to these functions, but may either modify their arguments, or return pointers to statically allocated data, neither of which are acceptable.
dirname(3), basename(3), malloc(3), free(3)