]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
terminal-util: try to avoid reading more from terminal than we need in get_default_ba...
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Jul 2024 13:56:15 +0000 (15:56 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 19 Jul 2024 09:44:04 +0000 (11:44 +0200)
src/basic/terminal-util.c

index 058f8df1097489f5eb2c7e5a21cbdfab90c5cda0..635b5f62ee44d6e5d98849f94bb95a724f5bf2eb 100644 (file)
@@ -1830,11 +1830,11 @@ int get_default_background_color(double *ret_red, double *ret_green, double *ret
                 goto finish;
 
         usec_t end = usec_add(now(CLOCK_MONOTONIC), 100 * USEC_PER_MSEC);
-        char buf[256];
+        char buf[STRLEN("\x1B]11;rgb:0/0/0\x07")]; /* shortest possible reply */
         size_t buf_full = 0;
         BackgroundColorContext context = {};
 
-        for (;;) {
+        for (bool first = true;; first = false) {
                 if (buf_full == 0) {
                         usec_t n = now(CLOCK_MONOTONIC);
 
@@ -1851,7 +1851,10 @@ int get_default_background_color(double *ret_red, double *ret_green, double *ret
                                 goto finish;
                         }
 
-                        ssize_t l = read(STDIN_FILENO, buf, sizeof(buf));
+                        /* On the first try, read multiple characters, i.e. the shortest valid
+                         * reply. Afterwards read byte-wise, since we don't want to read too much, and
+                         * unnecessarily drop too many characters from the input queue. */
+                        ssize_t l = read(STDIN_FILENO, buf, first ? sizeof(buf) : 1);
                         if (l < 0) {
                                 r = -errno;
                                 goto finish;