2 * Mini more implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Blended by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * based on the original more implementation and code from the Debian
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 /* Turning this off makes things a bit smaller (and less pretty) */
28 #define BB_FEATURE_USE_TERMIOS
29 /* Turning this off makes things a bit smaller (and less pretty) */
30 #define BB_FEATURE_AUTOWIDTH
38 #include <sys/ioctl.h>
40 static const char more_usage[] = "[file ...]";
42 /* ED: sparc termios is broken: revert back to old termio handling. */
43 #ifdef BB_FEATURE_USE_TERMIOS
46 # define USE_OLD_TERMIO
48 # define termios termio
49 # define stty(fd,argp) ioctl(fd,TCSETAF,argp)
52 # define stty(fd,argp) tcsetattr(fd,TCSANOW,argp)
56 struct termios initial_settings, new_settings;
58 void gotsig(int sig) {
59 stty(fileno(cin), &initial_settings);
60 fprintf(stdout, "\n");
67 #define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */
68 #define TERMINAL_HEIGHT 24
71 #if defined BB_FEATURE_AUTOWIDTH && ! defined USE_OLD_TERMIO
72 static int terminal_width = 0, terminal_height = 0;
74 #define terminal_width TERMINAL_WIDTH
75 #define terminal_height TERMINAL_HEIGHT
80 extern int more_main(int argc, char **argv)
82 int c, lines=0, input=0;
86 #ifdef BB_FEATURE_AUTOWIDTH
93 if ( argc > 0 && (strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0) ) {
101 file = fopen(*argv, "r");
107 fstat(fileno(file), &st);
109 #ifdef BB_FEATURE_USE_TERMIOS
110 cin = fopen("/dev/tty", "r");
112 cin = fopen("/dev/console", "r");
113 #ifdef USE_OLD_TERMIO
114 ioctl(fileno(cin),TCGETA,&initial_settings);
116 tcgetattr(fileno(cin),&initial_settings);
118 new_settings = initial_settings;
119 new_settings.c_lflag &= ~ICANON;
120 new_settings.c_lflag &= ~ECHO;
121 stty(fileno(cin), &new_settings);
123 #ifdef BB_FEATURE_AUTOWIDTH
124 ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
126 terminal_height = win.ws_row - 2;
128 terminal_width = win.ws_col - 1;
131 (void) signal(SIGINT, gotsig);
132 (void) signal(SIGQUIT, gotsig);
133 (void) signal(SIGTERM, gotsig);
136 while ((c = getc(file)) != EOF) {
141 len = fprintf(stdout, "--More-- ");
143 len += fprintf(stdout, "(%d%% of %ld bytes)",
144 (int) (100*( (double) ftell(file) / (double) st.st_size )),
147 len += fprintf(stdout, "%s",
148 #ifdef BB_FEATURE_USE_TERMIOS
158 #ifdef BB_FEATURE_USE_TERMIOS
159 /* Erase the "More" message */
162 while(++len <= terminal_width)
175 /* increment by just one line if we are at
176 * the end of this line*/
180 if ( ++lines == terminal_height )
189 } while (--argc > 0);
191 #ifdef BB_FEATURE_USE_TERMIOS