diff options
Diffstat (limited to 'src/shared/parse-argument.c')
-rw-r--r-- | src/shared/parse-argument.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/shared/parse-argument.c b/src/shared/parse-argument.c index ca10d51793..774cb1bc82 100644 --- a/src/shared/parse-argument.c +++ b/src/shared/parse-argument.c @@ -2,11 +2,42 @@ #include "format-table.h" #include "parse-argument.h" +#include "path-util.h" #include "signal-util.h" #include "stdio-util.h" #include "string-table.h" #include "string-util.h" +/* All functions in this file emit warnigs. */ + +int parse_path_argument(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_simplify(p, false); + if (suppress_root && empty_or_root(p)) + p = mfree(p); + + return free_and_replace(*arg, p); +} + int parse_signal_argument(const char *s, int *ret) { int r; |