aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--util-linux/more.c43
1 files changed, 21 insertions, 22 deletions
diff --git a/util-linux/more.c b/util-linux/more.c
index 04b29de17..5ece188b0 100644
--- a/util-linux/more.c
+++ b/util-linux/more.c
@@ -35,9 +35,9 @@
35#include <sys/ioctl.h> 35#include <sys/ioctl.h>
36#include "busybox.h" 36#include "busybox.h"
37 37
38static FILE *cin;
39 38
40#ifdef CONFIG_FEATURE_USE_TERMIOS 39#ifdef CONFIG_FEATURE_USE_TERMIOS
40static int cin_fileno;
41#include <termios.h> 41#include <termios.h>
42#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) 42#define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
43#define getTermSettings(fd,argp) tcgetattr(fd, argp); 43#define getTermSettings(fd,argp) tcgetattr(fd, argp);
@@ -46,7 +46,7 @@ static struct termios initial_settings, new_settings;
46 46
47static void set_tty_to_initial_mode(void) 47static void set_tty_to_initial_mode(void)
48{ 48{
49 setTermSettings(fileno(cin), &initial_settings); 49 setTermSettings(cin_fileno, &initial_settings);
50} 50}
51 51
52static void gotsig(int sig) 52static void gotsig(int sig)
@@ -57,18 +57,19 @@ static void gotsig(int sig)
57#endif /* CONFIG_FEATURE_USE_TERMIOS */ 57#endif /* CONFIG_FEATURE_USE_TERMIOS */
58 58
59 59
60static int terminal_width = 79; /* not 80 in case terminal has linefold bug */
61static int terminal_height = 24;
62
63
64extern int more_main(int argc, char **argv) 60extern int more_main(int argc, char **argv)
65{ 61{
66 int c, lines, input = 0; 62 int c, lines, input = 0;
67 int please_display_more_prompt = -1; 63 int please_display_more_prompt = 0;
68 struct stat st; 64 struct stat st;
69 FILE *file; 65 FILE *file;
70 FILE *in_file = stdin; 66 FILE *cin;
71 int len, page_height; 67 int len, page_height;
68 int terminal_width;
69 int terminal_height;
70#ifndef CONFIG_FEATURE_USE_TERMIOS
71 int cin_fileno;
72#endif
72 73
73 argc--; 74 argc--;
74 argv++; 75 argv++;
@@ -79,21 +80,23 @@ extern int more_main(int argc, char **argv)
79 cin = fopen(CURRENT_TTY, "r"); 80 cin = fopen(CURRENT_TTY, "r");
80 if (!cin) 81 if (!cin)
81 cin = bb_xfopen(CONSOLE_DEV, "r"); 82 cin = bb_xfopen(CONSOLE_DEV, "r");
82 in_file = cin; 83 cin_fileno = fileno(cin);
83 please_display_more_prompt = 0; 84 please_display_more_prompt = 2;
84#ifdef CONFIG_FEATURE_USE_TERMIOS 85#ifdef CONFIG_FEATURE_USE_TERMIOS
85 getTermSettings(fileno(cin), &initial_settings); 86 getTermSettings(cin_fileno, &initial_settings);
86 new_settings = initial_settings; 87 new_settings = initial_settings;
87 new_settings.c_lflag &= ~ICANON; 88 new_settings.c_lflag &= ~ICANON;
88 new_settings.c_lflag &= ~ECHO; 89 new_settings.c_lflag &= ~ECHO;
89 new_settings.c_cc[VMIN] = 1; 90 new_settings.c_cc[VMIN] = 1;
90 new_settings.c_cc[VTIME] = 0; 91 new_settings.c_cc[VTIME] = 0;
91 setTermSettings(fileno(cin), &new_settings); 92 setTermSettings(cin_fileno, &new_settings);
92 atexit(set_tty_to_initial_mode); 93 atexit(set_tty_to_initial_mode);
93 (void) signal(SIGINT, gotsig); 94 (void) signal(SIGINT, gotsig);
94 (void) signal(SIGQUIT, gotsig); 95 (void) signal(SIGQUIT, gotsig);
95 (void) signal(SIGTERM, gotsig); 96 (void) signal(SIGTERM, gotsig);
96#endif 97#endif
98 } else {
99 cin = stdin;
97 } 100 }
98 101
99 do { 102 do {
@@ -107,10 +110,9 @@ extern int more_main(int argc, char **argv)
107 st.st_size = 0; 110 st.st_size = 0;
108 fstat(fileno(file), &st); 111 fstat(fileno(file), &st);
109 112
110 if(please_display_more_prompt>0) 113 please_display_more_prompt &= ~1;
111 please_display_more_prompt = 0;
112 114
113 get_terminal_width_height(fileno(in_file), &terminal_width, &terminal_height); 115 get_terminal_width_height(cin_fileno, &terminal_width, &terminal_height);
114 if (terminal_height > 4) 116 if (terminal_height > 4)
115 terminal_height -= 2; 117 terminal_height -= 2;
116 if (terminal_width > 0) 118 if (terminal_width > 0)
@@ -121,7 +123,7 @@ extern int more_main(int argc, char **argv)
121 page_height = terminal_height; 123 page_height = terminal_height;
122 while ((c = getc(file)) != EOF) { 124 while ((c = getc(file)) != EOF) {
123 125
124 if (please_display_more_prompt>0) { 126 if ((please_display_more_prompt & 3) == 3) {
125 len = printf("--More-- "); 127 len = printf("--More-- ");
126 if (file != stdin && st.st_size > 0) { 128 if (file != stdin && st.st_size > 0) {
127#if _FILE_OFFSET_BITS == 64 129#if _FILE_OFFSET_BITS == 64
@@ -150,11 +152,10 @@ extern int more_main(int argc, char **argv)
150 while (--len >= 0) 152 while (--len >= 0)
151 putc(' ', stdout); 153 putc(' ', stdout);
152 putc('\r', stdout); 154 putc('\r', stdout);
153 fflush(stdout);
154 len=0; 155 len=0;
155 lines = 0; 156 lines = 0;
156 page_height = terminal_height; 157 page_height = terminal_height;
157 please_display_more_prompt = 0; 158 please_display_more_prompt &= ~1;
158 159
159 if (input == 'q') 160 if (input == 'q')
160 goto end; 161 goto end;
@@ -174,8 +175,7 @@ extern int more_main(int argc, char **argv)
174 /* increment by just one line if we are at 175 /* increment by just one line if we are at
175 * the end of this line */ 176 * the end of this line */
176 if (input == '\n') 177 if (input == '\n')
177 if(please_display_more_prompt==0) 178 please_display_more_prompt |= 1;
178 please_display_more_prompt = 1;
179 /* Adjust the terminal height for any overlap, so that 179 /* Adjust the terminal height for any overlap, so that
180 * no lines get lost off the top. */ 180 * no lines get lost off the top. */
181 if (len >= terminal_width) { 181 if (len >= terminal_width) {
@@ -190,8 +190,7 @@ extern int more_main(int argc, char **argv)
190 } 190 }
191 } 191 }
192 if (++lines >= page_height) { 192 if (++lines >= page_height) {
193 if(please_display_more_prompt==0) 193 please_display_more_prompt |= 1;
194 please_display_more_prompt = 1;
195 } 194 }
196 len=0; 195 len=0;
197 } 196 }