Simple implementation of the more commands under Unix:
#include <stdio.h> #define Pagelen 24#define linelen 512int do_more (file * file); int see_more (file * file); int main (i NT Argc,char * argv[]) {FILE * fp;if (argc = = 1) do_more (stdin); Else{int argv_index = 1;while (argc > 1) {if (fp = fopen (arg V[argv_index], "r")) = = NULL) exit (1);d O_more (FP); fclose (FP); argv_index++;argc--;}} return 0;} int Do_more (FILE * fp) {char Buffer[linelen];int number_of_lines = 0;int reply; FILE * Ftty;ftty = fopen ("/dev/tty", "R"), if (Ftty = = NULL) exit (1), while (Fgets (BUFFER,LINELEN,FP)) {if (Number_of_lines = = Pagelen) {fputs (buffer,stdout); reply = See_more (Ftty); if (reply = = 0) break;if (reply = = 1) number_of_lines = 0;//if (reply = = 2)//number_of_lines--;} Else{number_of_lines++;fputs (Buffer,stdout);}}} int See_more (FILE * fp) {char c;printf ("Enter your command (q space or enter):"), while ((c = getc (stdin))! = EOF) {if (c = = ' Q ') //Press Q to exit return 0;if (c = = ") return 1;//Press space to display the next page if (c = = ' n ') return 2;//press N to display the next line}return 3;}
Write your own shell command more