io-util: move fputs_with_newline to fileio

Follow-up for cdf6f34a2f

We already have other fputs()-like helpers in fileio rather than
io-util. While at it, switch the order of params.
This commit is contained in:
Mike Yuan 2024-06-13 13:57:25 +02:00 committed by Luca Boccassi
parent c43d439307
commit 0160a1dbbc
6 changed files with 28 additions and 24 deletions

View 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,

View 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,

View 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;
}

View 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);

View 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);

View 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)