getty: do not emit bogus error message on EOF
[oweals/busybox.git] / util-linux / more.c
index ee559b102eb7dc0d03af7abcc256ea3841f15751..55694e434936cb097fd958efe6632a3163d4aca7 100644 (file)
  */
 
 #include "libbb.h"
-#if ENABLE_FEATURE_USE_TERMIOS
-#include <termios.h>
-#endif /* FEATURE_USE_TERMIOS */
 
-
-#if ENABLE_FEATURE_USE_TERMIOS
+/* Support for FEATURE_USE_TERMIOS */
 
 struct globals {
        int cin_fileno;
        struct termios initial_settings;
        struct termios new_settings;
-};
+} FIX_ALIASING;
 #define G (*(struct globals*)bb_common_bufsiz1)
-//#define G (*ptr_to_globals)
 #define INIT_G() ((void)0)
-//#define INIT_G() PTR_TO_GLOBALS = xzalloc(sizeof(G))
 #define initial_settings (G.initial_settings)
 #define new_settings     (G.new_settings    )
 #define cin_fileno       (G.cin_fileno      )
 
-#define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp)
+#define setTermSettings(fd, argp) do { \
+               if (ENABLE_FEATURE_USE_TERMIOS) tcsetattr(fd, TCSANOW, argp); \
+       } while(0)
 #define getTermSettings(fd, argp) tcgetattr(fd, argp)
 
-static void gotsig(int sig)
+static void gotsig(int sig UNUSED_PARAM)
 {
        bb_putchar('\n');
        setTermSettings(cin_fileno, &initial_settings);
        exit(EXIT_FAILURE);
 }
 
-#else /* !FEATURE_USE_TERMIOS */
-#define INIT_G() ((void)0)
-#define setTermSettings(fd, argp) ((void)0)
-#endif /* FEATURE_USE_TERMIOS */
-
 #define CONVERTED_TAB_SIZE 8
 
 int more_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
-int more_main(int argc, char **argv)
+int more_main(int argc UNUSED_PARAM, char **argv)
 {
        int c = c; /* for gcc */
        int lines;
@@ -64,8 +55,8 @@ int more_main(int argc, char **argv)
        FILE *file;
        FILE *cin;
        int len;
-       int terminal_width;
-       int terminal_height;
+       unsigned terminal_width;
+       unsigned terminal_height;
 
        INIT_G();
 
@@ -74,23 +65,25 @@ int more_main(int argc, char **argv)
         * is not a tty and turns into cat. This makes sense. */
        if (!isatty(STDOUT_FILENO))
                return bb_cat(argv);
-       cin = fopen(CURRENT_TTY, "r");
+       cin = fopen_for_read(CURRENT_TTY);
        if (!cin)
                return bb_cat(argv);
 
-#if ENABLE_FEATURE_USE_TERMIOS
-       cin_fileno = fileno(cin);
-       getTermSettings(cin_fileno, &initial_settings);
-       new_settings = initial_settings;
-       new_settings.c_lflag &= ~ICANON;
-       new_settings.c_lflag &= ~ECHO;
-       new_settings.c_cc[VMIN] = 1;
-       new_settings.c_cc[VTIME] = 0;
-       setTermSettings(cin_fileno, &new_settings);
-       signal(SIGINT, gotsig);
-       signal(SIGQUIT, gotsig);
-       signal(SIGTERM, gotsig);
-#endif
+       if (ENABLE_FEATURE_USE_TERMIOS) {
+               cin_fileno = fileno(cin);
+               getTermSettings(cin_fileno, &initial_settings);
+               new_settings = initial_settings;
+               new_settings.c_lflag &= ~ICANON;
+               new_settings.c_lflag &= ~ECHO;
+               new_settings.c_cc[VMIN] = 1;
+               new_settings.c_cc[VTIME] = 0;
+               setTermSettings(cin_fileno, &new_settings);
+               bb_signals(0
+                       + (1 << SIGINT)
+                       + (1 << SIGQUIT)
+                       + (1 << SIGTERM)
+                       , gotsig);
+       }
 
        do {
                file = stdin;
@@ -117,11 +110,11 @@ int more_main(int argc, char **argv)
                        if (input != 'r' && please_display_more_prompt) {
                                len = printf("--More-- ");
                                if (st.st_size > 0) {
-                                       len += printf("(%d%% of %"OFF_FMT"d bytes)",
+                                       len += printf("(%u%% of %"OFF_FMT"u bytes)",
                                                (int) (ftello(file)*100 / st.st_size),
                                                st.st_size);
                                }
-                               fflush(stdout);
+                               fflush_all();
 
                                /*
                                 * We've just displayed the "--More--" prompt, so now we need
@@ -130,9 +123,8 @@ int more_main(int argc, char **argv)
                                for (;;) {
                                        input = getc(cin);
                                        input = tolower(input);
-#if !ENABLE_FEATURE_USE_TERMIOS
-                                       printf("\033[A"); /* up cursor */
-#endif
+                                       if (!ENABLE_FEATURE_USE_TERMIOS)
+                                               printf("\033[A"); /* cursor up */
                                        /* Erase the last message */
                                        printf("\r%*s\r", len, "");
 
@@ -154,10 +146,10 @@ int more_main(int argc, char **argv)
 
                                /* The user may have resized the terminal.
                                 * Re-read the dimensions. */
-#if ENABLE_FEATURE_USE_TERMIOS
-                               get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
-                               terminal_height -= 1;
-#endif
+                               if (ENABLE_FEATURE_USE_TERMIOS) {
+                                       get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
+                                       terminal_height -= 1;
+                               }
                        }
 
                        /* Crudely convert tabs into spaces, which are
@@ -165,7 +157,7 @@ int more_main(int argc, char **argv)
                        if (c == '\t') {
                                spaces = CONVERTED_TAB_SIZE - 1;
                                c = ' ';
-                       }
+                       }
 
                        /*
                         * There are two input streams to worry about here:
@@ -197,7 +189,7 @@ int more_main(int argc, char **argv)
                        putchar(c);
                }
                fclose(file);
-               fflush(stdout);
+               fflush_all();
        } while (*argv && *++argv);
  end:
        setTermSettings(cin_fileno, &initial_settings);