diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-10-06 00:30:51 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-10-06 00:30:51 +0000 |
commit | 4bea32abb5d25e3b83047c22fd3f4e8fae2479cd (patch) | |
tree | c8aad7a8b8c573c2421b7fb3e58fdd00a3c1fe9b /util-linux/more.c | |
parent | 2b69c40e8060934c115922c012737bd471956f09 (diff) | |
download | busybox-w32-4bea32abb5d25e3b83047c22fd3f4e8fae2479cd.tar.gz busybox-w32-4bea32abb5d25e3b83047c22fd3f4e8fae2479cd.tar.bz2 busybox-w32-4bea32abb5d25e3b83047c22fd3f4e8fae2479cd.zip |
latest and greatest.
Diffstat (limited to '')
-rw-r--r-- | util-linux/more.c | 169 |
1 files changed, 72 insertions, 97 deletions
diff --git a/util-linux/more.c b/util-linux/more.c index 65409999b..2ea709278 100644 --- a/util-linux/more.c +++ b/util-linux/more.c | |||
@@ -1,110 +1,85 @@ | |||
1 | /* | ||
2 | * Mini more implementation for busybox | ||
3 | * | ||
4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License | ||
17 | * along with this program; if not, write to the Free Software | ||
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
1 | #include "internal.h" | 22 | #include "internal.h" |
2 | #include <stdio.h> | 23 | #include <stdio.h> |
3 | #include <sys/types.h> | 24 | #include <signal.h> |
4 | #include <sys/stat.h> | ||
5 | #include <sys/ioctl.h> | ||
6 | #include <fcntl.h> | ||
7 | |||
8 | #define BB_MORE_TERM | ||
9 | |||
10 | #ifdef BB_MORE_TERM | ||
11 | #include <termios.h> | ||
12 | #include <signal.h> | ||
13 | 25 | ||
14 | FILE *cin; | 26 | const char more_usage[] = "[file ...]"; |
15 | struct termios initial_settings, new_settings; | ||
16 | 27 | ||
17 | void gotsig(int sig) { | ||
18 | tcsetattr(fileno(cin), TCSANOW, &initial_settings); | ||
19 | exit(0); | ||
20 | } | ||
21 | #endif | ||
22 | 28 | ||
23 | const char more_usage[] = "more [file]\n" | ||
24 | "\n" | ||
25 | "\tDisplays a file, one page at a time.\n" | ||
26 | "\tIf there are no arguments, the standard input is displayed.\n"; | ||
27 | 29 | ||
28 | extern int | 30 | extern int more_main(int argc, char **argv) |
29 | more_fn(const struct FileInfo * i) | ||
30 | { | 31 | { |
31 | FILE * f = stdin; | 32 | int c, lines=0; |
32 | int c; | 33 | int next_page=0, rows = 24, cols=79; |
33 | int lines = 0, tlines = 0; | 34 | struct stat st; |
34 | int next_page = 0; | 35 | FILE *file = stdin; |
35 | int rows = 24, cols = 79; | ||
36 | #ifdef BB_MORE_TERM | ||
37 | long sizeb = 0; | ||
38 | struct stat st; | ||
39 | struct winsize win; | ||
40 | #endif | ||
41 | |||
42 | if ( i ) { | ||
43 | if (! (f = fopen(i->source, "r") )) { | ||
44 | name_and_error(i->source); | ||
45 | return 1; | ||
46 | } | ||
47 | fstat(fileno(f), &st); | ||
48 | sizeb = st.st_size / 100; | ||
49 | } | ||
50 | |||
51 | #ifdef BB_MORE_TERM | ||
52 | cin = fopen("/dev/tty", "r"); | ||
53 | tcgetattr(fileno(cin),&initial_settings); | ||
54 | new_settings = initial_settings; | ||
55 | new_settings.c_lflag &= ~ICANON; | ||
56 | new_settings.c_lflag &= ~ECHO; | ||
57 | tcsetattr(fileno(cin), TCSANOW, &new_settings); | ||
58 | |||
59 | (void) signal(SIGINT, gotsig); | ||
60 | 36 | ||
61 | ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); | 37 | if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) { |
62 | if (win.ws_row > 4) rows = win.ws_row - 2; | 38 | fprintf(stderr, "Usage: %s %s", *argv, more_usage); |
63 | if (win.ws_col > 0) cols = win.ws_col - 1; | 39 | return(FALSE); |
40 | } | ||
41 | argc--; | ||
42 | argv++; | ||
64 | 43 | ||
44 | while (argc-- > 0) { | ||
45 | file = fopen(*argv, "r"); | ||
46 | if (file == NULL) { | ||
47 | perror(*argv); | ||
48 | return(FALSE); | ||
49 | } | ||
50 | fstat(fileno(file), &st); | ||
65 | 51 | ||
52 | while ((c = getc(file)) != EOF) { | ||
53 | if ( next_page ) { | ||
54 | int len=0; | ||
55 | next_page = 0; | ||
56 | lines=0; | ||
57 | len = fprintf(stdout, "--More-- (%d%% of %ld bytes)\n", | ||
58 | (int) (100*( (double) ftell(file) / (double) st.st_size )), | ||
59 | st.st_size); | ||
60 | fflush(stdout); | ||
61 | getc( stdin); | ||
62 | #if 0 | ||
63 | while(len-- > 0) | ||
64 | putc('\b', stdout); | ||
65 | while(len++ < cols) | ||
66 | putc('8', stdout); | ||
67 | while(len-- > 0) | ||
68 | putc('\b', stdout); | ||
69 | fflush(stdout); | ||
66 | #endif | 70 | #endif |
67 | 71 | } | |
68 | while ( (c = getc(f)) != EOF ) { | 72 | if ( c == '\n' && ++lines == (rows + 1) ) |
69 | if ( next_page ) { | 73 | next_page = 1; |
70 | char garbage; | 74 | putc(c, stdout); |
71 | int len; | ||
72 | tlines += lines; | ||
73 | lines = 0; | ||
74 | next_page = 0; //Percentage is based on bytes, not lines. | ||
75 | if ( i && i->source ) //It is not very acurate, but still useful. | ||
76 | len = printf("%s - %%%2ld - line: %d", i->source, (ftell(f) - sizeb - sizeb) / sizeb, tlines); | ||
77 | else | ||
78 | len = printf("line: %d", tlines); | ||
79 | |||
80 | fflush(stdout); | ||
81 | #ifndef BB_MORE_TERM | ||
82 | read(2, &garbage, 1); | ||
83 | #else | ||
84 | do { | ||
85 | fread(&garbage, 1, 1, cin); | ||
86 | } while ((garbage != ' ') && (garbage != '\n')); | ||
87 | |||
88 | if (garbage == '\n') { | ||
89 | lines = rows; | ||
90 | tlines -= rows; | ||
91 | } | ||
92 | garbage = 0; | ||
93 | //clear line, since tabs don't overwrite. | ||
94 | while(len-- > 0) putchar('\b'); | ||
95 | while(len++ < cols) putchar(' '); | ||
96 | while(len-- > 0) putchar('\b'); | ||
97 | fflush(stdout); | ||
98 | #endif | ||
99 | } | ||
100 | putchar(c); | ||
101 | if ( c == '\n' && ++lines == (rows + 1) ) | ||
102 | next_page = 1; | ||
103 | } | 75 | } |
104 | if ( f != stdin ) | 76 | fclose(file); |
105 | fclose(f); | 77 | fflush(stdout); |
106 | #ifdef BB_MORE_TERM | 78 | |
107 | gotsig(0); | 79 | argc--; |
108 | #endif | 80 | argv++; |
109 | return 0; | 81 | } |
82 | return(TRUE); | ||
110 | } | 83 | } |
84 | |||
85 | |||