1 #ifndef PATH_UTIL_H
2 #define PATH_UTIL_H
3 
4 /* Returns path as the normalized absolute path, which means that './'
5  * and '../' components are resolved, and that duplicate and trailing
6  * slashes are removed. If it's not already the absolute path, it's
7  * assumed to be relative to the current working directory.
8  *
9  * NOTE: Be careful with this function. The resolution of '../' components
10  * with the parent component as if it were a normal directory is not valid
11  * if the path contains symbolic links.
12  *
13  * Returns 0 on success, and -1 on failure. errno and error_r are set on
14  * failure, and error_r cannot be NULL.
15  */
16 int t_normpath(const char *path, const char **npath_r, const char **error_r);
17 /* Like t_normpath(), but path is relative to given root. */
18 int t_normpath_to(const char *path, const char *root, const char **npath_r,
19 		  const char **error_r);
20 
21 /* Returns path as the real normalized absolute path, which means that all
22  * symbolic links in the path are resolved, that './' and '../' components
23  * are resolved, and that duplicate and trailing slashes are removed. If it's
24  * not already the absolute path, it's assumed to be relative to the current
25  * working directory.
26  *
27  * NOTE: This function calls stat() for each path component and more when
28  * there are symbolic links (just like POSIX realpath()).
29  *
30  * Returns 0 on success, and -1 on failure. errno and error_r are set on
31  * failure, and error_r cannot be NULL.
32  */
33 int t_realpath(const char *path, const char **npath_r, const char **error_r);
34 /* Like t_realpath(), but path is relative to given root. */
35 int t_realpath_to(const char *path, const char *root, const char **npath_r,
36 		  const char **error_r);
37 
38 /* Returns path as absolute path. If it's not already absolute path,
39  * it's assumed to be relative to current working directory.
40  *
41  * In the t_abspath functions, the returned paths are not normalized. This
42  * means that './' and '../' are not resolved, but they left in the returned
43  * path as given in the parameters. Symbolic links are not resolved either.
44  *
45  * Returns 0 on success, and -1 on failure. error_r is set on failure, and
46  * cannot be NULL.
47  */
48 int t_abspath(const char *path, const char **abspath_r, const char **error_r);
49 /* Like t_abspath(), but path is relative to given root. */
50 const char *t_abspath_to(const char *path, const char *root);
51 
52 /* Get current working directory allocated from data stack. Returns 0 on
53  * success and 1 on failure. error_r is set on failure and cannot be NULL. */
54 int t_get_working_dir(const char **dir_r, const char **error_r);
55 
56 /* Get symlink destination allocated from data stack. Returns 0 on success and
57  * -1 on failure. error_r is set on failure and cannot be NULL. */
58 int t_readlink(const char *path, const char **dest_r, const char **error_r);
59 
60 /* Update binpath to be absolute:
61  * a) begins with '/' -> no change
62  * b) contains '/' -> assume relative to working directory
63  * c) set to first executable that's found from $PATH
64  *
65  * error_r is set on failure, and cannot be NULL.
66  */
67 bool t_binary_abspath(const char **binpath, const char **error_r);
68 
69 #endif
70