more命令编写

#include <stdio.h>
#include <stdlib.h>
#define PAGELEN 24
#define LINELEN 512
void do_more(FILE *);
int see_more();
int main( int ac, char* av[])//ac代表参数的个数,默认为0个 ac[0]是该进程的名字
{
        FILE *fp;
        if (ac == 1)  //两个参数,表示是正确的
                do_more(stdin);
        else
                while(--ac) //否则就打开第一个
                if((fp = fopen(* ++ av,"r"))!=NULL)
                {
                        do_more(fp);
                        fclose(fp);
                }
                else
                        exit(1);
                return 0;
}
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;
        FILE * fp_tty;
        fp_tty = fopen("/dev/tty","r");         //从键盘接到数据
        if (fp_tty == NULL)
            exit(1);
        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(1);
                num_of_lines++;
        }
}
int see_more()
/*
 * *print message,wait for response,return # of lines to advance
 * *q means no,space means yes,CR means one line
 * */
{
        int c;
        printf("\033[7m more?\033[m");
        while((c=getchar())!=EOF)
        {
                if(c=='q')
                        return 0;
                if(c==' ')
                        return PAGELEN;
                if(c=='\n')
                        return 1;
        }
        return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容