Linux System Programming of the cp command to achieve

Linux System Programming of the cp command to achieve

cp command is frequently used commands, copy files, and the idea of ​​its preparation process is very intuitive. The data file is read out, create a new file, and then write data into it. This is a very conventional way of thinking. The main issues to think about in preparation of a comprehensive program is to input each case must be considered fully.

Open the file, create a file, read the file data, write data, close the file is still common kernel function calls open (), creat (), read (), write (), close () to complete.

Before writing the code than to simple commands.

Because more error command, e.g. may occur according to the cp command: deletion of the source files, object files, the file data can be read without the like, so that the information package to be given a function.

 36 void opps(char *s1,char *s2)
 37 {
 38         fprintf(stderr,"Error:%s",s1);
 39         perror(s2);
 40         exit(1);
 41 }

Other file operations are more familiar with, but when used fprintf of checked its definition still can not understand this too, after their own little experiment you can understand it as the third output format output in the second term the first item in the file to where it flows to the standard error. The Perror more familiar, the cause of the error on the output functions to the standard error stream.

Then look at the main functions:

 10 int main(int ac,char *av[])
 11 {
 12         int in_fd,out_fd,n_chars;
 13         char buf[BUFFERSIZE];
 14 
 15         if(ac!=3)
 16         {
 17                 fprintf(stderr,"usage:%s source destination\n",*av);
 18                 exit(1);
 19         }
 20 
 21         if((in_fd=open(av[1],O_RDONLY)) ==-1)
 22                 opps("Cannot open",av[1]);
 23 
 24         if((out_fd=creat(av[2],COPYMODE)) ==-1)
 25                 opps("Cannot creat",av[2]);
 26         while((n_chars=read(in_fd,buf,BUFFERSIZE))>0)
 27                 if(write(out_fd,buf,n_chars) != n_chars)
 28                         opps("Write error to",av[2]);
 29         if(n_chars == -1)
 30                 opps("Read error from",av[1]);
 31         if(close(in_fd) == -1||close(out_fd) ==-1)
 32                 opps("Error closing files","");
 33 
 34 }

Function to determine the program itself is better understood, but very clever is the use of the main function parameters, to write their own words will introduce other variables flag judgment parameters of the problem before the main function related issues explored this principle is no longer detail says , you can look at its use.

In determining whether or not the correct command format to determine if (ac! = 3), itself already running when ac 1, while using the cp command requires the source file and the target file, so when the value of the ac course is 3, so if you do not 3 how to do? Outputs usage: cp source destination, the back pointer always points to the first command is stored in the parameter cp array, where it is clever use of main parameters as a function of judgment, to judge the need to introduce other variables.

Needless to say the latter, the second array is stored in the source file, target third storage file, in the latter determination may be employed av [i] instead of the source files and object files. It can be said with the main function parameters achieved once and for all.

After compile and run the code you can use to write their own cp command to copy files slightly.

Of course, this program also has a very worthwhile place to learn, defines a buffersize for temporarily storing the data out, that the provisions of the buffer zone, which is widely used not only in the usual programming, the kernel is also very important mechanism . For details, see System Programming Some raise a small sum .

Published 15 original articles · won praise 13 · views 9058

Guess you like

Origin blog.csdn.net/weixin_43122409/article/details/84535139