chiark / gitweb /
util: Rewrite in_charset to use strspn
[elogind.git] / src / shared / util.c
index d28caae6c2ebc0fd034acebf2d06fa77bd1c96cb..e1a11684565f4bc8780422af492238271a988fa1 100644 (file)
@@ -921,16 +921,9 @@ char *delete_chars(char *s, const char *bad) {
 }
 
 bool in_charset(const char *s, const char* charset) {
-        const char *i;
-
         assert(s);
         assert(charset);
-
-        for (i = s; *i; i++)
-                if (!strchr(charset, *i))
-                        return false;
-
-        return true;
+        return s[strspn(s, charset)] == '\0';
 }
 
 char *file_in_same_dir(const char *path, const char *filename) {
@@ -3198,19 +3191,27 @@ bool on_tty(void) {
         return cached_on_tty;
 }
 
-int running_in_chroot(void) {
-        struct stat a = {}, b = {};
+int files_same(const char *filea, const char *fileb) {
+        struct stat a, b;
 
-        /* Only works as root */
-        if (stat("/proc/1/root", &a) < 0)
+        if (stat(filea, &a) < 0)
                 return -errno;
 
-        if (stat("/", &b) < 0)
+        if (stat(fileb, &b) < 0)
                 return -errno;
 
-        return
-                a.st_dev != b.st_dev ||
-                a.st_ino != b.st_ino;
+        return a.st_dev == b.st_dev &&
+               a.st_ino == b.st_ino;
+}
+
+int running_in_chroot(void) {
+        int ret;
+
+        ret = files_same("/proc/1/root", "/");
+        if (ret < 0)
+                return ret;
+
+        return ret == 0;
 }
 
 static char *ascii_ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {