Fix symlink removal problem noted by Erik Andersen.
[oweals/busybox.git] / more.c
diff --git a/more.c b/more.c
index 9310cf930fc9dc79487c0088ce5bc14e6fa14acb..8ae2661ab64bf7e9c45247e254cd910be9431135 100644 (file)
--- a/more.c
+++ b/more.c
  *
  */
 
-#include "busybox.h"
 #include <stdio.h>
 #include <fcntl.h>
 #include <signal.h>
+#include <stdlib.h>
 #include <sys/ioctl.h>
-#define BB_DECLARE_EXTERN
-#define bb_need_help
-#include "messages.c"
+#include "busybox.h"
 
 /* ED: sparc termios is broken: revert back to old termio handling. */
 #ifdef BB_FEATURE_USE_TERMIOS
 #              define getTermSettings(fd,argp) tcgetattr(fd, argp);
 #      endif
 
-FILE *cin;
+static FILE *cin;
 
 static struct termios initial_settings, new_settings;
 
 static void gotsig(int sig)
 {
        setTermSettings(fileno(cin), &initial_settings);
-       fprintf(stdout, "\n");
-       exit(TRUE);
+       putchar('\n');
+       exit(EXIT_FAILURE);
 }
 #endif /* BB_FEATURE_USE_TERMIOS */
 
@@ -66,10 +64,11 @@ static int terminal_height = 24;
 
 extern int more_main(int argc, char **argv)
 {
-       int c, lines = 0, input = 0;
-       int please_display_more_prompt = 0;
+       int c, lines, input = 0;
+       int please_display_more_prompt;
        struct stat st;
        FILE *file;
+       int len, page_height;
 
 #if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
        struct winsize win = { 0, 0, 0, 0 };
@@ -89,7 +88,7 @@ extern int more_main(int argc, char **argv)
 #ifdef BB_FEATURE_USE_TERMIOS
                cin = fopen("/dev/tty", "r");
                if (!cin)
-                       cin = fopen("/dev/console", "r");
+                       cin = xfopen("/dev/console", "r");
                getTermSettings(fileno(cin), &initial_settings);
                new_settings = initial_settings;
                new_settings.c_cc[VMIN] = 1;
@@ -111,25 +110,26 @@ extern int more_main(int argc, char **argv)
                (void) signal(SIGTERM, gotsig);
 
 #endif
+               len=0;
+               lines = 0;
+               page_height = terminal_height;
+               please_display_more_prompt = 0;
                while ((c = getc(file)) != EOF) {
-                       if (please_display_more_prompt) {
-                               int len = 0;
 
-                               please_display_more_prompt = 0;
-                               lines = 0;
-                               len = fprintf(stdout, "--More-- ");
+                       if (please_display_more_prompt) {
+                               len = printf("--More-- ");
                                if (file != stdin) {
 #if _FILE_OFFSET_BITS == 64
-                                       len += fprintf(stdout, "(%d%% of %lld bytes)",
+                                       len += printf("(%d%% of %lld bytes)",
+                                                  (int) (100 * ((double) ftell(file) /
+                                                  (double) st.st_size)), (long long)st.st_size);
 #else
-                                       len += fprintf(stdout, "(%d%% of %ld bytes)",
+                                       len += printf("(%d%% of %ld bytes)",
+                                                  (int) (100 * ((double) ftell(file) /
+                                                  (double) st.st_size)), (long)st.st_size);
 #endif
-                                                                  (int) (100 *
-                                                                                 ((double) ftell(file) /
-                                                                                  (double) st.st_size)),
-                                                                  st.st_size);
                                }
-                               len += fprintf(stdout, "%s",
+                               len += printf("%s",
 #ifdef BB_FEATURE_USE_TERMIOS
                                                           ""
 #else
@@ -151,21 +151,25 @@ extern int more_main(int argc, char **argv)
 
 #ifdef BB_FEATURE_USE_TERMIOS
                                /* Erase the "More" message */
+                               putc('\r', stdout);
                                while (--len >= 0)
-                                       putc('\b', stdout);
-                               while (++len <= terminal_width)
                                        putc(' ', stdout);
-                               while (--len >= 0)
-                                       putc('\b', stdout);
+                               putc('\r', stdout);
                                fflush(stdout);
 #endif
+                               len=0;
+                               lines = 0;
+                               page_height = terminal_height;
+                               please_display_more_prompt = 0;
 
+                               if (input == 'q')
+                                       goto end;
                        }
 
                        /* 
                         * There are two input streams to worry about here:
                         *
-                        *     c : the character we are reading from the file being "mored"
+                        * c     : the character we are reading from the file being "mored"
                         * input : a character received from the keyboard
                         *
                         * If we hit a newline in the _file_ stream, we want to test and
@@ -173,23 +177,32 @@ extern int more_main(int argc, char **argv)
                         * allows the user to quit while in the middle of a file.
                         */
                        if (c == '\n') {
-                               switch (input) {
-                               case 'q':
-                                       goto end;
-                               case '\n':
-                                       /* increment by just one line if we are at 
-                                        * the end of this line*/
+                               /* increment by just one line if we are at
+                                * the end of this line */
+                               if (input == '\n')
                                        please_display_more_prompt = 1;
-                                       break;
+                               /* Adjust the terminal height for any overlap, so that
+                                * no lines get lost off the top. */
+                               if (len >= terminal_width) {
+                                       div_t result = div( len, terminal_width); 
+                                       if (result.quot) {
+                                               if (result.rem)
+                                                       page_height-=result.quot;
+                                               else
+                                                       page_height-=(result.quot-1);
+                                       }
                                }
-                               if (++lines == terminal_height)
+                               if (++lines >= page_height) {
                                        please_display_more_prompt = 1;
+                               }
+                               len=0;
                        }
                        /*
                         * If we just read a newline from the file being 'mored' and any
                         * key other than a return is hit, scroll by one page
                         */
                        putc(c, stdout);
+                       len++;
                }
                fclose(file);
                fflush(stdout);
@@ -198,7 +211,7 @@ extern int more_main(int argc, char **argv)
        } while (--argc > 0);
   end:
 #ifdef BB_FEATURE_USE_TERMIOS
-       gotsig(0);
+       setTermSettings(fileno(cin), &initial_settings);
 #endif
-       return(TRUE);
+       return 0;
 }