]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
logind: use extract_first_word()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 29 Jul 2020 09:47:38 +0000 (11:47 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 9 Sep 2020 07:34:54 +0000 (09:34 +0200)
src/login/logind-inhibit.c
src/login/logind-inhibit.h

index 1d335f914c6fe84b02bcaf44ffcda27360c499d7..254201d23a28f692410a03e7f1e1dcbbe3db249e 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "alloc-util.h"
 #include "env-file.h"
+#include "errno-list.h"
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -483,31 +484,39 @@ const char *inhibit_what_to_string(InhibitWhat w) {
         return buffer;
 }
 
-InhibitWhat inhibit_what_from_string(const char *s) {
+int inhibit_what_from_string(const char *s) {
         InhibitWhat what = 0;
-        const char *word, *state;
-        size_t l;
 
-        FOREACH_WORD_SEPARATOR(word, l, s, ":", state) {
-                if (l == 8 && strneq(word, "shutdown", l))
+        for (const char *p = s;;) {
+                _cleanup_free_ char *word = NULL;
+                int r;
+
+                /* A sanity check that our return values fit in an int */
+                assert_cc((int) _INHIBIT_WHAT_MAX == _INHIBIT_WHAT_MAX);
+
+                r = extract_first_word(&p, &word, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
+                if (r < 0)
+                        return r;
+                if (r == 0)
+                        return what;
+
+                if (streq(word, "shutdown"))
                         what |= INHIBIT_SHUTDOWN;
-                else if (l == 5 && strneq(word, "sleep", l))
+                else if (streq(word, "sleep"))
                         what |= INHIBIT_SLEEP;
-                else if (l == 4 && strneq(word, "idle", l))
+                else if (streq(word, "idle"))
                         what |= INHIBIT_IDLE;
-                else if (l == 16 && strneq(word, "handle-power-key", l))
+                else if (streq(word, "handle-power-key"))
                         what |= INHIBIT_HANDLE_POWER_KEY;
-                else if (l == 18 && strneq(word, "handle-suspend-key", l))
+                else if (streq(word, "handle-suspend-key"))
                         what |= INHIBIT_HANDLE_SUSPEND_KEY;
-                else if (l == 20 && strneq(word, "handle-hibernate-key", l))
+                else if (streq(word, "handle-hibernate-key"))
                         what |= INHIBIT_HANDLE_HIBERNATE_KEY;
-                else if (l == 17 && strneq(word, "handle-lid-switch", l))
+                else if (streq(word, "handle-lid-switch"))
                         what |= INHIBIT_HANDLE_LID_SWITCH;
                 else
                         return _INHIBIT_WHAT_INVALID;
         }
-
-        return what;
 }
 
 static const char* const inhibit_mode_table[_INHIBIT_MODE_MAX] = {
index cea67a08c599962728f8cfa01f978dae52cab9ad..7eaecee0b4534b8d0745317764f255aed41cda5c 100644 (file)
@@ -66,7 +66,7 @@ InhibitWhat manager_inhibit_what(Manager *m, InhibitMode mm);
 bool manager_is_inhibited(Manager *m, InhibitWhat w, InhibitMode mm, dual_timestamp *since, bool ignore_inactive, bool ignore_uid, uid_t uid, Inhibitor **offending);
 
 const char *inhibit_what_to_string(InhibitWhat k);
-InhibitWhat inhibit_what_from_string(const char *s);
+int inhibit_what_from_string(const char *s);
 
 const char *inhibit_mode_to_string(InhibitMode k);
 InhibitMode inhibit_mode_from_string(const char *s);