ea5e22562165b1b78cf4644c6cf6bedebab7defd
[oweals/busybox.git] / more.c
1 /*
2  * Mini more implementation for busybox
3  *
4  *
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 
8  * boot-floppies team.
9  *
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.
14  *
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.
19  *
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
23  *
24  */
25
26
27 /* Turning this off makes things a bit smaller (and less pretty) */
28 #define BB_MORE_TERM
29
30
31
32 #include "internal.h"
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <signal.h>
36
37
38 static const char more_usage[] = "[file ...]";
39
40
41 /* ED: sparc termios is broken: revert back to old termio handling. */
42 #ifdef BB_MORE_TERM
43
44 #if #cpu(sparc)
45 #      define USE_OLD_TERMIO
46 #      include <termio.h>
47 #      include <sys/ioctl.h>
48 #      define termios termio
49 #      define stty(fd,argp) ioctl(fd,TCSETAF,argp)
50 #else
51 #      include <termios.h>
52 #      define stty(fd,argp) tcsetattr(fd,TCSANOW,argp)
53 #endif
54
55     FILE *cin;
56     struct termios initial_settings, new_settings;
57
58     void gotsig(int sig) { 
59             stty(fileno(cin), &initial_settings);
60             exit( TRUE);
61     }
62 #endif
63
64 extern int more_main(int argc, char **argv)
65 {
66     int c, lines=0, input=0;
67     int next_page=0;
68     struct stat st;     
69     FILE *file;
70
71     argc--;
72     argv++;
73
74     if ( argc > 0 && (strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0) ) {
75         usage (more_usage);
76     }
77     do {
78         if (argc==0) {
79             file = stdin;
80         }
81         else
82             file = fopen(*argv, "r");
83
84         if (file == NULL) {
85             perror(*argv);
86             exit(FALSE);
87         }
88         fstat(fileno(file), &st);
89
90 #ifdef BB_MORE_TERM
91         cin = fopen("/dev/tty", "r");
92         if (!cin)
93             cin = fopen("/dev/console", "r");
94 #ifdef USE_OLD_TERMIO
95         ioctl(fileno(cin),TCGETA,&initial_settings);
96 #else
97         tcgetattr(fileno(cin),&initial_settings);
98 #endif
99         new_settings = initial_settings;
100         new_settings.c_lflag &= ~ICANON;
101         new_settings.c_lflag &= ~ECHO;
102         stty(fileno(cin), &new_settings);
103         
104         (void) signal(SIGINT, gotsig);
105         (void) signal(SIGQUIT, gotsig);
106         (void) signal(SIGTERM, gotsig);
107
108
109 #endif
110         while ((c = getc(file)) != EOF) {
111             if ( next_page ) {
112                 int len=0;
113                 next_page = 0;
114                 lines=0;
115                 len = fprintf(stdout, "--More-- ");
116                 if (file != stdin) {
117                     len += fprintf(stdout, "(%d%% of %ld bytes)", 
118                         (int) (100*( (double) ftell(file) / (double) st.st_size )),
119                         st.st_size);
120                 }
121                 len += fprintf(stdout, "%s",
122 #ifdef BB_MORE_TERM
123                         ""
124 #else
125                         "\n"
126 #endif
127                         );
128
129                 fflush(stdout);
130                 input = getc( cin);
131
132 #ifdef BB_MORE_TERM
133                 /* Erase the "More" message */
134                 while(len-- > 0)
135                     putc('\b', stdout);
136                 while(len++ < 79)
137                     putc(' ', stdout);
138                 while(len-- > 0)
139                     putc('\b', stdout);
140                 fflush(stdout);
141 #endif
142
143             }
144             if (input=='q')
145                 goto end;
146             if (input=='\n' &&  c == '\n' )
147                 next_page = 1;
148             if ( c == ' ' && ++lines == 24 )
149                 next_page = 1;
150             putc(c, stdout);
151         }
152         fclose(file);
153         fflush(stdout);
154
155         argv++;
156     } while (--argc > 0);
157 end:
158 #ifdef BB_MORE_TERM
159     gotsig(0);
160 #endif  
161     exit(TRUE);
162 }
163