科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件CTDP linux 程序员手册 C和C++编程(11)Viewmod程序

CTDP linux 程序员手册 C和C++编程(11)Viewmod程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

大家可以通过这个例子体会linux的c编程。

作者:杀尽天下的恶狗 来源:CSDN 2008年3月26日

关键字: Viewmod 手册 程序员 开源

  • 评论
  • 分享微博
  • 分享邮件

大家可以通过这个例子体会linux的c编程。

#include <termios.h>

#include <stdio.h>

#include <unistd.h>

#include <fcntl.h>

#include <dirent.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

#include <sys/signal.h>

#include <sys/types.h>

#include <sys/stat.h>

#define _POSIX_SOURCE 1         //POSIX 兼容源码

#define FALSE 0

#define TRUE 1

#define MAX_DEVICES 64

#define MAX_IDS 64

#define LINE_LENGTH 1128

FILE *output;

char input_fname[80];                                                                           //打印机的文件名称

FILE *input_file;

mode_t mode;

char Char;                                                                             //用于单个char的处理

int display;               //"-D" 项: 0-ASC,1-ASC/HEX, 2-Hex 3-dec 4-dec/asc 5-DOS text 6-UNIX text 7- strip all non-asc

int out_length; //-L 选项

int display_col;         //显示列计数

int char_len;

int asc_char;            //0-non-asc, 1-asc, 2-CR, 3-LF

int nospace;

int last_char;            //最后一个 char的类型

int get_linestring(char *file_line, char *label_name, int qty_statements, int strno);

int freadln(FILE *handle,char *outputline); //从一个文件读一行

void Process_Bufchar();           //一次处理一个在缓存中的Char

main(int Parm_Count, char *Parms[])

{

   char message[90];

   int start_options, in_source, done;

   char *lastslash;                                                 //最后一个slash 的字符串地址

   char dirname[80];                               //目录名称

   DIR *current_directory;

   int i;

   char In1, Key;

   char buf[255];                       //数据缓存

   out_length = 80;

   display = 1; //缺省值 HEX/ASC 显示, 在CR, CR/LF, LF后断开

   start_options=0;

   in_source = 0;          //缺省值,便准输出

   if (Parm_Count>1) //在程序名后面如果有参数

   {

        start_options = 1;

        strcpy(message,Parms[1]);

                               if (message[0]!='-') //如果是一个输入文件的名称

                               {

                                              if ((input_file = fopen(Parms[1], "rb")) == NULL)

                                              {

                                                             fprintf(stderr,"%s: Unable to open the input file %s\n",Parms[0],Parms[1]);

                                                             exit(1);                     //发生错误退出

                                              }

                                              else

                                              {

                                                             in_source = 1; //file

                                              }

                               } //end 如果是一个输入文件

               } //end在程序名后面如果有参数

               //获得参数

               if (start_options<Parm_Count) //如果开始选项存在

               {

                               for (i=1; i<Parm_Count; i++)

                               {

                                              strcpy(message, Parms[i]);

                                              if (message[0]=='-')

                                              {

                                                             if ((message[1]=='D') || (message[1]=='d')) //if display option

                                                             {

                                                                            if (message[2]=='0') display=0;

                                                                            if (message[2]=='1') display=1;

                                                                            if (message[2]=='2') display=2;

                                                                            if (message[2]=='3') display=3;

                                                                            if (message[2]=='4') display=4;

                                                                            if (message[2]=='5') display=5;

                                                                            if (message[2]=='6') display=6;

                                                                            if (message[2]=='7') display=7;

                                                             }

                                                             if ((message[1]=='L') || (message[1]=='l')) //如果有 length选项

                                                             {

                                                                            out_length=atoi(&message[2]);

                                                             }

                                              }

                               }

               } //end如果开始选项存在

               done = 0;

               display_col=0;

               nospace = 1; //我们不需要space

               last_char=4;

               while (!done)

               {

                               if (in_source==1) //从文件读一个字符

                               {

                                              if ((Char=fgetc(input_file))==EOF)

                                              {

                                                             done = 1;

                                              }

                               }

                               else         //从标准输入读一个字符

                               {

                                              Char=getchar();

                                              if (Char==EOF)

                                              {

                                                             done = 1;

                                              }

                               }

                               if (done==0)             //输出这个字符到标准输出

                               {

                                              asc_char=0;             //假设不是 asc 字符

                                              if ((Char>31) && (Char < 127)) asc_char = 1;

                                              if (Char==13) asc_char = 2;

                                              if (Char==10) asc_char = 3;

                                  switch (display)

                                              {

                                              case 0:                    //ASC

                                                             if (asc_char < 2) char_len=1;

                                                             else char_len=0;

                                                             if (display_col+char_len>out_length)

                                                             {

                                                                            putchar(10); //如果此行太长滚动

                                                                            display_col=0;

                                                             }

                                                             putchar(Char);

                                                              display_col++;

                                                             if (asc_char==3) display_col=0;

                                                             break;

                                              case 1:                    //ASC/HEX

                                              default:

                                                             if (asc_char==1) char_len=1;

                                                             else char_len=2;

                                                             if ((display_col==0) || (last_char==4) || ((last_char==1) && (asc_char==1))) nospace=1; //dont need space

                                                             else nospace=0;

                                                             if (nospace==0) char_len++; //如果我们需要一个 space

                                                             if (display_col+char_len>out_length)

                                                             {

                                                                            putchar(10); //如果此行太长滚动

                                                                            display_col=0;

                                                                            if (nospace==0)

                                                                            {

                                                                                            nospace=1;

                                                                                            char_len--;

                                                                            }

                                                             }

                                                             if (nospace==0) //加一个 space

                                                             {

                                                                            putchar(' ');

                                                                            display_col++;

                                                             }

                                                             if (asc_char==1)

                                                             {

                                                                            putchar(Char);

                                                                            display_col++;

                                                             }

                                                             else

                                                             {

                                                                            sprintf(message,"%2x",Char);

                                                                            fputs(message,stdout);

                                                                            display_col +=2;

                                                                            if (asc_char==3)

                                                                            {

                                                                                            putchar(10);

                                                                                            display_col=0;

                                                                            }

                                                             }

                                                             if ((last_char==2) && (asc_char!=3)) //如果有 CR 没有 LF

                                                             {

                                                                            putchar(10);

                                                                            display_col=0;

                                                             }

 

                                                             last_char=asc_char;

                                                             break;

                                              case 2:                    //hex

                                                             char_len=2;

                                                             if (display_col==0) nospace = 1;

                                                             else nospace = 0;

                                                             if (nospace == 0) char_len++;                  //如果我们需要一个 space

                                                             if (display_col+char_len>out_length)

                                                             {

                                                                            putchar(10); //如果此行太长滚动

                                                                            display_col=0;

                                                                            if (nospace==0)

                                                                            {

                                                                                            nospace=1;

                                                                                            char_len--;

                                                                            }

                                                             }

                                                             if (nospace==0) //添加space

                                                             {

                                                                            putchar(' ');

                                                                            display_col++;

                                                             }

                                                             sprintf(message,"%2x",Char);

                                                             fputs(message,stdout);

                                                             display_col +=2;

                                                             break;

                                              case 5:     //unix 文本 – 调整dos文本,在所有LF加CR

                                                             if (Char==10) putchar(13);

                                                             putchar(Char);

                                                             break;

                                              case 6:                    //dos 文本 – 到 unix, 跳过所有CR

                                                             if ((last_char==2) && (asc_char !=3)) putchar(10); //if this char is not a LF and the last one was CR, need a line feed for unix

                                                             if (Char!=13) putchar(Char);

                                                             last_char=asc_char;

                                                             break;

                                              case 3:                    //decimal

                                                             char_len=3;

                                                             if (display_col==0) nospace = 1;

                                                             else nospace = 0;

                                                             if (nospace == 0) char_len++;                  //如果我们需要一个 space

                                                             if (display_col+char_len>out_length)

                                                             {

                                                                            putchar(10); //如果此行太长滚动

                                                                            display_col=0;

                                                                            if (nospace==0)

                                                                            {

                                                                                            nospace=1;

                                                                                            char_len--;

                                                                            }

                                                             }

                                                             if (nospace==0) //添加一个 space

                                                             {

                                                                            putchar(' ');

                                                                            display_col++;

                                                             }

                                                             sprintf(message,"%3d",Char);

                                                             fputs(message,stdout);

                                                             display_col +=2;

                                                             break;

                                              case 4:                    //decimal asc

                                                             if (asc_char==1) char_len=1;

                                                             else char_len=3;

                                                             if ((display_col==0) || (last_char==4) || ((last_char==1) && (asc_char==1))) nospace=1; //dont need space

                                                             else nospace=0;

                                                             if (nospace==0) char_len++; //如果我们需要一个space

                                                             if (display_col+char_len>out_length)

                                                             {

                                                                            putchar(10); //如果此行太长滚动

                                                                            display_col=0;

                                                                            if (nospace==0)

                                                                            {

                                                                                            nospace=1;

                                                                                            char_len--;

                                                                            }

                                                             }

                                                             if (nospace==0) //add a space

                                                             {

                                                                            putchar(' ');

                                                                            display_col++;

                                                             }

                                                             if (asc_char==1)

                                                             {

                                                                            putchar(Char);

                                                                            display_col++;

                                                             }

                                                             else

                                                             {

                                                                            sprintf(message,"%3d",Char);

                                                                            fputs(message,stdout);

                                                                            display_col +=2;

                                                                            if (asc_char==3)

                                                                            {

                                                                                            putchar(10);

                                                                                            display_col=0;

                                                                            }

                                                             }

                                                             if ((last_char==2) && (asc_char!=3)) //如果有 CR 没有 LF

                                                             {

                                                                            putchar(10);

                                                                            display_col=0;

                                                             }

                                                             last_char=asc_char;

                                                             break;

                                              case 7:                                   //跳过所有得 non-asc 字符转到 Unix 文件 (除了ASC 和 LF)

                                                             if ((asc_char==1) || (asc_char==3)) putchar(Char);

                                                             break;

                                              } //end of switch display

                               } //end of if not done

               } //end of while not done

} //end of main

 

 

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章