]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: port to extract_first_word 1755/head
authorSusant Sahani <ssahani@gmail.com>
Mon, 2 Nov 2015 16:17:40 +0000 (21:47 +0530)
committerSusant Sahani <ssahani@gmail.com>
Mon, 2 Nov 2015 16:17:40 +0000 (21:47 +0530)
src/systemctl/systemctl.c

index da816e6d0e2d193155a51dad820b4ec16edc33ed..70871cf3e64a32431685f875c6e7ee2334497113 100644 (file)
@@ -6631,6 +6631,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                 {}
         };
 
+        const char *p;
         int c, r;
 
         assert(argc >= 0);
@@ -6651,15 +6652,19 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         return version();
 
                 case 't': {
-                        const char *word, *state;
-                        size_t size;
+                        if (isempty(optarg))
+                                return log_error_errno(r, "--type requires arguments.");
 
-                        FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
-                                _cleanup_free_ char *type;
+                        p = optarg;
+                        for(;;) {
+                                _cleanup_free_ char *type = NULL;
 
-                                type = strndup(word, size);
-                                if (!type)
-                                        return -ENOMEM;
+                                r = extract_first_word(&p, &type, ",", 0);
+                                if (r < 0)
+                                        return log_error_errno(r, "Failed to parse type: %s", optarg);
+
+                                if (r == 0)
+                                        break;
 
                                 if (streq(type, "help")) {
                                         help_types();
@@ -6700,18 +6705,21 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                                 if (!arg_properties)
                                         return log_oom();
                         } else {
-                                const char *word, *state;
-                                size_t size;
+                                p = optarg;
+                                for(;;) {
+                                        _cleanup_free_ char *prop = NULL;
 
-                                FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
-                                        char *prop;
+                                        r = extract_first_word(&p, &prop, ",", 0);
+                                        if (r < 0)
+                                                return log_error_errno(r, "Failed to parse property: %s", optarg);
 
-                                        prop = strndup(word, size);
-                                        if (!prop)
-                                                return log_oom();
+                                        if (r == 0)
+                                                break;
 
-                                        if (strv_consume(&arg_properties, prop) < 0)
+                                        if (strv_push(&arg_properties, prop) < 0)
                                                 return log_oom();
+
+                                        prop = NULL;
                                 }
                         }
 
@@ -6877,15 +6885,19 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
                         break;
 
                 case ARG_STATE: {
-                        const char *word, *state;
-                        size_t size;
+                        if (isempty(optarg))
+                                return log_error_errno(r, "--signal requires arguments.");
 
-                        FOREACH_WORD_SEPARATOR(word, size, optarg, ",", state) {
+                        p = optarg;
+                        for(;;) {
                                 _cleanup_free_ char *s = NULL;
 
-                                s = strndup(word, size);
-                                if (!s)
-                                        return log_oom();
+                                r = extract_first_word(&p, &s, ",", 0);
+                                if (r < 0)
+                                        return log_error_errno(r, "Failed to parse signal: %s", optarg);
+
+                                if (r == 0)
+                                        break;
 
                                 if (streq(s, "help")) {
                                         help_states();