summaryrefslogtreecommitdiff
path: root/src/basic/path-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-10-22 19:54:29 +0200
committerLennart Poettering <lennart@poettering.net>2015-10-24 23:03:49 +0200
commit0f03c2a4c093e3d44f4072144827e943c05c8904 (patch)
tree70232d1b14b4dfcd82983894d13963e2ca9f4fec /src/basic/path-util.c
parent0f4743651081b5367ab06f238827ddfd4da74e74 (diff)
path-util: unify how we process paths specified on the command line
Let's introduce a common function that makes relative paths absolute and warns about any errors while doing so.
Diffstat (limited to 'src/basic/path-util.c')
-rw-r--r--src/basic/path-util.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/basic/path-util.c b/src/basic/path-util.c
index 96705cc9d8..6b05b6edb1 100644
--- a/src/basic/path-util.c
+++ b/src/basic/path-util.c
@@ -902,3 +902,35 @@ char *prefix_root(const char *root, const char *path) {
strcpy(p, path);
return n;
}
+
+int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
+ char *p;
+ int r;
+
+ /*
+ * This function is intended to be used in command line
+ * parsers, to handle paths that are passed in. It makes the
+ * path absolute, and reduces it to NULL if omitted or
+ * root (the latter optionally).
+ *
+ * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
+ * SUCCESS! Hence, do not pass in uninitialized pointers.
+ */
+
+ if (isempty(path)) {
+ *arg = mfree(*arg);
+ return 0;
+ }
+
+ r = path_make_absolute_cwd(path, &p);
+ if (r < 0)
+ return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
+
+ path_kill_slashes(p);
+ if (suppress_root && path_equal(p, "/"))
+ p = mfree(p);
+
+ free(*arg);
+ *arg = p;
+ return 0;
+}