]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: introduce fputs_with_space() and make use of it at various places
authorLennart Poettering <lennart@poettering.net>
Mon, 25 Jan 2016 21:42:36 +0000 (22:42 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 26 Jan 2016 13:42:04 +0000 (14:42 +0100)
The call combines outputing a string with prefixing it with a space, optionally. This is useful to shorten the logic
for outputing lists of strings, that are space separated.

src/basic/fileio.c
src/basic/fileio.h
src/basic/strv.c
src/network/networkd-link.c
src/network/networkd-manager.c

index 5ed5460904b4207d960ac4b11d7e94eca6997a99..3ff70310e184d4f4e3aceed7b7a0d7763d2dbbd6 100644 (file)
@@ -1251,3 +1251,32 @@ int read_timestamp_file(const char *fn, usec_t *ret) {
         *ret = (usec_t) t;
         return 0;
 }
+
+int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space) {
+        int r;
+
+        assert(s);
+
+        /* Outputs the specified string with fputs(), but optionally prefixes it with a separator. The *space parameter
+         * when specified shall initially point to a boolean variable initialized to false. It is set to true after the
+         * first invocation. This call is supposed to be use in loops, where a separator shall be inserted between each
+         * element, but not before the first one. */
+
+        if (!f)
+                f = stdout;
+
+        if (space) {
+                if (!separator)
+                        separator = " ";
+
+                if (*space) {
+                        r = fputs(separator, f);
+                        if (r < 0)
+                                return r;
+                }
+
+                *space = true;
+        }
+
+        return fputs(s, f);
+}
index 95e8698941c4d9914ae47828ecf83b67f925f58a..9e0957413380f8fb7680db28e3f53cdae176a817 100644 (file)
@@ -82,3 +82,5 @@ int tempfn_random_child(const char *p, const char *extra, char **ret);
 
 int write_timestamp_file_atomic(const char *fn, usec_t n);
 int read_timestamp_file(const char *fn, usec_t *ret);
+
+int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
index dc5bafcf244db1a6021238dd72f92cb868d2e749..5532c53ad1936412b52f0d13bb57c6a6f65604f1 100644 (file)
@@ -29,6 +29,7 @@
 #include "alloc-util.h"
 #include "escape.h"
 #include "extract-word.h"
+#include "fileio.h"
 #include "string-util.h"
 #include "strv.h"
 #include "util.h"
@@ -879,25 +880,13 @@ int fputstrv(FILE *f, char **l, const char *separator, bool *space) {
 
         /* Like fputs(), but for strv, and with a less stupid argument order */
 
-        if (!f)
-                f = stdout;
-        if (!separator)
-                separator = " ";
         if (!space)
                 space = &b;
 
         STRV_FOREACH(s, l) {
-                if (*space) {
-                        r = fputs(separator, f);
-                        if (r < 0)
-                                return r;
-                }
-
-                r = fputs(*s, f);
+                r = fputs_with_space(f, *s, separator, space);
                 if (r < 0)
                         return r;
-
-                *space = true;
         }
 
         return 0;
index bf13544dbc5af501a63ce675c5e381e5d233e66d..a2f0eceb6de8caba51a1f153388157fd22f2ddd4 100644 (file)
@@ -2820,12 +2820,8 @@ int link_save(Link *link) {
                 fputs("DOMAINS=", f);
                 fputstrv(f, link->network->search_domains, NULL, &space);
 
-                if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES && dhcp_domainname) {
-                        if (space)
-                                fputc(' ', f);
-                        fputs(dhcp_domainname, f);
-                        space = true;
-                }
+                if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES && dhcp_domainname)
+                        fputs_with_space(f, dhcp_domainname, NULL, &space);
 
                 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_YES && dhcp6_domains)
                         fputstrv(f, dhcp6_domains, NULL, &space);
@@ -2835,12 +2831,8 @@ int link_save(Link *link) {
                 fputs("ROUTE_DOMAINS=", f);
                 fputstrv(f, link->network->route_domains, NULL, NULL);
 
-                if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE && dhcp_domainname) {
-                        if (space)
-                                fputc(' ', f);
-                        fputs(dhcp_domainname, f);
-                        space = true;
-                }
+                if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE && dhcp_domainname)
+                        fputs_with_space(f, dhcp_domainname, NULL, &space);
 
                 if (link->network->dhcp_use_domains == DHCP_USE_DOMAINS_ROUTE && dhcp6_domains)
                         fputstrv(f, dhcp6_domains, NULL, &space);
@@ -2861,12 +2853,8 @@ int link_save(Link *link) {
 
                         fputs("DNSSEC_NTA=", f);
                         space = false;
-                        SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i) {
-                                if (space)
-                                        fputc(' ', f);
-                                fputs(n, f);
-                                space = true;
-                        }
+                        SET_FOREACH(n, link->network->dnssec_negative_trust_anchors, i)
+                                fputs_with_space(f, n, NULL, &space);
                         fputc('\n', f);
                 }
 
@@ -2906,12 +2894,8 @@ int link_save(Link *link) {
                 bool space = false;
 
                 fputs("CARRIER_BOUND_TO=", f);
-                HASHMAP_FOREACH(carrier, link->bound_to_links, i) {
-                        if (space)
-                                fputc(' ', f);
-                        fputs(carrier->ifname, f);
-                        space = true;
-                }
+                HASHMAP_FOREACH(carrier, link->bound_to_links, i)
+                        fputs_with_space(f, carrier->ifname, NULL, &space);
 
                 fputc('\n', f);
         }
@@ -2921,12 +2905,8 @@ int link_save(Link *link) {
                 bool space = false;
 
                 fputs("CARRIER_BOUND_BY=", f);
-                HASHMAP_FOREACH(carrier, link->bound_by_links, i) {
-                        if (space)
-                                fputc(' ', f);
-                        fputs(carrier->ifname, f);
-                        space = true;
-                }
+                HASHMAP_FOREACH(carrier, link->bound_by_links, i)
+                        fputs_with_space(f, carrier->ifname, NULL, &space);
 
                 fputc('\n', f);
         }
index 723a92b5b801979631bc6138e0a262bb8060fbbc..c10635d202a7a2dd11fc60ada051cf5b4e94b01f 100644 (file)
@@ -822,12 +822,9 @@ static void print_string_set(FILE *f, const char *field, OrderedSet *s) {
 
         fputs(field, f);
 
-        ORDERED_SET_FOREACH(p, s, i) {
-                if (space)
-                        fputc(' ', f);
-                fputs(p, f);
-                space = true;
-        }
+        ORDERED_SET_FOREACH(p, s, i)
+                fputs_with_space(f, p, NULL, &space);
+
         fputc('\n', f);
 }