Path processing utilities

Constants

PATHMAXLEN, PATHPARTMAXLEN


Maximum length of path

#define PATHMAXLEN  1024
#define PATHPARTMAXLEN  256

Functions

path_join_array

public


Create path from array into string. Similiar to python's os.path.join

ARGUMENTS

RETURN VALUES
destination with formed path or NULL if destination was overflown

LIBEXPORT char* path_join_array(char* dest, size_t len, int num_parts, const char** parts)

path_join

public


Create path from various arguments

Example: path_join(path, PATHMAXLEN, "/tmp", tmpdir, "file", NULL)

NOTES
Last argument should be always NULL

REFERENCE
path_join_array

LIBEXPORT char* path_join(char* dest, size_t len, ...)

path_join_aas

public


Join paths into auto-allocated string

NOTES
Last argument should be always NULL

REFERENCE
path_join
aas_init
aas_free

LIBEXPORT char* path_join_aas(char** aas, ...)

path_split

public


Split path into parts. Saves result into path iterator which may be walked using
path_split_next. Returns first part of path or NULL if max is too large.

If max value is negative, path_split would put paths in reverse order, so
first part would be filename.

Uses iter as temporary storage, it is re-enterable.

First (or last for reverse order) component is root path item. For Windows it is
drive letter, on POSIX - empty string.

ARGUMENTS

RETURN VALUES
Pointer to first part or NULL in case of error

NOTES
if number of parts exceeds PATHMAXPARTS, will fail and returns NULL

LIBEXPORT const char* path_split(path_split_iter_t* iter, int max, const char* path)

path_split_next

public


Returns next path part from iterator or NULL if all parts was
walked.

LIBEXPORT const char* path_split_next(path_split_iter_t* iter)

path_split_reset

public


Resets iterator to beginning

LIBEXPORT const char* path_split_reset(path_split_iter_t* iter)

path_dirname

public


Returns directory name. Uses iter as temporary storage

NOTES
uses path_split

STATIC_INLINE const char* path_dirname(path_split_iter_t* iter, const char* path) 

path_basename

public


Returns name of file. Uses iter as temporary storage

NOTES
uses path_split

STATIC_INLINE const char* path_basename(path_split_iter_t* iter, const char* path) 

path_remove

public


Remove relative path element path from the end of absolute path.

For example:
path_remove("/opt/tsload/var/tsload", "var/tsload") -> "/opt/tsload"

ARGUMENTS

RETURN VALUES
NULL if path is not element of abspath or result

LIBEXPORT char* path_remove(char* result, size_t len, const char* abspath, const char* path)

path_argfile

public


Based on argument arg provided by user deduces if he provided
path to configuration file (which name is defined by cfgfname)
or to a directory containing it.

ARGUMENTS

RETURN VALUES
argdir

NOTES
This function doesn't check if file exists, directory is accessible, etc.

LIBEXPORT char* path_argfile(char* cfgdir, size_t len, const char* cfgfname, const char* arg)

path_abslink

public


Read symbolic link and if its destination is not absolute,
make it relative to path. If path was absolute, will make destination
path absolute too.

ARGUMENTS

RETURN VALUES
NULL if readlink()/path_split() was unsuccessful or path_join_array result

REFERENCE
path_join_array

LIBEXPORT char* path_abslink(char* linkpath, size_t linklen, const char* path)

Types

path_split_iter_t


Temporary storage for path splitting operations

typedef struct {
    char ps_storage[PATHMAXLEN];
    char* ps_dest;

    int     ps_part;
    int     ps_num_parts;
    char*  ps_parts[PATHMAXPARTS];

#ifdef PLAT_POSIX
    boolean_t ps_last_is_root;
#endif
} path_split_iter_t;