From: Mike Yuan Date: Thu, 13 Jun 2024 11:57:25 +0000 (+0200) Subject: io-util: move fputs_with_newline to fileio X-Git-Tag: v257-rc1~1167 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0160a1dbbca2bd428d913a60a2a66ed8de5ddb13;p=thirdparty%2Fsystemd.git io-util: move fputs_with_newline to fileio 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. --- diff --git a/src/basic/fileio.c b/src/basic/fileio.c index 523378177fb..977fdd294c3 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -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, diff --git a/src/basic/fileio.h b/src/basic/fileio.h index 03c3f3ff283..e9fba165802 100644 --- a/src/basic/fileio.h +++ b/src/basic/fileio.h @@ -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, diff --git a/src/basic/io-util.c b/src/basic/io-util.c index e07e1a1e191..6bcbef34136 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -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; -} diff --git a/src/basic/io-util.h b/src/basic/io-util.h index 0d9007a304d..e027c1a878c 100644 --- a/src/basic/io-util.h +++ b/src/basic/io-util.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #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); diff --git a/src/core/unit.c b/src/core/unit.c index fb0519b5831..2f82902d4f2 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -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); diff --git a/src/varlinkctl/varlinkctl.c b/src/varlinkctl/varlinkctl.c index 739770d2961..5d3a2f4c0a0 100644 --- a/src/varlinkctl/varlinkctl.c +++ b/src/varlinkctl/varlinkctl.c @@ -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)