lineedit: do not hang on error, but return error indicator.
[oweals/busybox.git] / util-linux / more.c
index 2d5535991a447423d7bff9eb1712c24861a9eb25..1fd6f9ee82c975afe3b8821bc1792a1c9d05b5ba 100644 (file)
  *
  * Termios corrects by Vladimir Oleynik <dzo@simtreas.ru>
  *
- * Licensed under GPLv2 or later, see file License in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 #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 INIT_G() ((void)0)
 #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 c = c; /* for compiler */
        int lines;
        int input = 0;
        int spaces = 0;
@@ -62,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();
 
@@ -72,25 +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);
-       bb_signals(0
-               + (1 << SIGINT)
-               + (1 << SIGQUIT)
-               + (1 << 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
@@ -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);