zoukankan      html  css  js  c++  java
  • 【Linux开发习作】more命令的编写(1)

     作者:gnuhpc 
    出处:http://www.cnblogs.com/gnuhpc/

    /*
    * =====================================================================================
    *
    * Filename: more01.c
    *
    * Description: A User Version of Command more
    *
    * Version: 1.0
    * Created: 12/03/2008 06:36:56 PM
    * Revision: none
    * Compiler: gcc
    *
    * Author: Futuredaemon (BUPT),gnuhpc@gmail.com
    * Company: BUPT_UNITED
    *
    * =====================================================================================
    */
    
    #include <stdlib.h>
    #include <stdio.h>
    
    
    #define PAGELEN 24 /*The number of pape to be printed */
    #define LINELEN 512 /*The length of Line to be printed */
    
    void do_more(FILE *);
    int see_more(FILE *);
    
    int main ( int argc, char *argv[] )
    {
    FILE *fp; /* File Descriptor */
    if ( argc ==1 ) /* If no files exist,use the keyboard */
    do_more(stdin);
    else
    while(--argc)
    if((fp=fopen(*++argv,"r"))!=NULL)
    {
    do_more(fp);
    fclose(fp);
    }
    else
    exit(1);
    return EXIT_SUCCESS;
    } /* ---------- end of function main ---------- */
    
    void do_more(FILE *fp)
    {
    char line[LINELEN];
    int num_of_lines=0;
    int 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(FILE *cmd)
    {
    int c;
    printf("/033[7m more? /033[m");
    while((c=getc(cmd))!=EOF)
    {
    //printf("This is a test!");
    if(c == 'q')
    return 0;
    if(c == ' ')
    return PAGELEN;
    if(c == '/n')
    return 1;
    }
    return 0;
    }


                   作者:gnuhpc
                   出处:http://www.cnblogs.com/gnuhpc/
                   除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    TCP/IP协议栈与数据包封装+TCP与UDP区别
    MySQL数据库优化总结
    MySQL存储引擎,锁,优化简述
    java实现常见查找算法
    Java的几种常见排序算法
    UML类图学习
    高性能的RTC服务器OpenFire
    常用的设计模式
    Swing JInternalFrame的使用
    Qt 模态与非模态
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2321355.html
Copyright © 2011-2022 走看看