zoukankan      html  css  js  c++  java
  • Linux---More命令 初级实现

    Linux: more
    已实现:more filename , quit不需要回车
    未实现:command | more 重定向 ,显示百分比
    Waiting。。。

     1 /*
     2 Linux: more
     3 已实现:more filename , quit不需要回车
     4 未实现:command | more 重定向 ,显示百分比 
     5 Waiting。。。
     6 */
     7 #include<stdio.h>
     8 #define PAGELEN 24
     9 #define LINELEN 512
    10 void do_more( FILE * );
    11 int see_more();
    12 int main( int ac,char *av[] ){
    13     FILE * fp;
    14     if( ac==1 )
    15         do_more( stdin );
    16     else{ 
    17         while( --ac )
    18             if( (fp=fopen(* ++av,"r"))!=NULL ){
    19                 do_more( fp );
    20                 fclose( fp );
    21             }
    22             else{ 
    23                 exit( 1 );
    24             }
    25     }
    26     return 0;
    27 }
    28     
    29 void do_more( FILE *fp ){
    30     char line[ LINELEN ];
    31     int num_of_lines = 0;
    32     int see_more(),reply;
    33     while( fgets( line,LINELEN,fp ) ){
    34         if( num_of_lines == PAGELEN ){
    35             reply = see_more();
    36             if( reply == 0 ){
    37                 break;
    38             }
    39             num_of_lines -= reply ;
    40         }
    41         if( fputs( line,stdout )==EOF ){
    42             exit( 1 );
    43         }/* 标准输入输出 */
    44         num_of_lines ++;
    45     }
    46 }
    47 
    48 int see_more(){
    49     int c;
    50     system ("stty -F /dev/tty cbreak");/*打开/dev/tty作为输入终端,并且控制属性为不需要回车*/ 
    51     printf("33[7m more?33[m");
    52     while( (c=getchar())!=EOF ){
    53         if( c=='q' ){
    54             return 0;
    55         }
    56         if( c==' ' ){
    57             return PAGELEN;
    58         }
    59         if( c=='
    ' ){
    60             return 1;
    61         }
    62     }
    63     system ("stty -F /dev/tty -cbreak");/*恢复终端属性为需要回车*/  
    64     return 0;
    65 }
    View Code
    keep moving...
  • 相关阅读:
    中文转数字
    半角全角互转
    sql快速查记录数
    杀进程批处理
    线程基本用法
    sql游标用法示例
    BUGFREE的使用
    SQL常用函数
    ASP.NET 2.0 下的验证码控件
    经典sql语句
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3360130.html
Copyright © 2011-2022 走看看