Fix a silly off-by-one error noticed by Santiago Garcia Mantinan <manty@debian.org>
[oweals/busybox.git] / more.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini more implementation for busybox
4  *
5  *
6  * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7  *
8  * Latest version blended together by Erik Andersen <andersen@lineo.com>,
9  * based on the original more implementation by Bruce, and code from the 
10  * Debian boot-floppies team.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  */
27
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <sys/ioctl.h>
33 #include "busybox.h"
34
35 /* ED: sparc termios is broken: revert back to old termio handling. */
36 #ifdef BB_FEATURE_USE_TERMIOS
37 #       if #cpu(sparc)
38 #               include <termio.h>
39 #               define termios termio
40 #               define setTermSettings(fd,argp) ioctl(fd,TCSETAF,argp)
41 #               define getTermSettings(fd,argp) ioctl(fd,TCGETA,argp)
42 #       else
43 #               include <termios.h>
44 #               define setTermSettings(fd,argp) tcsetattr(fd,TCSANOW,argp)
45 #               define getTermSettings(fd,argp) tcgetattr(fd, argp);
46 #       endif
47
48 static FILE *cin;
49
50 static struct termios initial_settings, new_settings;
51
52 static void gotsig(int sig)
53 {
54         setTermSettings(fileno(cin), &initial_settings);
55         putchar('\n');
56         exit(EXIT_FAILURE);
57 }
58 #endif /* BB_FEATURE_USE_TERMIOS */
59
60
61 static int terminal_width = 79; /* not 80 in case terminal has linefold bug */
62 static int terminal_height = 24;
63
64
65 extern int more_main(int argc, char **argv)
66 {
67         int c, lines, input = 0;
68         int please_display_more_prompt;
69         struct stat st;
70         FILE *file;
71         int len, page_height;
72
73 #if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS
74         struct winsize win = { 0, 0, 0, 0 };
75 #endif
76
77         argc--;
78         argv++;
79
80         do {
81                 if (argc == 0) {
82                         file = stdin;
83                 } else
84                         file = xfopen(*argv, "r");
85
86                 fstat(fileno(file), &st);
87
88 #ifdef BB_FEATURE_USE_TERMIOS
89                 cin = fopen("/dev/tty", "r");
90                 if (!cin)
91                         cin = xfopen("/dev/console", "r");
92                 getTermSettings(fileno(cin), &initial_settings);
93                 new_settings = initial_settings;
94                 new_settings.c_cc[VMIN] = 1;
95                 new_settings.c_cc[VTIME] = 0;
96                 new_settings.c_lflag &= ~ICANON;
97                 new_settings.c_lflag &= ~ECHO;
98                 setTermSettings(fileno(cin), &new_settings);
99
100 #       ifdef BB_FEATURE_AUTOWIDTH
101                 ioctl(fileno(stdout), TIOCGWINSZ, &win);
102                 if (win.ws_row > 4)
103                         terminal_height = win.ws_row - 2;
104                 if (win.ws_col > 0)
105                         terminal_width = win.ws_col - 1;
106 #       endif
107
108                 (void) signal(SIGINT, gotsig);
109                 (void) signal(SIGQUIT, gotsig);
110                 (void) signal(SIGTERM, gotsig);
111
112 #endif
113                 len=0;
114                 lines = 0;
115                 page_height = terminal_height;
116                 please_display_more_prompt = 0;
117                 while ((c = getc(file)) != EOF) {
118
119                         if (please_display_more_prompt) {
120                                 len = printf("--More-- ");
121                                 if (file != stdin) {
122 #if _FILE_OFFSET_BITS == 64
123                                         len += printf("(%d%% of %lld bytes)",
124                                                    (int) (100 * ((double) ftell(file) /
125                                                    (double) st.st_size)), (long long)st.st_size);
126 #else
127                                         len += printf("(%d%% of %ld bytes)",
128                                                    (int) (100 * ((double) ftell(file) /
129                                                    (double) st.st_size)), (long)st.st_size);
130 #endif
131                                 }
132                                 len += printf("%s",
133 #ifdef BB_FEATURE_USE_TERMIOS
134                                                            ""
135 #else
136                                                            "\n"
137 #endif
138                                         );
139
140                                 fflush(stdout);
141
142                                 /*
143                                  * We've just displayed the "--More--" prompt, so now we need
144                                  * to get input from the user.
145                                  */
146 #ifdef BB_FEATURE_USE_TERMIOS
147                                 input = getc(cin);
148 #else
149                                 input = getc(stdin);
150 #endif
151
152 #ifdef BB_FEATURE_USE_TERMIOS
153                                 /* Erase the "More" message */
154                                 putc('\r', stdout);
155                                 while (--len >= 0)
156                                         putc(' ', stdout);
157                                 putc('\r', stdout);
158                                 fflush(stdout);
159 #endif
160                                 len=0;
161                                 lines = 0;
162                                 page_height = terminal_height;
163                                 please_display_more_prompt = 0;
164
165                                 if (input == 'q')
166                                         goto end;
167                         }
168
169                         /* 
170                          * There are two input streams to worry about here:
171                          *
172                          * c     : the character we are reading from the file being "mored"
173                          * input : a character received from the keyboard
174                          *
175                          * If we hit a newline in the _file_ stream, we want to test and
176                          * see if any characters have been hit in the _input_ stream. This
177                          * allows the user to quit while in the middle of a file.
178                          */
179                         if (c == '\n') {
180                                 /* increment by just one line if we are at
181                                  * the end of this line */
182                                 if (input == '\n')
183                                         please_display_more_prompt = 1;
184                                 /* Adjust the terminal height for any overlap, so that
185                                  * no lines get lost off the top. */
186                                 if (len >= terminal_width) {
187                                         div_t result = div( len, terminal_width); 
188                                         if (result.quot) {
189                                                 if (result.rem)
190                                                         page_height-=result.quot;
191                                                 else
192                                                         page_height-=(result.quot-1);
193                                         }
194                                 }
195                                 if (++lines >= page_height) {
196                                         please_display_more_prompt = 1;
197                                 }
198                                 len=0;
199                         }
200                         /*
201                          * If we just read a newline from the file being 'mored' and any
202                          * key other than a return is hit, scroll by one page
203                          */
204                         putc(c, stdout);
205                         len++;
206                 }
207                 fclose(file);
208                 fflush(stdout);
209
210                 argv++;
211         } while (--argc > 0);
212   end:
213 #ifdef BB_FEATURE_USE_TERMIOS
214         setTermSettings(fileno(cin), &initial_settings);
215 #endif
216         return 0;
217 }