Implementation of simple cp commands in linux: $./cp ~ /Filename ~ /OtherName // copy the file to the file $./cp ~ /Directory/filename. // copy the file to the current directory $./cp ~ /Directory/... implementation of simple cp commands in linux: $./cp ~ /Filename ~ /OtherName // copy the file to the file $./cp ~ /Directory/filename. // copy the file to the current directory $./cp ~ /Directory/filename ~ /Directory // copying a file to a directory is not in vain. directly uploading the code is King! 001 # include 002 # include 003 # include 004 # include 005 # include 006 # include 007 # include 008 # include 009 www.2cto.com 010 # define BUF_SIZE 1024011 # define PATH_LEN 128012 013 void my_err (char * err_string, int line) 014 {015 fprintf (stderr, "line: % d", line ); 016 perror (err_string); 017 exit (1); 018} 019 020 void copy_data (const int frd, const int fwd) 021 {022 int read_len = 0, write_len = 0; 023 unsigned char buf [BUF_SIZE], * p_buf; 024 025 while (read_len = read (frd, buf, BUF_SIZE) {026 027 if (-1 = read_len) {028 my_err ("Read error", _ LINE _); 029} 030 else if (read_len> 0) {// write the Read part to the target file 031 p_buf = buf; www.2cto.com 032 while (write_len = write (fwd, p_buf, read_len) {033 if (write_len = read_len) {034 break; 035} 036 else if (write_len> 0) {// only Write part 037 p_buf + = write_len; 038 read_len-= write_len; 039} 040 else if (-1 = write_len) {041 my_err ("Write error ", _ LINE _); 042} 043} 044 if (-1 = write_len) break; 045} 046} 047} 048 049int main (int argc, char ** argv) 050 {051 052 int frd, fwd; // Read/Write file descriptor 053 int len = 0; 054 char * pSrc, * pDes; // point to the source file path and target file path 055 struct stat src_st, des_st; 056 057 if (argc <3) {058 printf ("usage. /MyCp <源文件路径> <目标文件路径> \ N "); 059 my_err (" arguments error ", _ LINE _); 060} 061 062 frd = open (argv [1], O_RDONLY ); 063 if (frd =-1) {064 my_err ("Can not opne file", _ LINE _); 065} 066 067 if (fstat (frd, & src_st) =-1) {068 my_err ("stat error" ,__ LINE __); 069} www.2cto.com 070/* check whether the source file path is a directory */071 if (S_ISDIR (src_st.st_mode) {072 my_err ("skipping directories" ,__ LINE __); 073} 074 075 pDes = argv [2]; 076 stat (argv [2], & des_st); 077 if (S_ISDIR (de S_st.st_mode) {// if the target path is a directory, use the source file name 078 079 len = strlen (argv [1]); 080 pSrc = argv [1] + (len-1 ); // point to the last character 081/* First find the source file name */082 while (pSrc> = argv [1] & * pSrc! = '/') {083 pSrc --; 084} 085 pSrc ++; // point to the source file name 086 087 len = strlen (argv [2]); 088 //. indicates copying to the current working directory 089 if (1 = len &&'. '= * (argv [2]) {090 len = 0; // if no space is requested, the 091 pDes = pSrc will not be released later; 092} 093 else {// copy to a directory and use the source file name 094 pDes = (char *) malloc (sizeof (char) * PATH_LEN ); 095 www.2cto.com if (NULL = pDes) {096 my_err ("malloc error", _ LINE _); 097} 098 099 strcpy (pDes, argv [2]); 100 101 if (* (pDes + (len-1 ))! = '/') {// If the last '/' is missing from the directory, add '/' 102 strcat (pDes, "/"); 103} 104 strcat (pDes + len, pSrc); 105} 106} 107 108/* open the target file with the same permissions as the source file */109 fwd = open (pDes, O_WRONLY | O_CREAT | O_TRUNC, src_st.st_mode ); 110 if (fwd =-1) {111 my_err ("Can not creat file", _ LINE _); 112} 113 copy_data (frd, fwd ); 114 // puts ("end of copy"); 115 if (len> 0 & pDes! = NULL) 116 www.2cto.com free (pDes); 117 118 close (frd); 119 close (fwd); 120 121 return 0; 122}
Author: bo
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:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.