chiark / gitweb /
util: introduce mkdir_p()
[elogind.git] / util.c
diff --git a/util.c b/util.c
index 939b2b06e9ddb2da442f688e30a596ed449b6837..29f48b08e3b918991350caa2794682a239b7ca5e 100644 (file)
--- a/util.c
+++ b/util.c
 #include "log.h"
 #include "strv.h"
 
 #include "log.h"
 #include "strv.h"
 
+bool streq_ptr(const char *a, const char *b) {
+
+        /* Like streq(), but tries to make sense of NULL pointers */
+
+        if (a && b)
+                return streq(a, b);
+
+        if (!a && !b)
+                return true;
+
+        return false;
+}
+
 usec_t now(clockid_t clock_id) {
         struct timespec ts;
 
 usec_t now(clockid_t clock_id) {
         struct timespec ts;
 
@@ -412,7 +425,7 @@ finish:
 int read_one_line_file(const char *fn, char **line) {
         FILE *f;
         int r;
 int read_one_line_file(const char *fn, char **line) {
         FILE *f;
         int r;
-        char t[64], *c;
+        char t[2048], *c;
 
         assert(fn);
         assert(line);
 
         assert(fn);
         assert(line);
@@ -438,6 +451,33 @@ finish:
         return r;
 }
 
         return r;
 }
 
+char *truncate_nl(char *s) {
+        assert(s);
+
+        s[strcspn(s, NEWLINE)] = 0;
+        return s;
+}
+
+int get_process_name(pid_t pid, char **name) {
+        char *p;
+        int r;
+
+        assert(pid >= 1);
+        assert(name);
+
+        if (asprintf(&p, "/proc/%llu/comm", (unsigned long long) pid) < 0)
+                return -ENOMEM;
+
+        r = read_one_line_file(p, name);
+        free(p);
+
+        if (r < 0)
+                return r;
+
+        truncate_nl(*name);
+        return 0;
+}
+
 char *strappend(const char *s, const char *suffix) {
         size_t a, b;
         char *r;
 char *strappend(const char *s, const char *suffix) {
         size_t a, b;
         char *r;
@@ -688,6 +728,20 @@ int mkdir_parents(const char *path, mode_t mode) {
         }
 }
 
         }
 }
 
+int mkdir_p(const char *path, mode_t mode) {
+        int r;
+
+        /* Like mkdir -p */
+
+        if ((r = mkdir_parents(path, mode)) < 0)
+                return r;
+
+        if (mkdir(path, mode) < 0)
+                return -errno;
+
+        return 0;
+}
+
 char hexchar(int x) {
         static const char table[16] = "0123456789abcdef";
 
 char hexchar(int x) {
         static const char table[16] = "0123456789abcdef";
 
@@ -1193,6 +1247,17 @@ finish:
         return r;
 }
 
         return r;
 }
 
+bool chars_intersect(const char *a, const char *b) {
+        const char *p;
+
+        /* Returns true if any of the chars in a are in b. */
+        for (p = a; *p; p++)
+                if (strchr(b, *p))
+                        return true;
+
+        return false;
+}
+
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",
 static const char *const ioprio_class_table[] = {
         [IOPRIO_CLASS_NONE] = "none",
         [IOPRIO_CLASS_RT] = "realtime",