Simple implementation of the Linux CP command

Source: Internet
Author: User

/*To implement the CP command:*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<fcntl.h>#include<string.h>intMainintargcChar*argv[]) {    intRD_FD, WR_FD;//read file descriptors and write file descriptors    Charbuf[ -]={0}; intRd_ret =0; if(ARGC <3) {printf ("Please input src file and des file!\n"); return-1; }    //Open source FileRD_FD = open (argv[1], o_rdonly); if(Rd_fd <0) {printf ("open src file%s failed!\n", argv[1]); return-2; } printf ("open src file%s succ, rd_fd =%d\n", argv[1], RD_FD); //Open the destination fileWR_FD = open (argv[2], o_wronly|o_creat); if(Wr_fd <0) {printf ("Open des file%s failed!\n", argv[2]); return-3; }         while(1) {Rd_ret= Read (RD_FD, buf, -); if(Rd_ret < -)//determine if the data has been read        {             Break;        } write (WR_FD, buf, Rd_ret); memset (BUF,0, -);//clean up the cache} write (WR_FD, buf, Rd_ret);//do one last write//Close File DescriptorClose (WR_FD); Close (RD_FD)return 0;}

Simple implementation of the Linux CP command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.