path handling functions
More...
#include <errno.h>
#include <sys/stat.h>
#include "snprintf.h"
Go to the source code of this file.
#define | NSPATH_MKDIR_SKIP_LAST (1 << 0) |
| Don't mkdir() last element of path when calling nspath_mkdir_p()
|
|
char * | nspath_normalize (const char *orig_path) |
| Normalize a path By "normalize", we mean that we convert dot-slash and dot-dot-slash embedded components into a legible continuous string of characters.
|
|
char * | nspath_absolute (const char *rel_path, const char *base) |
| Make the "base"-relative path "rel_path" absolute.
|
|
char * | nspath_real (const char *rel_path, const char *base) |
| Canonicalize the "base"-relative path "rel_path".
|
|
char * | nspath_absolute_dirname (const char *path, const char *base) |
| Get absolute dirname of "path", relative to "base".
|
|
int | nspath_mkdir_p (const char *path, mode_t mode, int options) |
| Recursively create a directory, just like mkdir_p would.
|
|
path handling functions
This library handles path normalization and resolution. It's nifty if you want to turn relative paths into absolute ones, or if you want to make insane ones sane, but without chdir()'ing your way around the filesystem.
◆ nspath_absolute()
char * nspath_absolute |
( |
const char * | rel_path, |
|
|
const char * | base ) |
|
extern |
Make the "base"-relative path "rel_path" absolute.
Turns the relative path "rel_path" into an absolute path and resolves it as if we were currently in "base". If "base" is NULL, the current working directory is used. If "base" is not null, it should be an absolute path for the result to make sense.
- Parameters
-
rel_path | The relative path to convert |
base | The base directory (if NULL, we use current working dir) |
- Returns
- A newly allocated string containing the absolute path
◆ nspath_absolute_dirname()
char * nspath_absolute_dirname |
( |
const char * | path, |
|
|
const char * | base ) |
|
extern |
Get absolute dirname of "path", relative to "base".
- Parameters
-
path | Full path to target object (file or subdir) |
base | The base directory (if NULL, we use current working dir) |
- Returns
- NULL on errors, allocated absolute directory name on success
◆ nspath_mkdir_p()
int nspath_mkdir_p |
( |
const char * | path, |
|
|
mode_t | mode, |
|
|
int | options ) |
|
extern |
Recursively create a directory, just like mkdir_p would.
- Note
- This function will taint errno with ENOENT if any path component has to be created.
-
If "path" has a trailing slash, NSPATH_MKDIR_SKIP_LAST won't have any effect. That's considered a feature, since the option is designed so one can send a file-path to the function and have it create the directory structure for it.
- Parameters
-
path | Path to create, in normalized form |
mode | Filemode (same as mkdir() takes) |
options | Options flag. See NSPATH_MKDIR_* for or-able options |
- Returns
- 0 on success, -1 on errors and errno will hold error code from either stat() or mkdir().
◆ nspath_normalize()
char * nspath_normalize |
( |
const char * | orig_path | ) |
|
|
extern |
Normalize a path By "normalize", we mean that we convert dot-slash and dot-dot-slash embedded components into a legible continuous string of characters.
Leading and trailing slashes are kept exactly as they are in input, but with sequences of slashes reduced to a single one.
"foo/bar/.././lala.txt" becomes "foo/lala.txt" "../../../../bar/../foo/" becomes "/foo/" "////foo////././bar" becomes "/foo/bar"
- Parameters
-
orig_path | The path to normalize |
- Returns
- A newly allocated string containing the normalized path
◆ nspath_real()
char * nspath_real |
( |
const char * | rel_path, |
|
|
const char * | base ) |
|
extern |
Canonicalize the "base"-relative path "rel_path".
errno gets properly set in case of errors.
- Parameters
-
rel_path | The path to transform |
base | The base we should operate relative to |
- Returns
- Newly allocated canonical path on success, NULL on errors