chiark / gitweb /
util: fix ascii_strlower() return value
[elogind.git] / util.c
diff --git a/util.c b/util.c
index c5c8bd4edda6d68f1ef88607b9d09c559e42840a..9da7d985fa5caacaffac58648f3b533f243b4e72 100644 (file)
--- a/util.c
+++ b/util.c
 #include <sched.h>
 #include <sys/resource.h>
 #include <linux/sched.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include "macro.h"
 #include "util.h"
 #include "ioprio.h"
 #include "missing.h"
+#include "log.h"
+#include "strv.h"
 
 usec_t now(clockid_t clock_id) {
         struct timespec ts;
@@ -269,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;
@@ -277,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;
@@ -337,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;
@@ -510,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);
 
@@ -519,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;
 
@@ -539,7 +585,7 @@ int reset_all_signal_handlers(void) {
                                 return -errno;
         }
 
-    return 0;
+        return 0;
 }
 
 char *strstrip(char *s) {
@@ -590,6 +636,39 @@ char *file_in_same_dir(const char *path, const char *filename) {
         return r;
 }
 
+int mkdir_parents(const char *path, mode_t mode) {
+        const char *p, *e;
+
+        assert(path);
+
+        /* Creates every parent directory in the path except the last
+         * component. */
+
+        p = path + strspn(path, "/");
+        for (;;) {
+                int r;
+                char *t;
+
+                e = p + strcspn(p, "/");
+                p = e + strspn(e, "/");
+
+                /* Is this the last component? If so, then we're
+                 * done */
+                if (*p == 0)
+                        return 0;
+
+                if (!(t = strndup(path, e - path)))
+                        return -ENOMEM;
+
+                r = mkdir(t, mode);
+
+                free(t);
+
+                if (r < 0 && errno != EEXIST)
+                        return -errno;
+        }
+}
+
 char hexchar(int x) {
         static const char table[16] = "0123456789abcdef";
 
@@ -622,6 +701,18 @@ int unoctchar(char c) {
         return -1;
 }
 
+char decchar(int x) {
+        return '0' + (x % 10);
+}
+
+int undecchar(char c) {
+
+        if (c >= '0' && c <= '9')
+                return c - '0';
+
+        return -1;
+}
+
 char *cescape(const char *s) {
         char *r, *t;
         const char *f;
@@ -824,8 +915,8 @@ char *xescape(const char *s, const char *bad) {
 
         for (f = s, t = r; *f; f++) {
 
-                if (*f < ' ' || *f >= 127 ||
-                    *f == '\\' || strchr(bad, *f)) {
+                if ((*f < ' ') || (*f >= 127) ||
+                    (*f == '\\') || strchr(bad, *f)) {
                         *(t++) = '\\';
                         *(t++) = 'x';
                         *(t++) = hexchar(*f >> 4);
@@ -967,16 +1058,30 @@ bool path_startswith(const char *path, const char *prefix) {
         }
 }
 
-char *ascii_strlower(char *path) {
+char *ascii_strlower(char *t) {
         char *p;
 
-        assert(path);
+        assert(t);
 
-        for (p = path; *p; p++)
+        for (p = t; *p; p++)
                 if (*p >= 'A' && *p <= 'Z')
                         *p = *p - 'A' + 'a';
 
-        return p;
+        return t;
+}
+
+bool ignore_file(const char *filename) {
+        assert(filename);
+
+        return
+                filename[0] == '.' ||
+                endswith(filename, "~") ||
+                endswith(filename, ".rpmnew") ||
+                endswith(filename, ".rpmsave") ||
+                endswith(filename, ".rpmorig") ||
+                endswith(filename, ".dpkg-old") ||
+                endswith(filename, ".dpkg-new") ||
+                endswith(filename, ".swp");
 }
 
 static const char *const ioprio_class_table[] = {