mirror of
https://github.com/systemd/systemd.git
synced 2026-08-03 22:50:29 +00:00
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:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user