]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
io-util: move fputs_with_newline to fileio
authorMike Yuan <me@yhndnzj.com>
Thu, 13 Jun 2024 11:57:25 +0000 (13:57 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Thu, 13 Jun 2024 14:56:02 +0000 (15:56 +0100)
Follow-up for cdf6f34a2fd1448c5d1b75f4717c57b057dd51b2

We already have other fputs()-like helpers in fileio rather than
io-util. While at it, switch the order of params.

src/basic/fileio.c
src/basic/fileio.h
src/basic/io-util.c
src/basic/io-util.h
src/core/unit.c
src/varlinkctl/varlinkctl.c

index 523378177fb7ecb6c84bd4c9b99659670bba5883..977fdd294c3d511b7999100abb4715b423128897 100644 (file)
@@ -1354,6 +1354,29 @@ int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *sp
         return 0;
 }
 
+int fputs_with_newline(FILE *f, const char *s) {
+
+        /* This is like fputs() but outputs a trailing newline char, but only if the string isn't empty
+         * and doesn't end in a newline already. Returns 0 in case we didn't append a newline, > 0 otherwise. */
+
+        if (isempty(s))
+                return 0;
+
+        if (!f)
+                f = stdout;
+
+        if (fputs(s, f) < 0)
+                return -EIO;
+
+        if (endswith(s, "\n"))
+                return 0;
+
+        if (fputc('\n', f) < 0)
+                return -EIO;
+
+        return 1;
+}
+
 /* A bitmask of the EOL markers we know */
 typedef enum EndOfLineMarker {
         EOL_NONE     = 0,
index 03c3f3ff2839376310ca7cd6afb9308baac47c4e..e9fba1658024ecc034e924021c3b3975ab48665b 100644 (file)
@@ -144,6 +144,7 @@ int write_timestamp_file_atomic(const char *fn, usec_t n);
 int read_timestamp_file(const char *fn, usec_t *ret);
 
 int fputs_with_separator(FILE *f, const char *s, const char *separator, bool *space);
+int fputs_with_newline(FILE *f, const char *s);
 
 typedef enum ReadLineFlags {
         READ_LINE_ONLY_NUL  = 1 << 0,
index e07e1a1e1911fb295c3b9c7f1188ebbe06e53135..6bcbef3413623dd624ff0f98c44fe952cffbecf5 100644 (file)
@@ -306,19 +306,3 @@ ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
 
         return q - (const uint8_t*) p;
 }
-
-int fputs_with_newline(const char *s, FILE *f) {
-        assert(s);
-        assert(f);
-
-        /* This is like fputs() but outputs a trailing newline char, but only if the string doesn't end in a
-         * newline anyway. Just like fputs() returns EOF on error. Otherwise returns 0 in case we didn't
-         * append a newline, > 0 otherwise. */
-
-        if (fputs(s, f) == EOF)
-                return EOF;
-        if (endswith(s, "\n"))
-                return 0;
-
-        return fputc('\n', f) == EOF ? EOF : 1;
-}
index 0d9007a304d7144370f9c2a086723f7d6988ad52..e027c1a878ccd80f5cb0b2d412ce1811ed3aae3a 100644 (file)
@@ -5,7 +5,6 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
-#include <stdio.h>
 #include <sys/types.h>
 
 #include "macro.h"
@@ -45,5 +44,3 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
         return FILE_SIZE_VALID(l);
 
 }
-
-int fputs_with_newline(const char *s, FILE *f);
index fb0519b58312f693827a1e87525f54e9de65c54d..2f82902d4f23f66e105dd3314c274c6bfe85b6ac 100644 (file)
@@ -33,7 +33,6 @@
 #include "format-util.h"
 #include "id128-util.h"
 #include "install.h"
-#include "io-util.h"
 #include "iovec-util.h"
 #include "label-util.h"
 #include "load-dropin.h"
@@ -4570,9 +4569,9 @@ int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const ch
         }
 
         if (u->transient_file) {
-                /* When this is a transient unit file in creation, then let's not create a new drop-in but instead
-                 * write to the transient unit file. */
-                fputs_with_newline(data, u->transient_file);
+                /* When this is a transient unit file in creation, then let's not create a new drop-in,
+                 * but instead write to the transient unit file. */
+                fputs_with_newline(u->transient_file, data);
 
                 /* Remember which section we wrote this entry to */
                 u->last_section_private = !!(flags & UNIT_PRIVATE);
index 739770d2961ebe81ef43f473fca1ec2d5acfd34f..5d3a2f4c0a05cf0ccb93b4ac720bc32960c4125a 100644 (file)
@@ -373,7 +373,7 @@ static int verb_introspect(int argc, char *argv[], void *userdata) {
                                 log_warning_errno(r, "Failed to parse returned interface description at %u:%u, showing raw interface description: %m", line, column);
 
                                 pager_open(arg_pager_flags);
-                                fputs_with_newline(description, stdout);
+                                fputs_with_newline(stdout, description);
                         } else if (list_methods) {
                                 for (const VarlinkSymbol *const *y = vi->symbols, *symbol; (symbol = *y); y++) {
                                         if (symbol->symbol_type != VARLINK_METHOD)