chiark / gitweb /
util: add chars_intersect() call
[elogind.git] / util.c
diff --git a/util.c b/util.c
index e31649861d3b642b16396c99a1787cc8baaacc53..a0f26762dc39b87b3f6cb3c24acb50db14849512 100644 (file)
--- a/util.c
+++ b/util.c
@@ -32,6 +32,8 @@
 #include <linux/sched.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <fcntl.h>
+#include <dirent.h>
 
 #include "macro.h"
 #include "util.h"
@@ -585,7 +587,7 @@ int reset_all_signal_handlers(void) {
                                 return -errno;
         }
 
-    return 0;
+        return 0;
 }
 
 char *strstrip(char *s) {
@@ -609,6 +611,23 @@ char *strstrip(char *s) {
 
 }
 
+char *delete_chars(char *s, const char *bad) {
+        char *f, *t;
+
+        /* Drops all whitespace, regardless where in the string */
+
+        for (f = s, t = s; *f; f++) {
+                if (strchr(bad, *f))
+                        continue;
+
+                *(t++) = *f;
+        }
+
+        *t = 0;
+
+        return s;
+}
+
 char *file_in_same_dir(const char *path, const char *filename) {
         char *e, *r;
         size_t k;
@@ -701,6 +720,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;
@@ -903,8 +934,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);
@@ -1046,16 +1077,16 @@ 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) {
@@ -1072,6 +1103,107 @@ bool ignore_file(const char *filename) {
                 endswith(filename, ".swp");
 }
 
+int fd_nonblock(int fd, bool nonblock) {
+        int flags;
+
+        assert(fd >= 0);
+
+        if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
+                return -errno;
+
+        if (nonblock)
+                flags |= O_NONBLOCK;
+        else
+                flags &= ~O_NONBLOCK;
+
+        if (fcntl(fd, F_SETFL, flags) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int fd_cloexec(int fd, bool cloexec) {
+        int flags;
+
+        assert(fd >= 0);
+
+        if ((flags = fcntl(fd, F_GETFD, 0)) < 0)
+                return -errno;
+
+        if (cloexec)
+                flags |= FD_CLOEXEC;
+        else
+                flags &= ~FD_CLOEXEC;
+
+        if (fcntl(fd, F_SETFD, flags) < 0)
+                return -errno;
+
+        return 0;
+}
+
+int close_all_fds(const int except[], unsigned n_except) {
+        DIR *d;
+        struct dirent *de;
+        int r = 0;
+
+        if (!(d = opendir("/proc/self/fd")))
+                return -errno;
+
+        while ((de = readdir(d))) {
+                int fd = -1;
+
+                if (de->d_name[0] == '.')
+                        continue;
+
+                if ((r = safe_atoi(de->d_name, &fd)) < 0)
+                        goto finish;
+
+                if (fd < 3)
+                        continue;
+
+                if (fd == dirfd(d))
+                        continue;
+
+                if (except) {
+                        bool found;
+                        unsigned i;
+
+                        found = false;
+                        for (i = 0; i < n_except; i++)
+                                if (except[i] == fd) {
+                                        found = true;
+                                        break;
+                                }
+
+                        if (found)
+                                continue;
+                }
+
+                if ((r = close_nointr(fd)) < 0) {
+                        /* Valgrind has its own FD and doesn't want to have it closed */
+                        if (errno != EBADF)
+                                goto finish;
+                }
+        }
+
+        r = 0;
+
+finish:
+        closedir(d);
+        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",