X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=more.c;h=732396f03341a918b974136b3911f1cb97b85c2b;hb=a283157c3832f151fa9f5abf5f18c56d05e5815d;hp=45b7c1274aa65f4f59f583c0ee5aa674dd937ad9;hpb=8a2e56c5dfc41f6946e36234eef4df559286db05;p=oweals%2Fbusybox.git diff --git a/more.c b/more.c index 45b7c1274..732396f03 100644 --- a/more.c +++ b/more.c @@ -25,11 +25,12 @@ * */ -#include "internal.h" #include #include #include +#include #include +#include "busybox.h" #define BB_DECLARE_EXTERN #define bb_need_help #include "messages.c" @@ -47,15 +48,15 @@ # define getTermSettings(fd,argp) tcgetattr(fd, argp); # endif -FILE *cin; +static FILE *cin; -struct termios initial_settings, new_settings; +static struct termios initial_settings, new_settings; -void gotsig(int sig) +static void gotsig(int sig) { setTermSettings(fileno(cin), &initial_settings); - fprintf(stdout, "\n"); - exit(TRUE); + putchar('\n'); + exit(EXIT_FAILURE); } #endif /* BB_FEATURE_USE_TERMIOS */ @@ -66,10 +67,11 @@ static int terminal_height = 24; extern int more_main(int argc, char **argv) { - int c, lines = 0, input = 0; - int please_display_more_prompt = 0; + int c, lines, input = 0; + int please_display_more_prompt; struct stat st; FILE *file; + int len, page_height; #if defined BB_FEATURE_AUTOWIDTH && defined BB_FEATURE_USE_TERMIOS struct winsize win = { 0, 0, 0, 0 }; @@ -82,18 +84,14 @@ extern int more_main(int argc, char **argv) if (argc == 0) { file = stdin; } else - file = fopen(*argv, "r"); + file = xfopen(*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"); + cin = xfopen("/dev/console", "r"); getTermSettings(fileno(cin), &initial_settings); new_settings = initial_settings; new_settings.c_cc[VMIN] = 1; @@ -115,25 +113,26 @@ extern int more_main(int argc, char **argv) (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) { - int len = 0; - please_display_more_prompt = 0; - lines = 0; - len = fprintf(stdout, "--More-- "); + if (please_display_more_prompt) { + len = printf("--More-- "); if (file != stdin) { #if _FILE_OFFSET_BITS == 64 - len += fprintf(stdout, "(%d%% of %lld bytes)", + len += printf("(%d%% of %lld bytes)", + (int) (100 * ((double) ftell(file) / + (double) st.st_size)), (long long)st.st_size); #else - len += fprintf(stdout, "(%d%% of %ld bytes)", + len += printf("(%d%% of %ld bytes)", + (int) (100 * ((double) ftell(file) / + (double) st.st_size)), (long)st.st_size); #endif - (int) (100 * - ((double) ftell(file) / - (double) st.st_size)), - st.st_size); } - len += fprintf(stdout, "%s", + len += printf("%s", #ifdef BB_FEATURE_USE_TERMIOS "" #else @@ -155,21 +154,22 @@ extern int more_main(int argc, char **argv) #ifdef BB_FEATURE_USE_TERMIOS /* Erase the "More" message */ + putc('\r', stdout); while (--len >= 0) - putc('\b', stdout); - while (++len <= terminal_width) putc(' ', stdout); - while (--len >= 0) - putc('\b', 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" + * 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 @@ -186,14 +186,28 @@ extern int more_main(int argc, char **argv) please_display_more_prompt = 1; break; } - if (++lines == terminal_height) + /* 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);