15 struct termios initial_settings, new_settings;
17 void gotsig(int sig) {
18 tcsetattr(fileno(cin), TCSANOW, &initial_settings);
23 const char more_usage[] = "more [file]\n"
25 "\tDisplays a file, one page at a time.\n"
26 "\tIf there are no arguments, the standard input is displayed.\n";
29 more_fn(const struct FileInfo * i)
33 int lines = 0, tlines = 0;
35 int rows = 24, cols = 79;
43 if (! (f = fopen(i->source, "r") )) {
44 name_and_error(i->source);
47 fstat(fileno(f), &st);
48 sizeb = st.st_size / 100;
52 cin = fopen("/dev/tty", "r");
53 tcgetattr(fileno(cin),&initial_settings);
54 new_settings = initial_settings;
55 new_settings.c_lflag &= ~ICANON;
56 new_settings.c_lflag &= ~ECHO;
57 tcsetattr(fileno(cin), TCSANOW, &new_settings);
59 (void) signal(SIGINT, gotsig);
61 ioctl(STDOUT_FILENO, TIOCGWINSZ, &win);
62 if (win.ws_row > 4) rows = win.ws_row - 2;
63 if (win.ws_col > 0) cols = win.ws_col - 1;
68 while ( (c = getc(f)) != EOF ) {
74 next_page = 0; //Percentage is based on bytes, not lines.
75 if ( i && i->source ) //It is not very acurate, but still useful.
76 len = printf("%s - %%%2ld - line: %d", i->source, (ftell(f) - sizeb - sizeb) / sizeb, tlines);
78 len = printf("line: %d", tlines);
85 fread(&garbage, 1, 1, cin);
86 } while ((garbage != ' ') && (garbage != '\n'));
88 if (garbage == '\n') {
93 //clear line, since tabs don't overwrite.
94 while(len-- > 0) putchar('\b');
95 while(len++ < cols) putchar(' ');
96 while(len-- > 0) putchar('\b');
101 if ( c == '\n' && ++lines == (rows + 1) )