1 /* Licensed under BSD-MIT - see LICENSE file for details */
4 #include <ccan/tal/tal.h>
8 * path_cwd - get current directory.
9 * @ctx: the context to tal from
11 * Returns NULL and sets errno on error.
13 char *path_cwd(const tal_t *ctx);
16 * path_readlink - get a symbolic link contents
17 * @ctx: the context to tal the result from
18 * @link: the link to read (can be take())
20 * Returns NULL and sets errno on error, otherwise returns nul-terminated
23 char *path_readlink(const tal_t *ctx, const char *link TAKES);
26 * path_canon - return the canonical absolute pathname.
27 * @ctx: the context to tal the result from.
28 * @a: path to canonicalize (can be take())
30 * Returns NULL and sets errno on error, otherwise returns an absolute
31 * path with no symbolic links and no extra separators (ie. as per
34 char *path_canon(const tal_t *ctx, const char *a TAKES);
37 * path_simplify - remove double-/, ./ and some ../, plus trailing /.
38 * @ctx: the context to tal the result from
39 * @a: path to simplify (can be take())
41 * Unlike path_canon(), this routine does not convert a path to absolute
42 * terms or remove symlinks, but it does neaten it by removing extraneous
45 char *path_simplify(const tal_t *ctx, const char *a TAKES);
48 * path_join - attach one path to another.
49 * @ctx: the context to tal the result from
50 * @base: the path to start at (can be take())
51 * @a: the path to head from there (can be take())
53 * If @a is an absolute path, return a copy of it. Otherwise, attach
56 char *path_join(const tal_t *ctx, const char *base TAKES, const char *a TAKES);
59 * path_pushd - save old dir and change to a new one.
60 * @ctx: the context to tal the result from
61 * @dir: the directory to return to (can be take())
63 struct path_pushd *path_pushd(const tal_t *ctx, const char *dir TAKES);
66 * path_popd - return to old, path_pushd dir.
67 * @olddir: the return from a previous path_pushd.
69 * Returns false and sets errno if it fails.
71 bool path_popd(struct path_pushd *olddir);
74 * path_rel - get relative path from a to b.
75 * @ctx: the context to tal the result from.
76 * @fromdir: the starting location (can be take())
77 * @to: the destination location (can be take())
79 * This returns a relative path which leads from @fromdir (assumed to be a
80 * directory) to @to. If @ctx it TAL_TAKE, frees both @fromdir and @to.
83 * char *path = path_rel(NULL, "/tmp", "/");
84 * assert(strcmp(path, "..") == 0);
86 char *path_rel(const tal_t *ctx,
87 const char *fromdir TAKES, const char *to TAKES);
90 * path_basename - get trailing filename part of path
91 * @ctx: the context to tal the result from
92 * @path: the path (can be take())
95 * path dirname basename
96 * "/usr/lib" "/usr" "lib"
106 char *path_basename(const tal_t *ctx, const char *path TAKES);
109 * path_dirname - get the directory part of path
110 * @ctx: the context to tal the result from.
111 * @path: the path (can be take())
113 * This follows SUSv2.
118 char *path_dirname(const tal_t *ctx, const char *path TAKES);
121 * path_is_abs - is a path absolute?
122 * @path: the path to examine.
124 bool path_is_abs(const char *path);
127 * path_is_file - is a path an existing file (or long to one)?
128 * @path: the path to examine.
130 bool path_is_file(const char *path);
133 * path_is_file - is a path an existing directory (or long to one)?
134 * @path: the path to examine.
136 bool path_is_dir(const char *path);
139 * path_split - split a path into its pathname components
140 * @ctx: the context to tal the result from
141 * @path: the path (can be take())
143 * This returns the sections of a path, such that joining them with /
144 * will restore the original path. This means that the resulting
145 * strings will never contain / unless the input path was entirely one
146 * or more "/" characters.
148 * The final char * in the array will be NULL.
153 char **path_split(const tal_t *ctx, const char *path TAKES);
156 * path_ext_off - get offset of the extension within a pathname.
159 * This returns the offset of the final . in the pathname (ie.
160 * path[path_ext_off(path)] == '.') or the length of the string
161 * if there is no extension.
163 * Note that if the only . in the basename is at the start
164 * (eg. /home/person/.bashrc), that is not considered an extension!
166 size_t path_ext_off(const char *path);
168 /* Separator constants */
169 #define PATH_SEP_STR "/"
170 #define PATH_SEP (PATH_SEP_STR[0])
172 #endif /* CCAN_PATH_H */