X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=more.c;h=f8421490588ad1c4a43727c1d6ba5d9ef7de6231;hb=0a64de9b4f7b3254008affe21b3ad4174c26f7f5;hp=72d58a6f4c1551204d15661cae7980c1e75b937e;hpb=ef8b6c757de9684f5d88eff4b014527e87121137;p=oweals%2Fbusybox.git diff --git a/more.c b/more.c index 72d58a6f4..f84214905 100644 --- a/more.c +++ b/more.c @@ -1,7 +1,13 @@ +/* vi: set sw=4 ts=4: */ /* * Mini more implementation for busybox * - * Copyright (C) 1998 by Erik Andersen + * + * Copyright (C) 1995, 1996 by Bruce Perens . + * + * Latest version blended together by Erik Andersen , + * based on the original more implementation by Bruce, and code from the + * Debian boot-floppies team. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,140 +25,158 @@ * */ - -/* Turning this off makes things a bit smaller (and less pretty) */ -#define BB_MORE_TERM - - - #include "internal.h" #include #include #include +#include +static const char more_usage[] = "more [file ...]\n"; -static const char more_usage[] = "[file ...]"; +#ifdef BB_FEATURE_USE_TERMIOS +#include -/* ED: sparc termios is broken: revert back to old termio handling. */ -#ifdef BB_MORE_TERM +FILE *cin; +/* sparc and other have broken termios support: use old termio handling. */ +struct termio initial_settings, new_settings; +void gotsig(int sig) +{ + ioctl(fileno(cin), TCSETAF, &initial_settings); + fprintf(stdout, "\n"); + exit(TRUE); +} +#endif -#if defined (__sparc__) -# define USE_OLD_TERMIO -# include -# include -# define termios termio -# define stty(fd,argp) ioctl(fd,TCSETAF,argp) + + +#define TERMINAL_WIDTH 79 /* not 80 in case terminal has linefold bug */ +#define TERMINAL_HEIGHT 24 + + +#if defined BB_FEATURE_AUTOWIDTH +static int terminal_width = 0, terminal_height = 0; #else -# include -# define stty(fd,argp) tcsetattr(fd,TCSANOW,argp) +#define terminal_width TERMINAL_WIDTH +#define terminal_height TERMINAL_HEIGHT #endif - FILE *cin; - struct termios initial_settings, new_settings; - void gotsig(int sig) { - stty(fileno(cin), &initial_settings); - exit( TRUE); - } -#endif extern int more_main(int argc, char **argv) { - int c, lines=0, input=0; - int next_page=0; - struct stat st; - FILE *file; - - if ( strcmp(*argv,"--help")==0 || strcmp(*argv,"-h")==0 ) { - usage (more_usage); - } - argc--; - argv++; - - while (argc >= 0) { - if (argc==0) { - file = stdin; - } - else - file = fopen(*argv, "r"); + int c, lines = 0, input = 0; + int next_page = 0; + struct stat st; + FILE *file; + +#ifdef BB_FEATURE_AUTOWIDTH + struct winsize win = { 0, 0 }; +#endif - if (file == NULL) { - perror(*argv); - exit(FALSE); + argc--; + argv++; + + if (argc > 0 + && (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0)) { + usage(more_usage); } - fstat(fileno(file), &st); - -#ifdef BB_MORE_TERM - cin = fopen("/dev/tty", "r"); - if (!cin) - cin = fopen("/dev/console", "r"); -#ifdef USE_OLD_TERMIO - ioctl(fileno(cin),TCGETA,&initial_settings); -#else - tcgetattr(fileno(cin),&initial_settings); + do { + if (argc == 0) { + file = stdin; + } else + file = fopen(*argv, "r"); + + if (file == NULL) { + perror(*argv); + exit(FALSE); + } + fstat(fileno(file), &st); + +#ifdef BB_FEATURE_USE_TERMIOS + cin = fopen("/dev/tty", "r"); + if (!cin) + cin = fopen("/dev/console", "r"); + ioctl(fileno(cin), TCGETA, &initial_settings); + new_settings = initial_settings; + new_settings.c_lflag &= ~ICANON; + new_settings.c_lflag &= ~ECHO; + ioctl(fileno(cin), TCSETAF, &new_settings); + +#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 - new_settings = initial_settings; - new_settings.c_lflag &= ~ICANON; - new_settings.c_lflag &= ~ECHO; - stty(fileno(cin), &new_settings); - - (void) signal(SIGINT, gotsig); + + (void) signal(SIGINT, gotsig); + (void) signal(SIGQUIT, gotsig); + (void) signal(SIGTERM, gotsig); #endif - while ((c = getc(file)) != EOF) { - if ( next_page ) { - int len=0; - next_page = 0; - lines=0; - len = fprintf(stdout, "--More-- "); - if (file != stdin) { - len += fprintf(stdout, "(%d%% of %ld bytes)", - (int) (100*( (double) ftell(file) / (double) st.st_size )), - st.st_size); - } - len += fprintf(stdout, "%s", -#ifdef BB_MORE_TERM - "" + while ((c = getc(file)) != EOF) { + if (next_page) { + int len = 0; + + next_page = 0; + lines = 0; + len = fprintf(stdout, "--More-- "); + if (file != stdin) { + len += fprintf(stdout, "(%d%% of %ld bytes)", + (int) (100 * + ((double) ftell(file) / + (double) st.st_size)), + st.st_size); + } + len += fprintf(stdout, "%s", +#ifdef BB_FEATURE_USE_TERMIOS + "" #else - "\n" + "\n" #endif - ); - - fflush(stdout); - input = getc( cin); - -#ifdef BB_MORE_TERM - /* Erase the "More" message */ - while(len-- > 0) - putc('\b', stdout); - while(len++ < 79) - putc(' ', stdout); - while(len-- > 0) - putc('\b', stdout); - fflush(stdout); + ); + + fflush(stdout); + input = getc(cin); + +#ifdef BB_FEATURE_USE_TERMIOS + /* Erase the "More" message */ + while (--len >= 0) + putc('\b', stdout); + while (++len <= terminal_width) + putc(' ', stdout); + while (--len >= 0) + putc('\b', stdout); + fflush(stdout); #endif - } - if (input=='q') - goto end; - if (input==' ' && c == '\n' ) - next_page = 1; - if ( c == '\n' && ++lines == 24 ) - next_page = 1; - putc(c, stdout); - } - fclose(file); - fflush(stdout); + } + 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*/ + next_page = 1; + break; + } + if (++lines == terminal_height) + next_page = 1; + } + putc(c, stdout); + } + fclose(file); + fflush(stdout); - argc--; - argv++; - } -end: -#ifdef BB_MORE_TERM - gotsig(0); -#endif - exit(TRUE); + argv++; + } while (--argc > 0); + end: +#ifdef BB_FEATURE_USE_TERMIOS + gotsig(0); +#endif + exit(TRUE); } -