chiark / gitweb /
util: add various utility calls
authorLennart Poettering <lennart@poettering.net>
Sat, 13 Feb 2010 00:05:12 +0000 (01:05 +0100)
committerLennart Poettering <lennart@poettering.net>
Sat, 13 Feb 2010 00:05:12 +0000 (01:05 +0100)
util.c
util.h

diff --git a/util.c b/util.c
index 517c99c45ef5cbe9fab4f50a1e53a97527391392..496f55166b7f35a1dad171fbe7c2541a97dd7fa8 100644 (file)
--- a/util.c
+++ b/util.c
@@ -38,6 +38,7 @@
 #include "ioprio.h"
 #include "missing.h"
 #include "log.h"
+#include "strv.h"
 
 usec_t now(clockid_t clock_id) {
         struct timespec ts;
@@ -272,7 +273,7 @@ int safe_atolli(const char *s, long long int *ret_lli) {
 }
 
 /* Split a string into words. */
-char *split_spaces(const char *c, size_t *l, char **state) {
+char *split(const char *c, size_t *l, const char *separator, char **state) {
         char *current;
 
         current = *state ? *state : (char*) c;
@@ -280,24 +281,8 @@ char *split_spaces(const char *c, size_t *l, char **state) {
         if (!*current || *c == 0)
                 return NULL;
 
-        current += strspn(current, WHITESPACE);
-        *l = strcspn(current, WHITESPACE);
-        *state = current+*l;
-
-        return (char*) current;
-}
-
-/* Split a path into filenames. */
-char *split_slash(const char *c, size_t *l, char **state) {
-        char *current;
-
-        current = *state ? *state : (char*) c;
-
-        if (!*current || *c == 0)
-                return NULL;
-
-        current += strspn(current, "/");
-        *l = strcspn(current, "/");
+        current += strspn(current, separator);
+        *l = strcspn(current, separator);
         *state = current+*l;
 
         return (char*) current;
@@ -340,6 +325,21 @@ char *split_quoted(const char *c, size_t *l, char **state) {
         return (char*) current;
 }
 
+char **split_path_and_make_absolute(const char *p) {
+        char **l;
+        assert(p);
+
+        if (!(l = strv_split(p, ":")))
+                return NULL;
+
+        if (!strv_path_make_absolute_cwd(l)) {
+                strv_free(l);
+                return NULL;
+        }
+
+        return l;
+}
+
 int get_parent_of_pid(pid_t pid, pid_t *_ppid) {
         int r;
         FILE *f;
@@ -513,6 +513,9 @@ char *path_make_absolute(const char *p, const char *prefix) {
 
         assert(p);
 
+        /* Makes every item in the list an absolute path by prepending
+         * the prefix, if specified and necessary */
+
         if (path_is_absolute(p) || !prefix)
                 return strdup(p);
 
@@ -522,6 +525,46 @@ char *path_make_absolute(const char *p, const char *prefix) {
         return r;
 }
 
+char *path_make_absolute_cwd(const char *p) {
+        char *cwd, *r;
+
+        assert(p);
+
+        /* Similar to path_make_absolute(), but prefixes with the
+         * current working directory. */
+
+        if (path_is_absolute(p))
+                return strdup(p);
+
+        if (!(cwd = get_current_dir_name()))
+                return NULL;
+
+        r = path_make_absolute(p, cwd);
+        free(cwd);
+
+        return r;
+}
+
+char **strv_path_make_absolute_cwd(char **l) {
+        char **s;
+
+        /* Goes through every item in the string list and makes it
+         * absolute. This works in place and won't rollback any
+         * changes on failure. */
+
+        STRV_FOREACH(s, l) {
+                char *t;
+
+                if (!(t = path_make_absolute_cwd(*s)))
+                        return NULL;
+
+                free(*s);
+                *s = t;
+        }
+
+        return l;
+}
+
 int reset_all_signal_handlers(void) {
         int sig;
 
diff --git a/util.h b/util.h
index ba7e542baa15cac6b8092e3d48985dfc8e635b2f..71645a70ccc076d54e698757096d2b3eaba727d1 100644 (file)
--- a/util.h
+++ b/util.h
@@ -95,18 +95,19 @@ int safe_atoli(const char *s, long int *ret_i);
 int safe_atollu(const char *s, unsigned long long *ret_u);
 int safe_atolli(const char *s, long long int *ret_i);
 
-char *split_spaces(const char *c, size_t *l, char **state);
+char *split(const char *c, size_t *l, const char *separator, char **state);
 char *split_quoted(const char *c, size_t *l, char **state);
-char *split_slash(const char *c, size_t *l, char **state);
 
 #define FOREACH_WORD(word, length, s, state)                            \
-        for ((state) = NULL, (word) = split_spaces((s), &(l), &(state)); (word); (word) = split_spaces((s), &(l), &(state)))
+        for ((state) = NULL, (word) = split((s), &(l), WHITESPACE, &(state)); (word); (word) = split((s), &(l), WHITESPACE, &(state)))
+
+#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
+        for ((state) = NULL, (word) = split((s), &(l), (separator), &(state)); (word); (word) = split((s), &(l), (separator), &(state)))
 
 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
         for ((state) = NULL, (word) = split_quoted((s), &(l), &(state)); (word); (word) = split_quoted((s), &(l), &(state)))
 
-#define FOREACH_WORD_SLASH(word, length, s, state)                      \
-        for ((state) = NULL, (word) = split_slash((s), &(l), &(state)); (word); (word) = split_slash((s), &(l), &(state)))
+char **split_path_and_make_absolute(const char *p);
 
 pid_t get_parent_of_pid(pid_t pid, pid_t *ppid);
 
@@ -122,6 +123,8 @@ bool is_path(const char *p);
 
 bool path_is_absolute(const char *p);
 char *path_make_absolute(const char *p, const char *prefix);
+char *path_make_absolute_cwd(const char *p);
+char **strv_path_make_absolute_cwd(char **l);
 
 int reset_all_signal_handlers(void);