📄 mymore.cpp
字号:
/*mymore.cpp - my version of moreread and print 24 lines then pause for a few special command*/#include<stdio.h>#include<sys/stat.h>#include<termios.h>#define PAGELEN 17#define LINELEN 512int do_more(FILE *fp);int see_more(FILE *cmd);void set_cr_mode();void tty_mode(int how);int seesize;struct stat infobuf;struct termios original_mode;int main(int argc, char *argv[]) { FILE *fp; if (argc == 1) do_more(stdin); else { while (--argc) { if ((fp = fopen(* ++argv, "r")) != NULL) { tty_mode(0); set_cr_mode(); seesize = 0; stat(argv[0], &infobuf);//get size of file do_more(fp); tty_mode(1); fclose(fp); } else return(1); } } return(0);}int do_more(FILE *fp) { int i; char line[LINELEN]; int num_of_lines = 0; int reply; FILE *fp_tty; fp_tty = fopen("/dev/tty", "r"); if (fp_tty == NULL) return(1); while (fgets(line, LINELEN, fp)) { for (i = 0;i < LINELEN;i ++) { if (line[i] == '\0') { seesize += i; break; } } if (num_of_lines == PAGELEN) { reply = see_more(fp_tty); if (reply == 0) break; num_of_lines -= reply; } if (fputs(line, stdout) == EOF) return(1); num_of_lines ++; }}int see_more(FILE *cmd) { int c; printf("\033[7m--More--(%.0f%)\033[m", float(seesize)/float(infobuf.st_size) * 100); while ((c = getc(cmd)) != EOF) { if (c == 'q') return(0); if (c == ' ') return(PAGELEN); if (c == '\n') return(1); } return(0);}void set_cr_mode() {/*put file descriptor into char_by_char mode*/ struct termios ttystate; tcgetattr(0, &ttystate); ttystate.c_lflag &= ~ICANON; ttystate.c_cc[VMIN] = 1; tcsetattr(0, TCSANOW, &ttystate);}void tty_mode(int how) {/*save or set the mode of tty*/ if (how == 0) tcgetattr(0, &original_mode); else tcsetattr(0, TCSANOW, &original_mode);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -