Skip to content

Commit 609ae0f

Browse files
committed
fileio: optionally allow telling read_line_full() whether we are processing a tty or not
1 parent 14f594b commit 609ae0f

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/basic/fileio.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(FILE*, funlockfile);
10091009
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
10101010
size_t n = 0, allocated = 0, count = 0;
10111011
_cleanup_free_ char *buffer = NULL;
1012-
int r, tty = -1;
1012+
int r;
10131013

10141014
assert(f);
10151015

@@ -1088,17 +1088,17 @@ int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret) {
10881088
* \n as the single EOL marker, so there is no need to wait. We check
10891089
* this condition last to avoid isatty() check if not necessary. */
10901090

1091-
if (tty < 0) {
1091+
if ((flags & (READ_LINE_IS_A_TTY|READ_LINE_NOT_A_TTY)) == 0) {
10921092
int fd;
10931093

10941094
fd = fileno(f);
10951095
if (fd < 0) /* Maybe an fmemopen() stream? Handle this gracefully,
10961096
* and don't call isatty() on an invalid fd */
1097-
tty = false;
1097+
flags |= READ_LINE_NOT_A_TTY;
10981098
else
1099-
tty = isatty(fd);
1099+
flags |= isatty(fd) ? READ_LINE_IS_A_TTY : READ_LINE_NOT_A_TTY;
11001100
}
1101-
if (tty > 0)
1101+
if (FLAGS_SET(flags, READ_LINE_IS_A_TTY))
11021102
break;
11031103
}
11041104

src/basic/fileio.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ int read_timestamp_file(const char *fn, usec_t *ret);
8888
int fputs_with_space(FILE *f, const char *s, const char *separator, bool *space);
8989

9090
typedef enum ReadLineFlags {
91-
READ_LINE_ONLY_NUL = 1 << 0,
91+
READ_LINE_ONLY_NUL = 1 << 0,
92+
READ_LINE_IS_A_TTY = 1 << 1,
93+
READ_LINE_NOT_A_TTY = 1 << 2,
9294
} ReadLineFlags;
9395

9496
int read_line_full(FILE *f, size_t limit, ReadLineFlags flags, char **ret);

0 commit comments

Comments
 (0)