diff options
-rw-r--r-- | util-linux/more.c | 68 |
1 files changed, 28 insertions, 40 deletions
diff --git a/util-linux/more.c b/util-linux/more.c index 85209ce61..d048ace92 100644 --- a/util-linux/more.c +++ b/util-linux/more.c | |||
@@ -17,11 +17,11 @@ | |||
17 | #include "busybox.h" | 17 | #include "busybox.h" |
18 | 18 | ||
19 | 19 | ||
20 | #ifdef CONFIG_FEATURE_USE_TERMIOS | 20 | #if ENABLE_FEATURE_USE_TERMIOS |
21 | static int cin_fileno; | 21 | static int cin_fileno; |
22 | #include <termios.h> | 22 | #include <termios.h> |
23 | #define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp) | 23 | #define setTermSettings(fd, argp) tcsetattr(fd, TCSANOW, argp) |
24 | #define getTermSettings(fd,argp) tcgetattr(fd, argp); | 24 | #define getTermSettings(fd, argp) tcgetattr(fd, argp); |
25 | 25 | ||
26 | static struct termios initial_settings, new_settings; | 26 | static struct termios initial_settings, new_settings; |
27 | 27 | ||
@@ -35,7 +35,7 @@ static void gotsig(int sig) | |||
35 | putchar('\n'); | 35 | putchar('\n'); |
36 | exit(EXIT_FAILURE); | 36 | exit(EXIT_FAILURE); |
37 | } | 37 | } |
38 | #endif /* CONFIG_FEATURE_USE_TERMIOS */ | 38 | #endif /* FEATURE_USE_TERMIOS */ |
39 | 39 | ||
40 | 40 | ||
41 | int more_main(int argc, char **argv) | 41 | int more_main(int argc, char **argv) |
@@ -52,14 +52,14 @@ int more_main(int argc, char **argv) | |||
52 | argc--; | 52 | argc--; |
53 | argv++; | 53 | argv++; |
54 | 54 | ||
55 | 55 | cin = stdin; | |
56 | /* not use inputing from terminal if usage: more > outfile */ | 56 | /* use input from terminal unless we do "more >outfile" */ |
57 | if(isatty(STDOUT_FILENO)) { | 57 | if (isatty(STDOUT_FILENO)) { |
58 | cin = fopen(CURRENT_TTY, "r"); | 58 | cin = fopen(CURRENT_TTY, "r"); |
59 | if (!cin) | 59 | if (!cin) |
60 | cin = xfopen(CONSOLE_DEV, "r"); | 60 | cin = xfopen(CONSOLE_DEV, "r"); |
61 | please_display_more_prompt = 2; | 61 | please_display_more_prompt = 2; |
62 | #ifdef CONFIG_FEATURE_USE_TERMIOS | 62 | #if ENABLE_FEATURE_USE_TERMIOS |
63 | cin_fileno = fileno(cin); | 63 | cin_fileno = fileno(cin); |
64 | getTermSettings(cin_fileno, &initial_settings); | 64 | getTermSettings(cin_fileno, &initial_settings); |
65 | new_settings = initial_settings; | 65 | new_settings = initial_settings; |
@@ -69,21 +69,19 @@ int more_main(int argc, char **argv) | |||
69 | new_settings.c_cc[VTIME] = 0; | 69 | new_settings.c_cc[VTIME] = 0; |
70 | setTermSettings(cin_fileno, &new_settings); | 70 | setTermSettings(cin_fileno, &new_settings); |
71 | atexit(set_tty_to_initial_mode); | 71 | atexit(set_tty_to_initial_mode); |
72 | (void) signal(SIGINT, gotsig); | 72 | signal(SIGINT, gotsig); |
73 | (void) signal(SIGQUIT, gotsig); | 73 | signal(SIGQUIT, gotsig); |
74 | (void) signal(SIGTERM, gotsig); | 74 | signal(SIGTERM, gotsig); |
75 | #endif | 75 | #endif |
76 | } else { | ||
77 | cin = stdin; | ||
78 | } | 76 | } |
79 | 77 | ||
80 | do { | 78 | do { |
81 | if (argc == 0) { | 79 | file = stdin; |
82 | file = stdin; | 80 | if (argc != 0) { |
83 | } else | ||
84 | file = fopen_or_warn(*argv, "r"); | 81 | file = fopen_or_warn(*argv, "r"); |
85 | if(file==0) | 82 | if (!file) |
86 | goto loop; | 83 | goto loop; |
84 | } | ||
87 | 85 | ||
88 | st.st_size = 0; | 86 | st.st_size = 0; |
89 | fstat(fileno(file), &st); | 87 | fstat(fileno(file), &st); |
@@ -96,7 +94,7 @@ int more_main(int argc, char **argv) | |||
96 | if (terminal_width > 0) | 94 | if (terminal_width > 0) |
97 | terminal_width -= 1; | 95 | terminal_width -= 1; |
98 | 96 | ||
99 | len=0; | 97 | len = 0; |
100 | lines = 0; | 98 | lines = 0; |
101 | page_height = terminal_height; | 99 | page_height = terminal_height; |
102 | while ((c = getc(file)) != EOF) { | 100 | while ((c = getc(file)) != EOF) { |
@@ -104,17 +102,10 @@ int more_main(int argc, char **argv) | |||
104 | if ((please_display_more_prompt & 3) == 3) { | 102 | if ((please_display_more_prompt & 3) == 3) { |
105 | len = printf("--More-- "); | 103 | len = printf("--More-- "); |
106 | if (file != stdin && st.st_size > 0) { | 104 | if (file != stdin && st.st_size > 0) { |
107 | #if _FILE_OFFSET_BITS == 64 | 105 | len += printf("(%d%% of %"OFF_FMT"d bytes)", |
108 | len += printf("(%d%% of %lld bytes)", | 106 | (int) (ftello(file)*100 / st.st_size), |
109 | (int) (100 * ((double) ftell(file) / | 107 | st.st_size); |
110 | (double) st.st_size)), (long long)st.st_size); | ||
111 | #else | ||
112 | len += printf("(%d%% of %ld bytes)", | ||
113 | (int) (100 * ((double) ftell(file) / | ||
114 | (double) st.st_size)), (long)st.st_size); | ||
115 | #endif | ||
116 | } | 108 | } |
117 | |||
118 | fflush(stdout); | 109 | fflush(stdout); |
119 | 110 | ||
120 | /* | 111 | /* |
@@ -122,15 +113,12 @@ int more_main(int argc, char **argv) | |||
122 | * to get input from the user. | 113 | * to get input from the user. |
123 | */ | 114 | */ |
124 | input = getc(cin); | 115 | input = getc(cin); |
125 | #ifndef CONFIG_FEATURE_USE_TERMIOS | 116 | #if !ENABLE_FEATURE_USE_TERMIOS |
126 | printf("\033[A"); /* up cursor */ | 117 | printf("\033[A"); /* up cursor */ |
127 | #endif | 118 | #endif |
128 | /* Erase the "More" message */ | 119 | /* Erase the "More" message */ |
129 | putc('\r', stdout); | 120 | printf("\r%*s\r", len, ""); |
130 | while (--len >= 0) | 121 | len = 0; |
131 | putc(' ', stdout); | ||
132 | putc('\r', stdout); | ||
133 | len=0; | ||
134 | lines = 0; | 122 | lines = 0; |
135 | page_height = terminal_height; | 123 | page_height = terminal_height; |
136 | please_display_more_prompt &= ~1; | 124 | please_display_more_prompt &= ~1; |
@@ -162,15 +150,15 @@ int more_main(int argc, char **argv) | |||
162 | rem = len - (quot * terminal_width); | 150 | rem = len - (quot * terminal_width); |
163 | if (quot) { | 151 | if (quot) { |
164 | if (rem) | 152 | if (rem) |
165 | page_height-=quot; | 153 | page_height -= quot; |
166 | else | 154 | else |
167 | page_height-=(quot-1); | 155 | page_height -= (quot - 1); |
168 | } | 156 | } |
169 | } | 157 | } |
170 | if (++lines >= page_height) { | 158 | if (++lines >= page_height) { |
171 | please_display_more_prompt |= 1; | 159 | please_display_more_prompt |= 1; |
172 | } | 160 | } |
173 | len=0; | 161 | len = 0; |
174 | } | 162 | } |
175 | /* | 163 | /* |
176 | * If we just read a newline from the file being 'mored' and any | 164 | * If we just read a newline from the file being 'mored' and any |
@@ -181,9 +169,9 @@ int more_main(int argc, char **argv) | |||
181 | } | 169 | } |
182 | fclose(file); | 170 | fclose(file); |
183 | fflush(stdout); | 171 | fflush(stdout); |
184 | loop: | 172 | loop: |
185 | argv++; | 173 | argv++; |
186 | } while (--argc > 0); | 174 | } while (--argc > 0); |
187 | end: | 175 | end: |
188 | return 0; | 176 | return 0; |
189 | } | 177 | } |