summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-04-13 11:20:59 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2020-04-17 14:33:11 +0200
commitabbfa8fdfa5a01edbe72619c407cc9da2d7c1693 (patch)
tree2fa5b125cf46add3b2e14a97b99660b640d069ab
parent10731dde4240719848589d94b0a7e6b4dc29d7e7 (diff)
downloadsystemd-abbfa8fdfa5a01edbe72619c407cc9da2d7c1693.tar.gz
fileio: optionally allow telling read_line_full() whether we are processing a tty or not
(cherry picked from commit 609ae0f59619619efe6db07e34f73a237e7f332b)
-rw-r--r--src/basic/fileio.c10
-rw-r--r--src/basic/fileio.h4
2 files changed, 8 insertions, 6 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index f7e083c8c0..818f9cc0e0 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -971,7 +971,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
size_t n = 0, allocated = 0, count = 0;
_cleanup_free_ char *buffer = NULL;
- int r, tty = -1;
+ int r;
assert(f);
@@ -1050,17 +1050,17 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
* \n as the single EOL marker, so there is no need to wait. We check
* this condition last to avoid isatty() check if not necessary. */
- if (tty < 0) {
+ if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) {
int fd;
fd = fileno(f);
if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
* and don't call isatty() on an invalid fd */
- tty = false;
+ flags |= READ_LINE_NOT_A_TTY;
else
- tty = isatty(fd);
+ flags |= isatty(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
}
- if (tty > 0)
+ if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
break;
}
diff --git a/src/basic/fileio.h b/src/basic/fileio.h
index e6fea2afd4..d140bfd99e 100644
--- a/src/basic/fileio.h
+++ b/src/basic/fileio.h
@@ -85,7 +85,9 @@ int read_timestamp_file(const char *fn, usec_t *ret);
int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
typedef enum ReadLineFlags {
- READ_LINE_ONLY_NUL = 1 << 0,
+ READ_LINE_ONLY_NUL = 1 << 0,
+ READ_LINE_IS_A_TTY = 1 << 1,
+ READ_LINE_NOT_A_TTY = 1 << 2,
} ReadLineFlags;
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);