hust testsuite: fix a false positive
[oweals/busybox.git] / miscutils / less.c
index 94ecf1686c0e70b1a0e7f704f18c8f2552dd3fe3..61acfdcb5045c06ae69b885f9c8d78f73c6dce6a 100644 (file)
@@ -169,6 +169,7 @@ enum { pattern_valid = 0 };
 struct globals {
        int cur_fline; /* signed */
        int kbd_fd;  /* fd to get input from */
+       int kbd_fd_orig_flags;
        int less_gets_pos;
 /* last position in last line, taking into account tabs */
        size_t last_line_pos;
@@ -304,6 +305,8 @@ static void print_statusline(const char *str)
 static void less_exit(int code)
 {
        set_tty_cooked();
+       if (!(G.kbd_fd_orig_flags & O_NONBLOCK))
+               ndelay_off(kbd_fd);
        clear_line();
        if (code < 0)
                kill_myself_with_sig(- code); /* does not return */
@@ -440,8 +443,6 @@ static int at_end(void)
  */
 static void read_lines(void)
 {
-#define        readbuf bb_common_bufsiz1
-#define sizeof_readbuf COMMON_BUFSIZE
        char *current_line, *p;
        int w = width;
        char last_terminated = terminated;
@@ -451,6 +452,9 @@ static void read_lines(void)
        unsigned old_max_fline = max_fline;
 #endif
 
+#define readbuf bb_common_bufsiz1
+       setup_common_bufsiz();
+
        /* (careful: max_fline can be -1) */
        if (max_fline + 1 > MAXLINES)
                return;
@@ -482,7 +486,7 @@ static void read_lines(void)
                                        time_t t;
 
                                        errno = 0;
-                                       eof_error = safe_read(STDIN_FILENO, readbuf, sizeof_readbuf);
+                                       eof_error = safe_read(STDIN_FILENO, readbuf, COMMON_BUFSIZE);
                                        if (errno != EAGAIN)
                                                break;
                                        t = time(NULL);
@@ -1588,16 +1592,23 @@ static char opp_bracket(char bracket)
 
 static void match_right_bracket(char bracket)
 {
-       unsigned i;
+       unsigned i = cur_fline;
 
-       if (strchr(flines[cur_fline], bracket) == NULL) {
+       if (i >= max_fline
+        || strchr(flines[i], bracket) == NULL
+       ) {
                print_statusline("No bracket in top line");
                return;
        }
+
        bracket = opp_bracket(bracket);
-       for (i = cur_fline + 1; i < max_fline; i++) {
+       for (; i < max_fline; i++) {
                if (strchr(flines[i], bracket) != NULL) {
-                       buffer_line(i);
+                       /*
+                        * Line with matched right bracket becomes
+                        * last visible line
+                        */
+                       buffer_line(i - max_displayed_line);
                        return;
                }
        }
@@ -1606,16 +1617,22 @@ static void match_right_bracket(char bracket)
 
 static void match_left_bracket(char bracket)
 {
-       int i;
+       int i = cur_fline + max_displayed_line;
 
-       if (strchr(flines[cur_fline + max_displayed_line], bracket) == NULL) {
+       if (i >= max_fline
+        || strchr(flines[i], bracket) == NULL
+       ) {
                print_statusline("No bracket in bottom line");
                return;
        }
 
        bracket = opp_bracket(bracket);
-       for (i = cur_fline + max_displayed_line; i >= 0; i--) {
+       for (; i >= 0; i--) {
                if (strchr(flines[i], bracket) != NULL) {
+                       /*
+                        * Line with matched left bracket becomes
+                        * first visible line
+                        */
                        buffer_line(i);
                        return;
                }
@@ -1783,9 +1800,10 @@ int less_main(int argc, char **argv)
        /* Some versions of less can survive w/o controlling tty,
         * try to do the same. This also allows to specify an alternative
         * tty via "less 1<>TTY".
-        * We don't try to use STDOUT_FILENO directly,
+        *
+        * We prefer not to use STDOUT_FILENO directly,
         * since we want to set this fd to non-blocking mode,
-        * and not bother with restoring it on exit.
+        * and not interfere with other processes which share stdout with us.
         */
        tty_name = xmalloc_ttyname(STDOUT_FILENO);
        if (tty_name) {
@@ -1797,10 +1815,12 @@ int less_main(int argc, char **argv)
                /* Try controlling tty */
  try_ctty:
                tty_fd = open(CURRENT_TTY, O_RDONLY);
-               if (tty_fd < 0)
-                       return bb_cat(argv);
+               if (tty_fd < 0) {
+                       /* If all else fails, less 481 uses stdout. Mimic that */
+                       tty_fd = STDOUT_FILENO;
+               }
        }
-       ndelay_on(tty_fd);
+       G.kbd_fd_orig_flags = ndelay_on(tty_fd);
        kbd_fd = tty_fd; /* save in a global */
 
        tcgetattr(kbd_fd, &term_orig);