選擇java 進入自由開放的國度

          隨筆 - 49, 文章 - 3, 評論 - 154, 引用 - 1
          數據加載中……

          linux中命令more的實現

          實現了命令more的基本功能,但還有如下缺陷:more的反白先是不能固定,不能顯示百分比,在輸入命令后必需按回車。

          下面是代碼,在FC4上使用gcc 編譯通過。
          ??1?#include?<stdio.h>
          ??2?#include?<stdlib.h>
          ??3?
          ??4?#define?PAGELEN?24
          ??5?#define?LINELEN?512
          ??6?
          ??7?void?do_more(FILE?*);
          ??8?int?see_more();
          ??9?int?see_more(FILE?*);
          ?10?
          ?11?
          ?12?int?main(int?ac,?char?*av[])
          ?13?{
          ?14???FILE?*fp;
          ?15???
          ?16???if?(ac?==?1)
          ?17????????do_more(stdin);
          ?18???else
          ?19????????while(--ac)
          ?20????????????if((fp?=?fopen(*?++av,?"r"))?!=?NULL)
          ?21????????????{
          ?22??????????????do_more(fp);
          ?23??????????????fclose(fp);
          ?24????????????}
          ?25????????????else
          ?26??????????????exit(1);
          ?27??
          ?28????return?0;
          ?29?}
          ?30?
          ?31?void?do_more(FILE?*fp)
          ?32?/*
          ?33??*??read?PAGELEN?lines,?then?call?see_more()?for?further?instructions
          ?34??*/
          ?35?{
          ?36???char?line[LINELEN];
          ?37???int?num_of_lines?=?0;
          ?38???int?see_more(FILE?*),?reply;
          ?39???
          ?40???
          ?41???//?get?input?from?stardard?input?device
          ?42????/*
          ?43????FILE?*fp_tty;
          ?44????fp_tty?=?fopen("/dev/tty",?"r");
          ?45????if?(fp_tty?=?NULL)?
          ?46????????exit(1);*/
          ?47????
          ?48????FILE????*fp_tty;
          ?49?
          ?50?????fp_tty?=?fopen(?"/dev/tty",?"r"?);????????
          ?51?????if?(?fp_tty?==?NULL?)???????????????
          ?52?????????exit(1);???????????????????????????
          ?53?????????
          ?54???while?(fgets(line,?LINELEN,?fp))
          ?55???{
          ?56?????if?(?num_of_lines?==?PAGELEN)
          ?57?????{
          ?58???????//printf("\n======?\n");
          ?59???????//reply?=?see_more();
          ?60???????reply?=?see_more(fp_tty);
          ?61???????if?(reply?==?0)?break;
          ?62???????num_of_lines?-=?reply;
          ?63?????}
          ?64?????
          ?65?????if?(fputs(line,?stdout)?==?EOF)?
          ?66?????????exit(1);
          ?67?????
          ?68?????num_of_lines++;
          ?69?????
          ?70?????//printf("\n======?num_of_lines?=?%d?\n",?num_of_lines);
          ?71???}
          ?72?}
          ?73?
          ?74?int?see_more()
          ?75??/*
          ?76??*??print?message?,?wait?for?response,?return?#?of?lines?to?advance
          ?77??*??q?means?no,?space?means?yes,?CR?means?one?line.
          ?78??*/
          ?79?{
          ?80???int?c;
          ?81???printf("\033[7m?more??\033[m");
          ?82???while?(?(c?=?getchar())?!=?EOF)
          ?83???{
          ?84?????if?(?c?==?'q')??return?0;
          ?85?????if?(?c?==?'?')??return?PAGELEN;
          ?86?????if?(?c?==?'\n')?return?1;
          ?87???}
          ?88???
          ?89???return?0;
          ?90?}
          ?91?
          ?92?int?see_more(FILE?*cmd)
          ?93?{
          ?94???int?c;
          ?95???printf("\033[7m?more??\033[m");
          ?96???while?(?(c?=?getc(cmd))?!=?EOF)
          ?97???{
          ?98?????if?(?c?==?'q')??return?0;
          ?99?????if?(?c?==?'?')??return?PAGELEN;
          100?????if?(?c?==?'\n')?return?1;
          101???}
          102???
          103???return?0;
          104?}
          105?
          有兩個子函數,do_more()和see_more(),前者處理顯示,后者處理命令。

          以后在進行改進。

          posted on 2006-03-27 08:36 soochow_hhb 以java論成敗 以架構論英雄 閱讀(1169) 評論(0)  編輯  收藏 所屬分類: Reading

          主站蜘蛛池模板: 天等县| 瓦房店市| 无棣县| 东城区| 如东县| 曲麻莱县| 顺平县| 饶河县| 通辽市| 宝坻区| 贡山| 仁布县| 吴川市| 黑山县| 武山县| 阿坝县| 威宁| 大足县| 明水县| 瓮安县| 来安县| 留坝县| 武安市| 沁阳市| 图木舒克市| 札达县| 交口县| 上蔡县| 永胜县| 甘德县| 多伦县| 江源县| 乌拉特前旗| 崇州市| 望城县| 兰溪市| 阳谷县| 淮安市| 扎兰屯市| 昌平区| 江孜县|