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
-
dest - Destination string
-
len - Length of destination string
-
num_parts - Number of parts provided
-
parts - Parts of path
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
-
iter - pre-allocated split path iterator
-
max - maximum number parts which will be processed
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
-
result - resulting buffer that contains all elements of abspath but path
-
len - length of result buffer
-
abspath - absolute path to be filtered
-
path - path element
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
-
cfgdir - resulting buffer containing directory with config
-
cfglen - length of cfgdir buffer
-
cfgfname - default file name for configuration file
-
arg - argument entered by user
RETURN VALUESargdir
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
-
linkpath - resulting buffer containing link destination
-
linklen - length of
linkpath
-
path - path to symbolic link
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;