linux more 命令的基础实现。
more :分页显示文件的内容
输入 more filename
会显示文件第一屏的内容,而且会有反白的 more 提升文件的百分比。空格=下一页,q=推出,h=连接帮助
more 的用法
1. more filename 如 :more filename.c
2. comand | more 如:ls -al / | more
3.more < filename 如:more < 1.txt
/* moreo1.c version 0.1 of more
* read and print 24 line then pause for a few special commands
*/
#include <stdio.h>
#define PAGELEN 24
#define LINELEN 512
void do_more(FILE *);
int see_more();
int main(int ac, char *av[])
{
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)
{
char line[LINELEN];
int num_of_lines = 0;
int see_more(),reply;
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)
exit(1);
num_of_lines++;
}
}
int see_more()
{
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;
}
////改进的more命令
/* moreo2.c version 0.1 of more
* read and print 24 line then pause for a few special commands
*/
#include <stdio.h>
#include <stdlib.h>
#define PAGELEN 24
#define LINELEN 512
void do_more(FILE *);
int see_more(FILE *);
int main(int ac, char *av[])
{
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)
{
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);
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)
{
if(c == 'q')
return 0;
if(c == ' ')
return PAGELEN;
if(c == '\n')
return 1;
}
return 0;
}
最新文章
- SqlServer禁用启用触发器、外键约束
- iOS的主要框架介绍
- solr6.0.0 + tomcat8 配置问题
- expdp远程导出数据
- C#复习⑦
- android:context,getApplicationContext()生命周期
- navigationView 的使用和布局文件的绑定
- uC/OS-II队列(OS_q)块
- SQL高级查询技巧(两次JOIN同一个表,自包含JOIN,不等JOIN)
- C#设计模式(22)——访问者模式(Vistor Pattern)
- CC254x(cc2540/cc2541)的微信AirSync调试笔记
- MFC中的CDC,CClientDC,CPaintDC,CWindowDC的区别
- *[hackerrank]Algorithmic Crush
- JAXB - Annotations, Annotations for the Schema: XmlSchema
- 大数据时代的 9 大Key-Value存储数据库
- sort函数的用法与实验
- JNDI 使用
- Visual Studio Enterprise 2019序列号
- 元注解——java.lang.annotation.Target(1.8)
- dedecms 模版里格式化时间标签
热门文章
- [gym102822I]Invaluable Assets
- [cf1285F]Classical
- [bzoj1109]堆积木
- [JSC2021 A~D + F]
- 洛谷 P6144 - [USACO20FEB]Help Yourself P(二项式定理+线段树)
- JSOI2021 游记
- 全基因组选择育种(GS)简介
- seqtk抽取测序数据
- 「Spark从精通到重新入门(一)」Spark 中不可不知的动态优化
- 实时数仓(二):DWD层-数据处理