chiark / gitweb /
util: add decchar()/undecchar() calls
[elogind.git] / util.c
diff --git a/util.c b/util.c
index b54c24a3dcd9efaecbe70ddb39efe8129b3eabd7..7306ddde34c4ee26db5cac1389f5647cfb05d409 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,5 +1,24 @@
 /*-*- Mode: C; c-basic-offset: 8 -*-*/
 
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
 #include <assert.h>
 #include <string.h>
 #include <unistd.h>
 #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) {
+usec_t now(clockid_t clock_id) {
         struct timespec ts;
 
-        assert_se(clock_gettime(clock, &ts) == 0);
+        assert_se(clock_gettime(clock_id, &ts) == 0);
 
         return timespec_load(&ts);
 }
@@ -250,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;
@@ -258,8 +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);
+        current += strspn(current, separator);
+        *l = strcspn(current, separator);
         *state = current+*l;
 
         return (char*) current;
@@ -302,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;
@@ -475,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);
 
@@ -484,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;
 
@@ -504,7 +585,7 @@ int reset_all_signal_handlers(void) {
                                 return -errno;
         }
 
-    return 0;
+        return 0;
 }
 
 char *strstrip(char *s) {
@@ -555,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";
 
@@ -587,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;
@@ -789,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);
@@ -805,11 +931,11 @@ char *xescape(const char *s, const char *bad) {
 }
 
 char *bus_path_escape(const char *s) {
-        assert(s);
-
         char *r, *t;
         const char *f;
 
+        assert(s);
+
         /* Escapes all chars that D-Bus' object path cannot deal
          * with. Can be reverse with bus_path_unescape() */
 
@@ -834,11 +960,11 @@ char *bus_path_escape(const char *s) {
 }
 
 char *bus_path_unescape(const char *s) {
-        assert(s);
-
         char *r, *t;
         const char *f;
 
+        assert(s);
+
         if (!(r = new(char, strlen(s)+1)))
                 return NULL;
 
@@ -944,6 +1070,20 @@ char *ascii_strlower(char *path) {
         return p;
 }
 
+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[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",