io-util: add new helper fputs_with_newline()
This commit is contained in:
@@ -306,3 +306,19 @@ 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,6 +5,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "macro.h"
|
||||
@@ -44,3 +45,5 @@ 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