+# ifdef BB_FEATURE_AUTOWIDTH
+ ioctl(fileno(stdout), TIOCGWINSZ, &win);
+ if (win.ws_row > 4)
+ terminal_height = win.ws_row - 2;
+ if (win.ws_col > 0)
+ terminal_width = win.ws_col - 1;
+# endif
+
+ (void) signal(SIGINT, gotsig);
+ (void) signal(SIGQUIT, gotsig);
+ (void) signal(SIGTERM, gotsig);
+
+#endif
+ len=0;
+ lines = 0;
+ page_height = terminal_height;
+ please_display_more_prompt = 0;
+ while ((c = getc(file)) != EOF) {
+
+ if (please_display_more_prompt) {
+ len = printf("--More-- ");
+ if (file != stdin) {
+#if _FILE_OFFSET_BITS == 64
+ len += printf("(%d%% of %lld bytes)",
+ (int) (100 * ((double) ftell(file) /
+ (double) st.st_size)), (long long)st.st_size);
+#else
+ len += printf("(%d%% of %ld bytes)",
+ (int) (100 * ((double) ftell(file) /
+ (double) st.st_size)), (long)st.st_size);
+#endif
+ }
+ len += printf("%s",
+#ifdef BB_FEATURE_USE_TERMIOS
+ ""
+#else
+ "\n"
+#endif
+ );
+
+ fflush(stdout);
+
+ /*
+ * We've just displayed the "--More--" prompt, so now we need
+ * to get input from the user.
+ */
+#ifdef BB_FEATURE_USE_TERMIOS
+ input = getc(cin);
+#else
+ input = getc(stdin);
+#endif
+
+#ifdef BB_FEATURE_USE_TERMIOS
+ /* Erase the "More" message */
+ putc('\r', stdout);
+ while (--len >= 0)
+ putc(' ', stdout);
+ putc('\r', stdout);
+ fflush(stdout);
+#endif
+ len=0;
+ lines = 0;
+ page_height = terminal_height;
+ please_display_more_prompt = 0;
+ }
+
+ /*
+ * There are two input streams to worry about here:
+ *
+ * c : the character we are reading from the file being "mored"
+ * input : a character received from the keyboard
+ *
+ * If we hit a newline in the _file_ stream, we want to test and
+ * see if any characters have been hit in the _input_ stream. This
+ * allows the user to quit while in the middle of a file.
+ */
+ if (c == '\n') {
+ switch (input) {
+ case 'q':
+ goto end;
+ case '\n':
+ /* increment by just one line if we are at
+ * the end of this line*/
+ please_display_more_prompt = 1;
+ break;
+ }
+ /* Adjust the terminal height for any overlap, so that
+ * no lines get lost off the top. */
+ if (len >= terminal_width) {
+ div_t result = div( len, terminal_width);
+ if (result.quot) {
+ if (result.rem)
+ page_height-=result.quot;
+ else
+ page_height-=(result.quot-1);
+ }
+ }
+ if (++lines >= page_height) {
+ please_display_more_prompt = 1;
+ }
+ len=0;
+ }
+ /*
+ * If we just read a newline from the file being 'mored' and any
+ * key other than a return is hit, scroll by one page
+ */
+ putc(c, stdout);
+ len++;
+ }
+ fclose(file);
+ fflush(stdout);
+
+ argv++;
+ } while (--argc > 0);
+ end:
+#ifdef BB_FEATURE_USE_TERMIOS
+ gotsig(0);
+#endif
+ return(TRUE);
+}