void do_more(FILE *fp) { char line[LINELEN]; char toPrint[PRINT]; int num_of_lines = 0; int pagePercent; int reply; int linesDisplayed = 0; FILE *fp_tty; fp_tty = fopen("/dev/tty", "r"); if(fp_tty == NULL) ctrl_c(); while(fgets(line, LINELEN, fp)) { if(num_of_lines == PAGELEN){ pagePercent = (linesDisplayed * PERCENT) / numLines; if(linesDisplayed == PAGELEN){ sprintf(toPrint, "%s %%%d", fileName, pagePercent); reply = see_more(fp_tty, toPrint); } if(linesDisplayed > PAGELEN){ sprintf(toPrint, "%%%d", pagePercent); reply = see_more(fp_tty, toPrint); } if(reply == 0) break; num_of_lines -= reply; } if(fputs(line, stdout) == EOF) ctrl_c(); num_of_lines++; linesDisplayed++; } }
void do_more( FILE *fp ) /* * read PAGELEN lines, then call see_more() for further instructions */ { char line[LINELEN]; int num_of_lines = 0; int reply; FILE *fp_tty; fp_tty = fopen( "/dev/tty", "r" ); /* NEW: cmd stream */ if ( fp_tty == NULL ) /* if open fails */ exit(1); /* no use in running */ while ( fgets( line, LINELEN, fp ) ){ /* more input */ if ( num_of_lines == PAGELEN ) { /* full screen? */ reply = see_more(fp_tty); /* NEW: pass FILE * */ if ( reply == 0 ) /* n: done */ break; num_of_lines -= reply; /* reset count */ } if ( fputs( line, stdout ) == EOF ) /* show line */ exit(1); /* or die */ num_of_lines++; /* count it */ } }
/* * read PAGELEN lines, then call see_more() for further instructions */ void do_more(FILE *fp) { char line[LINELEN]; int num_of_lines = 0; int reply; FILE *fp_tty; fp_tty = fopen("/dev/tty", "r"); /* cmd stream */ if (fp_tty == NULL) { perror("fopen() /dev/tty failed:"); exit(EXIT_FAILURE); } while (fgets(line, LINELEN, fp)) { if (num_of_lines == PAGELEN) { /* full screen? */ reply = see_more(fp_tty); /* y: ask user */ if (reply == 0) /* n: done */ break; num_of_lines -= reply; /* reset count */ } if (fputs(line, stdout) == EOF) /* show line */ exit(EXIT_FAILURE); /* or die */ num_of_lines++; /* count it */ } }
void do_more(FILE *fp){ char line[COL]; int num_of_lines = 0; int see_more(FILE *),reply; FILE * fp_tty; fp_tty = fopen("/dev/tty","r"); if (fp_tty == NULL) exit(1); while(fgets(line,COL, fp)){ if (num_of_lines == ROW){ reply = see_more(fp_tty); if (reply == 0) { printf("\n"); break; } num_of_lines -= reply; }//if MOVE_TO_DOWN_LEFT_CORNER(); CLEAN_LINE(); if (fputs(line, stdout) == EOF) exit(1); num_of_lines++; }//while }
void do_more(FILE *fp) /* * read PAGELEN lines, then call see_more() for further instructions */ { char line[LINELEN]; int num_of_lines = 0; int see_more(), reply; while (fgets(line, LINELEN, fp)) { /* more input */ if (num_of_lines == PAGELEN) { /* full screen? 第一次进入时,显然不会执行了。 */ reply = see_more(); /* y: ask user */ if (reply == 0) /* n: done */ break; num_of_lines -= reply; /* reset count */ // <--- 如果让你设计这个计数量 num_of_lines ,你会怎样做呢? // 我感觉这里的做法很好,num_of_lines 相当于一个游标,其满盈由 reply 变量 // 进行控制。 } if (fputs(line, stdout) == EOF) /* show line */ exit(1); /* or die */ num_of_lines++; /* count it */ // <--- 看代码的方法,比如这里 num_of_lines 在 52 行看到时,可能还有些纳闷,因为在此之前没有地方 // 会改变它的值。不要怀疑基本的逻辑,它一定会在后面的某个部位被修改,否则上面的这个条件永远无法 // 进入。 } }
/* read PAGELEN lines */ void do_more(FILE *fp) { char line[LINELEN]; int num_of_lines = 0; int see_more(FILE *), reply; FILE *fp_tty; fp_tty = fopen("/dev/tty","r"); if (fp_tty == NULL) exit(1); struct termios org_opts, new_opts; tcgetattr(STDIN_FILENO, &org_opts); memcpy(&new_opts, &org_opts, sizeof(new_opts)); new_opts.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); while (fgets(line, LINELEN, fp)) { if (num_of_lines == PAGELEN) { reply = see_more(fp_tty); // user want some more if (reply == 0) break; num_of_lines -= reply; } if (fputs(line, stdout) == EOF) exit(1); num_of_lines++; } tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); }
void do_more(FILE* fp) { char line[ LINELEN ]; int num_of_lines = 0; int reply; while ( fgets( line, LINELEN, fp ) ) { if ( num_of_lines == PAGLINE ) { reply = see_more(); if ( reply == 0 ) break; num_of_lines -= reply; } if ( fputs( line, stdout ) == EOF ) { exit( 1 ); } num_of_lines++; } }
void do_more(FILE* fp) { char line[MAXLINELEN]; int lineNum, reply; FILE* fp_tty; fp_tty = fopen("/dev/tty", "r"); if (fp_tty == NULL) { exit(1); } lineNum = 0; while((fgets(line, MAXLINELEN, fp)) != NULL) { if (lineNum == PAGELEN - 1) { reply = see_more(fp_tty); if (reply == -1) { return; } lineNum -= reply; } fputs(line, stdout); ++lineNum; } }
void do_more(FILE *fp) /* 读取PAGELEN行的文本,接着调用see_more()函数进行下一步的指令 */ { char line[LINELEN]; int num_of_lines = 0; int see_more(FILE *),reply; FILE *fp_tty; fp_tty = fopen("/dev/tty","r"); //NEW:cmd流 if(fp_tty == NULL) //如果打开失败 exit(1); while(fgets(line,LINELEN,fp)) //more输出 { if(num_of_lines == PAGELEN) { reply = see_more(fp_tty); //NEW:传递FILE* if(reply == 0) //完成 break; num_of_lines -= reply; //重新计数 } if(fputs(line,stdout) == EOF) //显示一行 exit(1); //或退程序 num_of_lines++; //计数 } }
/* * read PAGELEN lines, then call see_more() for further instructions */ void do_more(FILE *fp) { char line[LINELEN]; int num_of_lines = 0; int see_more(), reply; while ( fgets( line, LINELEN, fp) ) { /* more input */ if ( num_of_lines == PAGELEN ) { /*full screen? */ reply = see_more(); /*y: ask user */ if (reply == 0 ) /*n: done */ break; num_of_lines -= reply; /* reset count */ } if ( fputs( line, stdout ) == EOF ) /* show line */ exit(1); /* or die */ num_of_lines++; /* count it */ } }
void do_more(FILE * fp) { char line[LINELEN]; int reply; int num_of_lines = 0; FILE * fp_tty ; struct winsize size; struct termios term,save_term; struct stat buf; if((fp_tty = fopen("/dev/tty","r")) == NULL) exit(1); /*获取终端屏幕大小*/ if(ioctl(fileno(fp_tty),TIOCGWINSZ,&size) == -1) perror("get term size failed \n"); PAGELEN = size.ws_row; /*获取文件大小*/ if(fstat(fileno(fp),&buf) < 0) perror("get file stat failed\n"); file_size = buf.st_size; /* 改变终端的属性为无回显按下q 或 ' ' 无需再按Enter即可执行*/ if(tcgetattr(fileno(fp_tty),&term) < 0) perror("tcgetattr failed\n"); save_term = term; term.c_lflag &= ~( ECHO | ICANON); if(tcsetattr(fileno(fp_tty),TCSANOW,&term) < 0) perror("tcsetattr failed\n"); while(fgets(line,LINELEN,fp)) { file_pos = ftello(fp); //得到当前位置 if(num_of_lines == PAGELEN) { reply = see_more(fp_tty); if(reply == 0) { break; } num_of_lines -= reply; } if(fputs(line,stdout) == EOF) exit(1); num_of_lines++; } if(tcsetattr(fileno(fp_tty),TCSANOW,&save_term) < 0) //设置终端属性 perror("restore from save_term failed \n"); }
void do_more(FILE *fp){ char line[LINELEN]; int num_of_lines = 0; int reply; FILE *fp_tty = fopen("/dev/tty", "r"); if(fp_tty == NULL) exit(1); while(fgets(line, LINELEN, fp)){ /* read in one line */ if(num_of_lines == PAGELEN){ /* if the screen is full */ reply = see_more(fp_tty); /* ask user */ if(reply == 0) break; num_of_lines -= reply; } if(fputs(line, stdout) == EOF) /* show one line */ exit(1); num_of_lines++; } }
// 先打印行数:当行数达到 PAGELEN 时调用 see_more 函数处理命令;当输出到 stdout // 时发现 EOF, 自然终止。 extern void do_more(FILE *fp, FILE *fp_tty) { char line[LINELEN]; int num_of_lines = 0; int reply; while (fgets(line, LINELEN, fp)) { if (num_of_lines == PAGELEN) { reply = see_more(fp_tty); if (reply == 0) { exit(EXIT_SUCCESS); } num_of_lines -= reply; } if (fputs(line, stdout) == EOF) { printf("No more lines.\n"); exit(EXIT_SUCCESS); } ++num_of_lines; } }
void b_more( FILE * fp /*The file to print*/ ) { char line[LINELEN]; /* Text Buffer */ int num_of_lines = 0; /* Count of the lines printed*/ int reply; /* The reply of user*/ while (fgets(line, LINELEN, fp)) { if (num_of_lines == PAGELEN) { reply = see_more(); if (reply == 0) break; num_of_lines -= reply; } if (fputs(line, stdout) == EOF) return; num_of_lines++; } }
/* * read 24 lines, then call see_more() for further instructions */ void do_more(FILE* fp){ char line[512]; int num_of_lines = 0; //int see_more(); int reply; FILE* fp_tty = fopen("/dev/tty","r"); if(fp_tty == NULL) exit(1); while (fgets(line,512,fp)){ /*more input*/ if(num_of_lines == 24) { /*full screen? 此时才接收用户的输入*/ reply = see_more(fp_tty); /*ask user*/ if (reply == 0) /*done*/ break; num_of_lines -= reply; /*reset count*/ } if(fputs(line,stdout) == EOF){ /*show line*/ exit(1); } num_of_lines++; } }
void do_more(FILE * fp) { char line[LINELEN]; int num_of_lines = 0; int see_more(FILE*), reply; FILE *fp_tty; fp_tty = fopen("/dev/tty", "r"); if ( fp_tty == NULL ) exit(EXIT_FAILURE); while ( fgets(line, LINELEN, fp) ) { if (num_of_lines == PAGELEN) { reply = see_more(fp_tty); if (reply == 0) break; num_of_lines -= reply; } if (fputs(line, stdout) == EOF) exit(EXIT_FAILURE); num_of_lines++; } }
void do_more(FILE *fp){ char line[LINELEN]; int num_of_line=0; int see_more(FILE*),reply; FILE* tty = fopen("/dev/tty","r"); if(tty==NULL){ exit(1); } while(fgets(line,LINELEN,fp)){ if(num_of_line==PAGELEN){ reply=see_more(tty); if(reply == 0) break; num_of_line-=reply; } if(fputs(line,stdout)==EOF) exit(1); num_of_line++; } fclose(tty); }