summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2016-01-25 22:42:36 +0100
committerLennart Poettering <lennart@poettering.net>2016-01-26 14:42:04 +0100
commitd390f8ef2dc0cd041914d3c2fd3e1081605cbfc8 (patch)
treebaea925afd20e984696718b5813c2441444184ab /src/basic/fileio.c
parentb2a81c0b524fee0a1713720462b6db5c302c3933 (diff)
util: introduce fputs_with_space() and make use of it at various places
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.
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 5ed5460904..3ff70310e1 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -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);
+}