]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fileio: optionally allow telling read_line_full() whether we are processing a tty...
authorLennart Poettering <lennart@poettering.net>
Mon, 13 Apr 2020 09:20:59 +0000 (11:20 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 13 Apr 2020 09:27:07 +0000 (11:27 +0200)
src/basic/fileio.c
src/basic/fileio.h

index 387b3253b5e3e449e3cae50715d02545419f51b0..463a8596f5a719c2745cf9ef6f43f9ce11133c50 100644 (file)
@@ -1009,7 +1009,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);
 
@@ -1088,17 +1088,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;
                         }
 
index 58daabaa8ffdbe8eee2dbbf8e5fa300840097e89..e6062121a33ba136750599796e23171f9437a024 100644 (file)
@@ -88,7 +88,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);