Fixed up copyright notices and such
[platform/upstream/busybox.git] / util-linux / 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
45 #if defined (__sparc__)
46 #      define USE_OLD_TERMIO
47 #      include <termio.h>
48 #      include <sys/ioctl.h>
49 #      define termios termio
50 #      define stty(fd,argp) ioctl(fd,TCSETAF,argp)
51 #else
52 #      include <termios.h>
53 #      define stty(fd,argp) tcsetattr(fd,TCSANOW,argp)
54 #endif
55
56     FILE *cin;
57     struct termios initial_settings, new_settings;
58
59     void gotsig(int sig) { 
60             stty(fileno(cin), &initial_settings);
61             exit( TRUE);
62     }
63 #endif
64
65 extern int more_main(int argc, char **argv)
66 {
67     int c, lines=0, input=0;
68     int next_page=0;
69     struct stat st;     
70     FILE *file;
71
72     if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) {
73         usage (more_usage);
74     }
75     argc--;
76     argv++;
77
78     while (argc >= 0) {
79         if (argc==0) {
80             file = stdin;
81         }
82         else
83             file = fopen(*argv, "r");
84
85         if (file == NULL) {
86             perror(*argv);
87             exit(FALSE);
88         }
89         fstat(fileno(file), &st);
90
91 #ifdef BB_MORE_TERM
92         cin = fopen("/dev/tty", "r");
93         if (!cin)
94             cin = fopen("/dev/console", "r");
95 #ifdef USE_OLD_TERMIO
96         ioctl(fileno(cin),TCGETA,&initial_settings);
97 #else
98         tcgetattr(fileno(cin),&initial_settings);
99 #endif
100         new_settings = initial_settings;
101         new_settings.c_lflag &= ~ICANON;
102         new_settings.c_lflag &= ~ECHO;
103         stty(fileno(cin), &new_settings);
104         
105         (void) signal(SIGINT, gotsig);
106
107 #endif
108         while ((c = getc(file)) != EOF) {
109             if ( next_page ) {
110                 int len=0;
111                 next_page = 0;
112                 lines=0;
113                 len = fprintf(stdout, "--More-- ");
114                 if (file != stdin) {
115                     len += fprintf(stdout, "(%d%% of %ld bytes)", 
116                         (int) (100*( (double) ftell(file) / (double) st.st_size )),
117                         st.st_size);
118                 }
119                 len += fprintf(stdout, "%s",
120 #ifdef BB_MORE_TERM
121                         ""
122 #else
123                         "\n"
124 #endif
125                         );
126
127                 fflush(stdout);
128                 input = getc( cin);
129
130 #ifdef BB_MORE_TERM
131                 /* Erase the "More" message */
132                 while(len-- > 0)
133                     putc('\b', stdout);
134                 while(len++ < 79)
135                     putc(' ', stdout);
136                 while(len-- > 0)
137                     putc('\b', stdout);
138                 fflush(stdout);
139 #endif
140
141             }
142             if (input=='q')
143                 goto end;
144             if (input==' ' &&  c == '\n' )
145                 next_page = 1;
146             if ( c == '\n' && ++lines == 24 )
147                 next_page = 1;
148             putc(c, stdout);
149         }
150         fclose(file);
151         fflush(stdout);
152
153         argc--;
154         argv++;
155     }
156 end:
157 #ifdef BB_MORE_TERM
158     gotsig(0);
159 #endif  
160     exit(TRUE);
161 }
162