功能不完善
# include <stdio.h>
# include <stdlib.h>
# define PAGELEN 24
# define LINELEN 512
void do_more(FILE *fp);
int see_more(FILE *fp_tty);
int main(int argc, char *argv[])
{
FILE *fp;
if(argc < 2)
{
printf("error for argment!\n");
exit(1);
}
while(--argc)
{
if((fp = fopen(*(++argv), "r")) == NULL)
{
printf("Can't open file %s\n", *argv);
exit(1);
}
do_more(fp);
fclose(fp);
}
return 0;
}
void do_more(FILE *fp)
{
int num_len = 0;
char temp[LINELEN];
int reply;
FILE *fp_tty;
if((fp_tty = fopen("/dev/tty", "r")) == NULL)
{
printf("Can't open \"/dev/tty\"\n");
exit(1);
}
while(fgets(temp, LINELEN, fp) != NULL)
{
if(num_len == PAGELEN)
{
reply = see_more(fp_tty);
if(0 == reply)
break;
num_len -= reply;
}
if(fputs(temp, stdout) == EOF)
{
printf("error for fputs!\n");
exit(1);
}
num_len++;
}
}
int see_more(FILE *fp_tty)
{
int ch;
while((ch = getchar()) != EOF)
{
if(' ' == ch)
return PAGELEN;
if('\n' == ch)
return 1;
if('q' == ch)
return 0;
}
return 0;
}
类似more命令程序
最新推荐文章于 2023-07-20 00:07:42 发布