1. more第一版
實現基礎功能,顯示每一頁固定24行文本,“q Enter”退出, “Enter” 下一行, “space Enter”下一頁。/*************************************************************************
> File Name: more01.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年04月24日 星期四 14時58分25秒
> more01.c - version 0.1 of more
> read and print 24 lines then pause for a few special commands
************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#define PAGELEN 24
#define LINELEN 514
void do_more(FILE *);
int see_more();
int main(int ac, char *av[])
{
FILE *fp;
if(ac==1)
do_more(stdin);// more 後無參數,讀取stdin
else
{
while(--ac)
if( (fp = fopen(* ++av, "r")) != NULL) // 打開文件
{
printf("%s\n", *av);
do_more(fp);
fclose(fp);
}
else
exit(1);
}
return 0;
}
void do_more(FILE *fp)
{
char line[LINELEN];
//int see_more();
int reply;
int number_line = 0;
while(fgets(line, LINELEN, fp) != NULL)
{
if(number_line == PAGELEN)
{
reply = see_more();
if(reply == 0)
break;
number_line -= reply;
}
if( fputs(line, stdout) == EOF)
exit(1);
number_line ++;
}
}
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;
}
2.more第二版
解決上一個版本“ls -l /etc | ./more01”, “ls -l /etc” 輸出重定向爲“./more01” 輸入時 由於see_more() 函數中getchar()與do_more(FILE *fp)中讀取都是stdin中的數據,時輸出一頁後不回暫停等待命令。/*************************************************************************
> File Name: more02.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年04月24日 星期四 15時39分51秒
> more02.c - version 0.2 of more
> read and print 24 lines the pause for a few special commands
> feature of version 0.2: reads form /dev/tty for commands
************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#define PAGELEN 24
#define LINELEN 514
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 see_more();
int reply;
int number_line = 0;
FILE *fp_tty;
fp_tty = fopen("/dev/tty", "r");//打開/dev/tty設備文件
if(fp_tty == NULL)
exit(1);
while(fgets(line, LINELEN, fp) != NULL)
{
if(number_line == PAGELEN)
{
reply = see_more(fp_tty);
if(reply == 0)
break;
number_line -= reply;
}
if( fputs(line, stdout) == EOF)
exit(1);
number_line ++;
}
}
int see_more(FILE *cmd)
{
int c;
printf("\033[7m more? \033[m");
while( (c = getc(cmd)) != EOF ) //此處的getchar()從stdin讀取數據,getc(cmd)從文件cmd(/dev/tty)中讀入數據
{
if(c == 'q')
return 0;
if(c == ' ')
return PAGELEN;
if(c == '\n')
return 1;
}
return 0;
}
3. more第三版
/*************************************************************************
> File Name: more04.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年04月25日 星期五 10時23分22秒
> 添加鍵入字符立即響應程序
************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#define PAGELEN 24
#define LINELEN 514
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 reply;
int number_line = 0;
FILE *fp_tty_in, *fp_tty_out;
fp_tty_in = fopen("/dev/tty", "r");
fp_tty_out = fopen("/dev/tty", "w");
struct termios initial_settings, new_settings;
tcgetattr(fileno(fp_tty_in), &initial_settings);//獲取當前終端的屬性。
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;//設置終端爲非標準模式
//new_settings.c_lflag &= ~ECHO; //設置終端不回顯
//設置讀入一個字符,立即返回字符。
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
if(tcsetattr(fileno(fp_tty_in), TCSANOW, &new_settings) != 0) { // 重新配置終端接口
fprintf(stderr, "could not set attributes\n");
}
while(fgets(line, LINELEN, fp) != NULL)
{
if(number_line == PAGELEN)
{
reply = see_more(fp_tty_in);
if(reply == 0)
break;
number_line -= reply;
}
if( fputs(line, fp_tty_out) == EOF)
{
tcsetattr(fileno(fp_tty_in), TCSANOW, &initial_settings); // 恢復終端接口的配置
exit(1);
}
number_line ++;
}
tcsetattr(fileno(fp_tty_in), TCSANOW, &initial_settings);// 恢復終端接口的配置
}
int see_more(FILE *cmd)
{
int c;
printf("\033[7m more? \033[m");
do {
c = fgetc(cmd);
if(c == 'q')
return 0;
if(c == ' ')
return PAGELEN;
if(c == '\n')
return 1;
}while(1);
return 0;
}
4. more第四版
/*************************************************************************
> File Name: more04.c
> Author: qianlv
> Mail: [email protected]
> Created Time: 2014年04月25日 星期五 14時31分07秒
> 解決"more?"重複出現的問題,已經每頁行數根據終端大小動態決定,由於
> 文件的一行可能佔2行以上終端行數,所有有小bug,顯示出已經顯示的文
> 件佔文件總大小的百分比,如果顯示的是多個文件,那麼顯示出當前讀取
> 的文件的文件名。
************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <curses.h>
#include <term.h>
#include <string.h>
#include <sys/stat.h>
#define LINELEN 514
static unsigned long filesize = 0; // 文件的總字節數
static unsigned long input_filesize = 0; // 已經顯示的字節數
static FILE *out_stream = (FILE *) 0;
static int filesum = 0; // 當前要顯示的文件個數
void clear_more(int, int, FILE *); //“el”刪除一行到行尾,用於清除“more?”.
int cols_more(FILE *fp); // 獲取終端列數
int lines_more(FILE *); // 獲取終端行數
int char_to_terminal(int ); // 一個與putchar函數有相同的參數和返回值的函數,用於tputs函數的調用。
void do_more(FILE *, char *filename);
int see_more(FILE *,int, int);
unsigned long get_filesize(FILE *);
int main(int ac, char *av[])
{
FILE *fp;
filesum = ac - 1;
if(ac==1)
do_more(stdin,(char *)0);
else
{
while(--ac)
if( (fp = fopen(* ++av, "r")) != NULL)
{
filesize = input_filesize = 0; //清空前一個文件的大小。
filesize = get_filesize(fp);
do_more(fp, *av);
fclose(fp);
}
else
exit(1);
}
return 0;
}
void do_more(FILE *fp, char *filename)
{
char line[LINELEN];
int reply;
int number_line = 0;
FILE *fp_tty_in, *fp_tty_out;
fp_tty_in = fopen("/dev/tty", "r");
fp_tty_out = fopen("/dev/tty", "w");
struct termios initial_settings, new_settings;
tcgetattr(fileno(fp_tty_in), &initial_settings);//獲取當前終端的屬性。
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;//設置終端爲非標準模式
new_settings.c_lflag &= ~ECHO; //設置終端不回顯
//設置讀入一個字符,立即返回字符。
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
if(tcsetattr(fileno(fp_tty_in), TCSANOW, &new_settings) != 0) { // 重新配置終端接口
fprintf(stderr, "could not set attributes\n");
}
int Pagelen = lines_more(fp_tty_in) - 1; //終端行數
int PageCol = cols_more(fp_tty_in); //終端列數
int add_line;
if(filesum > 1) //顯示的文件個數 > 1 那麼把文件名也顯示出來。
{
fprintf(fp_tty_out, "-------> %s <-------\n",filename);
number_line = 1;
}
while(fgets(line, LINELEN, fp) != NULL)
{
if(number_line >= Pagelen) //輸出的行數大於終端行數時,即爲一頁,原因是每次讀取文件的一行,可能佔用終端2行以上。
{
reply = see_more(fp_tty_in,Pagelen, add_line);
int prePage = Pagelen;
Pagelen = lines_more(fp_tty_in) - 1; //終端行數
PageCol = cols_more(fp_tty_in); //終端列數
if(prePage < Pagelen)
clear_more(Pagelen-1, 0, fp_tty_out); //移動遊標至"more?"這一行最前面,然後清除此行。
else
clear_more(Pagelen, 0, fp_tty_out);
if(reply == 0)
break;
if(number_line != Pagelen && reply == 1) // 當終端變大時,且按下時回車“enter”,應把number_line改爲終端倒數第二行。
number_line = Pagelen -1;
else
number_line -= reply;
}
if( fputs(line, fp_tty_out) == EOF)
{
tcsetattr(fileno(fp_tty_in), TCSANOW, &initial_settings); // 恢復終端接口的配置
exit(1);
}
int line_len = strlen(line);
input_filesize += (unsigned long)line_len;//疊加字節個數。
add_line = (line_len + PageCol - 1)/PageCol;
number_line += add_line; //文檔的行數不等於終端顯示的行數,因爲一行字符串可能佔據2行以上。
//number_line ++;
//fprintf(te, "%d: %d - %d %s",nll++,number_line, add_line,line);
}
tcsetattr(fileno(fp_tty_in), TCSANOW, &initial_settings);// 恢復終端接口的配置
}
void clear_more(int posx,int posy,FILE *fp)
{
char *el;
char *cursor;
out_stream = fp;
cursor = tigetstr("cup");
el = tigetstr("el");
tputs(tparm(cursor, posx, posy), 1, char_to_terminal); //移動關閉至(posx,posy)處。
//////////////// sleep(1);
tputs(el, 1, char_to_terminal);//清除此行至行尾。
}
int see_more(FILE *cmd,int Pagelen, int add_line)
{
int c;
if(filesize > 0 ) // 如果重定向的輸入無法獲取大小,則不要顯示百分比。
printf("\033[7m more? \033[m %lu%%",input_filesize*100/filesize);
else
printf("\033[7m more? \033[m ");
do {
c = fgetc(cmd);
if(c == 'q')
return 0;
if(c == ' ')
{
return Pagelen;
}
if(c == '\n' || c == '\r') //非標準模式下,默認回車和換行之間的映射已不存在,所以檢查回車符'\r'。
return add_line;
}while(1);
return 0;
}
int char_to_terminal(int char_to_write)
{
if(out_stream) putc(char_to_write,out_stream);
return 0;
}
int lines_more(FILE *fp)
{
int nrows;
setupterm(NULL, fileno(fp), (int *)0);
nrows = tigetnum("lines");
return nrows;
}
int cols_more(FILE *fp)
{
int ncols;
setupterm(NULL, fileno(fp), (int *)0);
ncols = tigetnum("cols");
return ncols;
}
unsigned long get_filesize(FILE *fp)
{
struct stat buf;
if(fstat(fileno(fp), &buf) < 0)
return (unsigned long) 0;
return (unsigned long) buf.st_size;
}
5. 參考書籍
- 《Linux 程序設計 第四版》 by Neil Matthew,Richard Stones 人民郵電出版社。
- Unix/Linux 編程實踐教程 by Bruce Molay 清華大學出版社。