diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /util-linux/more.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'util-linux/more.c')
-rw-r--r-- | util-linux/more.c | 199 |
1 files changed, 102 insertions, 97 deletions
diff --git a/util-linux/more.c b/util-linux/more.c index 821427dda..d5711aa2f 100644 --- a/util-linux/more.c +++ b/util-linux/more.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini more implementation for busybox | 3 | * Mini more implementation for busybox |
3 | * | 4 | * |
@@ -45,19 +46,20 @@ static const char more_usage[] = "more [file ...]\n"; | |||
45 | # define stty(fd,argp) tcsetattr(fd,TCSANOW,argp) | 46 | # define stty(fd,argp) tcsetattr(fd,TCSANOW,argp) |
46 | #endif | 47 | #endif |
47 | 48 | ||
48 | FILE *cin; | 49 | FILE *cin; |
49 | struct termios initial_settings, new_settings; | 50 | struct termios initial_settings, new_settings; |
50 | 51 | ||
51 | void gotsig(int sig) { | 52 | void gotsig(int sig) |
52 | stty(fileno(cin), &initial_settings); | 53 | { |
53 | fprintf(stdout, "\n"); | 54 | stty(fileno(cin), &initial_settings); |
54 | exit( TRUE); | 55 | fprintf(stdout, "\n"); |
55 | } | 56 | exit(TRUE); |
57 | } | ||
56 | #endif | 58 | #endif |
57 | 59 | ||
58 | 60 | ||
59 | 61 | ||
60 | #define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */ | 62 | #define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */ |
61 | #define TERMINAL_HEIGHT 24 | 63 | #define TERMINAL_HEIGHT 24 |
62 | 64 | ||
63 | 65 | ||
@@ -72,118 +74,121 @@ static int terminal_width = 0, terminal_height = 0; | |||
72 | 74 | ||
73 | extern int more_main(int argc, char **argv) | 75 | extern int more_main(int argc, char **argv) |
74 | { | 76 | { |
75 | int c, lines=0, input=0; | 77 | int c, lines = 0, input = 0; |
76 | int next_page=0; | 78 | int next_page = 0; |
77 | struct stat st; | 79 | struct stat st; |
78 | FILE *file; | 80 | FILE *file; |
81 | |||
79 | #ifdef BB_FEATURE_AUTOWIDTH | 82 | #ifdef BB_FEATURE_AUTOWIDTH |
80 | struct winsize win = {0,0}; | 83 | struct winsize win = { 0, 0 }; |
81 | #endif | 84 | #endif |
82 | 85 | ||
83 | argc--; | 86 | argc--; |
84 | argv++; | 87 | argv++; |
85 | |||
86 | if ( argc > 0 && (strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0) ) { | ||
87 | usage (more_usage); | ||
88 | } | ||
89 | do { | ||
90 | if (argc==0) { | ||
91 | file = stdin; | ||
92 | } | ||
93 | else | ||
94 | file = fopen(*argv, "r"); | ||
95 | 88 | ||
96 | if (file == NULL) { | 89 | if (argc > 0 |
97 | perror(*argv); | 90 | && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0)) { |
98 | exit(FALSE); | 91 | usage(more_usage); |
99 | } | 92 | } |
100 | fstat(fileno(file), &st); | 93 | do { |
94 | if (argc == 0) { | ||
95 | file = stdin; | ||
96 | } else | ||
97 | file = fopen(*argv, "r"); | ||
98 | |||
99 | if (file == NULL) { | ||
100 | perror(*argv); | ||
101 | exit(FALSE); | ||
102 | } | ||
103 | fstat(fileno(file), &st); | ||
101 | 104 | ||
102 | #ifdef BB_FEATURE_USE_TERMIOS | 105 | #ifdef BB_FEATURE_USE_TERMIOS |
103 | cin = fopen("/dev/tty", "r"); | 106 | cin = fopen("/dev/tty", "r"); |
104 | if (!cin) | 107 | if (!cin) |
105 | cin = fopen("/dev/console", "r"); | 108 | cin = fopen("/dev/console", "r"); |
106 | #ifdef USE_OLD_TERMIO | 109 | #ifdef USE_OLD_TERMIO |
107 | ioctl(fileno(cin),TCGETA,&initial_settings); | 110 | ioctl(fileno(cin), TCGETA, &initial_settings); |
108 | #else | 111 | #else |
109 | tcgetattr(fileno(cin),&initial_settings); | 112 | tcgetattr(fileno(cin), &initial_settings); |
110 | #endif | 113 | #endif |
111 | new_settings = initial_settings; | 114 | new_settings = initial_settings; |
112 | new_settings.c_lflag &= ~ICANON; | 115 | new_settings.c_lflag &= ~ICANON; |
113 | new_settings.c_lflag &= ~ECHO; | 116 | new_settings.c_lflag &= ~ECHO; |
114 | stty(fileno(cin), &new_settings); | 117 | stty(fileno(cin), &new_settings); |
115 | 118 | ||
116 | #ifdef BB_FEATURE_AUTOWIDTH | 119 | #ifdef BB_FEATURE_AUTOWIDTH |
117 | ioctl(fileno(stdout), TIOCGWINSZ, &win); | 120 | ioctl(fileno(stdout), TIOCGWINSZ, &win); |
118 | if (win.ws_row > 4) | 121 | if (win.ws_row > 4) |
119 | terminal_height = win.ws_row - 2; | 122 | terminal_height = win.ws_row - 2; |
120 | if (win.ws_col > 0) | 123 | if (win.ws_col > 0) |
121 | terminal_width = win.ws_col - 1; | 124 | terminal_width = win.ws_col - 1; |
122 | #endif | 125 | #endif |
123 | 126 | ||
124 | (void) signal(SIGINT, gotsig); | 127 | (void) signal(SIGINT, gotsig); |
125 | (void) signal(SIGQUIT, gotsig); | 128 | (void) signal(SIGQUIT, gotsig); |
126 | (void) signal(SIGTERM, gotsig); | 129 | (void) signal(SIGTERM, gotsig); |
127 | 130 | ||
128 | #endif | 131 | #endif |
129 | while ((c = getc(file)) != EOF) { | 132 | while ((c = getc(file)) != EOF) { |
130 | if ( next_page ) { | 133 | if (next_page) { |
131 | int len=0; | 134 | int len = 0; |
132 | next_page = 0; | 135 | |
133 | lines=0; | 136 | next_page = 0; |
134 | len = fprintf(stdout, "--More-- "); | 137 | lines = 0; |
135 | if (file != stdin) { | 138 | len = fprintf(stdout, "--More-- "); |
136 | len += fprintf(stdout, "(%d%% of %ld bytes)", | 139 | if (file != stdin) { |
137 | (int) (100*( (double) ftell(file) / (double) st.st_size )), | 140 | len += fprintf(stdout, "(%d%% of %ld bytes)", |
138 | st.st_size); | 141 | (int) (100 * |
139 | } | 142 | ((double) ftell(file) / |
140 | len += fprintf(stdout, "%s", | 143 | (double) st.st_size)), |
144 | st.st_size); | ||
145 | } | ||
146 | len += fprintf(stdout, "%s", | ||
141 | #ifdef BB_FEATURE_USE_TERMIOS | 147 | #ifdef BB_FEATURE_USE_TERMIOS |
142 | "" | 148 | "" |
143 | #else | 149 | #else |
144 | "\n" | 150 | "\n" |
145 | #endif | 151 | #endif |
146 | ); | 152 | ); |
147 | 153 | ||
148 | fflush(stdout); | 154 | fflush(stdout); |
149 | input = getc( cin); | 155 | input = getc(cin); |
150 | 156 | ||
151 | #ifdef BB_FEATURE_USE_TERMIOS | 157 | #ifdef BB_FEATURE_USE_TERMIOS |
152 | /* Erase the "More" message */ | 158 | /* Erase the "More" message */ |
153 | while(--len >= 0) | 159 | while (--len >= 0) |
154 | putc('\b', stdout); | 160 | putc('\b', stdout); |
155 | while(++len <= terminal_width) | 161 | while (++len <= terminal_width) |
156 | putc(' ', stdout); | 162 | putc(' ', stdout); |
157 | while(--len >= 0) | 163 | while (--len >= 0) |
158 | putc('\b', stdout); | 164 | putc('\b', stdout); |
159 | fflush(stdout); | 165 | fflush(stdout); |
160 | #endif | 166 | #endif |
161 | 167 | ||
162 | } | 168 | } |
163 | if (c == '\n' ) { | 169 | if (c == '\n') { |
164 | switch(input) { | 170 | switch (input) { |
165 | case 'q': | 171 | case 'q': |
166 | goto end; | 172 | goto end; |
167 | case '\n': | 173 | case '\n': |
168 | /* increment by just one line if we are at | 174 | /* increment by just one line if we are at |
169 | * the end of this line*/ | 175 | * the end of this line*/ |
170 | next_page = 1; | 176 | next_page = 1; |
171 | break; | 177 | break; |
178 | } | ||
179 | if (++lines == terminal_height) | ||
180 | next_page = 1; | ||
181 | } | ||
182 | putc(c, stdout); | ||
172 | } | 183 | } |
173 | if ( ++lines == terminal_height ) | 184 | fclose(file); |
174 | next_page = 1; | 185 | fflush(stdout); |
175 | } | ||
176 | putc(c, stdout); | ||
177 | } | ||
178 | fclose(file); | ||
179 | fflush(stdout); | ||
180 | 186 | ||
181 | argv++; | 187 | argv++; |
182 | } while (--argc > 0); | 188 | } while (--argc > 0); |
183 | end: | 189 | end: |
184 | #ifdef BB_FEATURE_USE_TERMIOS | 190 | #ifdef BB_FEATURE_USE_TERMIOS |
185 | gotsig(0); | 191 | gotsig(0); |
186 | #endif | 192 | #endif |
187 | exit(TRUE); | 193 | exit(TRUE); |
188 | } | 194 | } |
189 | |||