最近在温习《Linux/UNIX 系统编程手册》,正好补一补薄弱的linux和C的知识。

以下为题的要求:

 

代码:

  1 #include <stdio.h>
  2 #include <unistd.h>
  3 #include <stdlib.h>
  4 #include <errno.h>
  5 #include <sys/types.h>
  6 #include <fcntl.h>
  7 #include <string.h>
  8 
  9 #define FILE_LENGTH 255
 10 #define CONTENT_LENGTH 1000
 11 
 12 void printHelpMsg();
 13 void errorExit(const char *errormsg);
 14 
 15 int main(int argc, char *argv[])
 16 {
 17     int res;
 18     int fdSrc,fdDst;
 19     int readnum, writenum;//实际读到的字节数
 20     int isappend = 0 ;  // 0----若文件存在,则覆盖, 1---若文件存在,则追加
 21     char *optstr = "a";
 22     int DstFlag = O_RDWR | O_CREAT;
 23     char DstFile[FILE_LENGTH];
 24     char SrcFile[FILE_LENGTH];
 25     char SrcContent[CONTENT_LENGTH] = {0};
 26     char DscContent[CONTENT_LENGTH] = {0};
 27     
 28     //命令行参数校验
 29     if (argc < 3)
 30     {        
 31         printHelpMsg();
 32         exit(-1);
 33     }
 34     
 35     strncpy(SrcFile, argv[1], FILE_LENGTH - 1);
 36     strncpy(DstFile, argv[2], FILE_LENGTH - 1);
 37     SrcFile[FILE_LENGTH - 1] = '\0';
 38     DstFile[FILE_LENGTH - 1] = '\0';
 39     
 40     //读取命令行参数
 41     while ((res = getopt(argc, argv, optstr)) != -1)
 42     {
 43         switch (res)
 44         {
 45             case 'a':
 46                 isappend = 1;
 47                 break;
 48             case 'h':
 49                 printHelpMsg();
 50                 break;
 51             default:
 52                 printf("unknown option\n");
 53                 exit(-1);
 54         }
 55     }
 56     
 57     //追加or覆盖
 58     if (0 == isappend)
 59     {
 60         DstFlag |= O_TRUNC;
 61     }
 62     else 
 63     {
 64         DstFlag |= O_APPEND;
 65     }
 66     
 67     //打开source file
 68     fdSrc = open(SrcFile, O_RDONLY, 0);
 69     if (fdSrc <  0)
 70     {
 71         errorExit("open Srcfile failed");
 72     }
 73     
 74     //读source file
 75     readnum = read(fdSrc, SrcContent, CONTENT_LENGTH);
 76     if (readnum < 0)
 77     {
 78         errorExit("read Srcfile failed");
 79     }
 80     
 81     //关闭 source file
 82     res = close(fdSrc);
 83     if (res < 0)
 84     {
 85         errorExit("close Srcfile failed");
 86     }
 87     SrcContent[CONTENT_LENGTH - 1] = '\0';
 88     
 89     //打印至标准输出
 90     printf("%s", SrcContent);
 91     
 92     //打开dst file
 93     fdDst = open(DstFile, DstFlag, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
 94     if (fdDst < 0)
 95     {
 96         errorExit("open Dstfile failed");
 97     }
 98     
 99     //写文件
100     writenum = write(fdDst, SrcContent, readnum);
101     if ( writenum != readnum)
102     {
103         errorExit("write Dstfile failed");
104     }
105     
106     //关闭dst file
107     res = close(fdDst);
108     if (res < 0)
109     {
110         errorExit("close Srcfile failed");
111     }
112     
113     exit(0);
114 }
115 
116 void printHelpMsg()
117 {
118     printf("Usage is mytee [source file]  [des file]  [options] \n");
119 }
120 
121 void errorExit(const char *errormsg)
122 {
123     printf("error: %s\n errno=%d\n", errormsg , errno);
124     exit(-1);
125 }