Try to make indent formatting less horrible
[oweals/busybox.git] / util-linux / more.c
index 6cdec729be153762e895636bd73c172530feae2a..f4018f5d188aba1f112c9bf5bad96212e1882978 100644 (file)
@@ -2,14 +2,14 @@
 /*
  * Mini more implementation for busybox
  *
- *
  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
+ * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
  *
- * Latest version blended together by Erik Andersen <andersen@lineo.com>,
+ * Latest version blended together by Erik Andersen <andersen@codepoet.org>,
  * based on the original more implementation by Bruce, and code from the 
  * Debian boot-floppies team.
  *
- * Termios corrects by Vladimir Oleynik <vodz@usa.net>
+ * Termios corrects by Vladimir Oleynik <dzo@simtreas.ru>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,7 +37,7 @@
 
 static FILE *cin;
 
-#ifdef BB_FEATURE_USE_TERMIOS
+#ifdef CONFIG_FEATURE_USE_TERMIOS
 #include <termios.h>
 #define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
 #define getTermSettings(fd,argp) tcgetattr(fd, argp);
@@ -54,7 +54,7 @@ static void gotsig(int sig)
        putchar('\n');
        exit(EXIT_FAILURE);
 }
-#endif /* BB_FEATURE_USE_TERMIOS */
+#endif /* CONFIG_FEATURE_USE_TERMIOS */
 
 
 static int terminal_width = 79;        /* not 80 in case terminal has linefold bug */
@@ -69,21 +69,17 @@ extern int more_main(int argc, char **argv)
        FILE *file;
        int len, page_height;
 
-#if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
-       struct winsize win = { 0, 0, 0, 0 };
-#endif
-
        argc--;
        argv++;
 
 
        /* not use inputing from terminal if usage: more > outfile */
        if(isatty(fileno(stdout))) {
-               cin = fopen("/dev/tty", "r");
+               cin = fopen(CURRENT_TTY, "r");
                if (!cin)
-                       cin = xfopen("/dev/console", "r");
+                       cin = bb_xfopen(CONSOLE_DEV, "r");
                please_display_more_prompt = 0;
-#ifdef BB_FEATURE_USE_TERMIOS
+#ifdef CONFIG_FEATURE_USE_TERMIOS
                getTermSettings(fileno(cin), &initial_settings);
                new_settings = initial_settings;
                new_settings.c_lflag &= ~ICANON;
@@ -105,22 +101,22 @@ extern int more_main(int argc, char **argv)
                if (argc == 0) {
                        file = stdin;
                } else
-                       file = wfopen(*argv, "r");
+                       file = bb_wfopen(*argv, "r");
                if(file==0)
                        goto loop;
                        
+               st.st_size = 0;
                fstat(fileno(file), &st);
 
                if(please_display_more_prompt>0)
                        please_display_more_prompt = 0;
 
-#if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
-               ioctl(fileno(stdout), TIOCGWINSZ, &win);
-               if (win.ws_row > 4)
-                       terminal_height = win.ws_row - 2;
-               if (win.ws_col > 0)
-                       terminal_width = win.ws_col - 1;
-#endif
+               get_terminal_width_height(0, &terminal_width, &terminal_height);
+               if (terminal_height > 4)
+                       terminal_height -= 2;
+               if (terminal_width > 0)
+                       terminal_width -= 1;
+
                len=0;
                lines = 0;
                page_height = terminal_height;
@@ -128,7 +124,7 @@ extern int more_main(int argc, char **argv)
 
                        if (please_display_more_prompt>0) {
                                len = printf("--More-- ");
-                               if (file != stdin) {
+                               if (file != stdin && st.st_size > 0) {
 #if _FILE_OFFSET_BITS == 64
                                        len += printf("(%d%% of %lld bytes)",
                                                   (int) (100 * ((double) ftell(file) /
@@ -147,7 +143,7 @@ extern int more_main(int argc, char **argv)
                                 * to get input from the user.
                                 */
                                input = getc(cin);
-#ifndef BB_FEATURE_USE_TERMIOS
+#ifndef CONFIG_FEATURE_USE_TERMIOS
                                printf("\033[A"); /* up cursor */
 #endif
                                /* Erase the "More" message */