Nagios 4.5.4
Dev docs for Nagios core and neb-module hackers
Loading...
Searching...
No Matches
nspath.h File Reference

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.
 

Detailed Description

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.

Function Documentation

◆ 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_pathThe relative path to convert
baseThe 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
pathFull path to target object (file or subdir)
baseThe 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
pathPath to create, in normalized form
modeFilemode (same as mkdir() takes)
optionsOptions 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_pathThe 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_pathThe path to transform
baseThe base we should operate relative to
Returns
Newly allocated canonical path on success, NULL on errors