
正文
C实现Linux中copy功能
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
/*
mycp.c
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<dirent.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<getopt.h>
#include<stdbool.h>#define BUFFERSIZE 1024
#define COPYMORE 0644/*用于处理从目录文件复制到目录文件的操作,传入的参数应该是目录路径*/
int copyD2D(char *src, char *dest);/*用于处理从文件复制到文件的操作,输入的参数是文件名*/
int copyF2F(char *src, char *dest);/*判断filename是否是目录名*/
bool isdir(char *filename);/*字符串反转*/
char *strrev(char *str);/*main函数用于接收命令行传进来的参数,并判断是否符合规范。
然后根据不同的情况调用不同的子函数。*/
int main(int argc, char **argv)
{
/*
标记-r/-R选项,该选项代表递归复制文件夹
标记-l选项,-l选项代表创建硬链接
标记-s选项,-s选项代表创建符号链接
*/
bool opt_r = false;
bool opt_l = false;
bool opt_s = false; /*
用于记录源文件
用于记录目标文件
记录选项的字符
*/
char *src = NULL;
char *dest = NULL; char c; /*循环检测命令行参数中的选项*/
while((c = getopt(argc, argv, "rRls")) != -)
{
switch(c)
{
case 'R':
case 'r':
opt_r = true;
break; case 'l':
opt_l = true;
break; case 's':
opt_s = true;
break;
}
} /*命令行参数中应该有两个文件名。若没有,则输出提示,并终止程序*/
if (optind >= argc - )
{
printf("lack operator \n");
exit();
} /*从命令行参数中读取源文件和目标文件名*/
src = argv[optind];
dest = argv[optind + ]; /*根据opt_l选项的真假,做相应操作。
若为真,则创建硬链接,使用link函数。*/
if (opt_l)
{
if (isdir(src))
{
printf("dirent canot build hard linker\n");
exit();
} /*link 函数的返回值:若成功,则返回0;若出错,返回-1*/
if ((link(src, dest)) == )
{
return ;
}
else
{
printf("create hard linker error\n");
exit();
}
} /*根据opt_s选项的真假,做相应操作。
若为真,则创建符号链接,使用symlink函数。*/
if (opt_s)
{
if(isdir(src))
{
printf("dirent cannot create soft linker\n");
exit();
} if (symlink(src, dest) == )
{
return ;
}
else
{
printf("create soft linker error\n");
exit();
}
} if (!isdir(src))
{
/*若源文件src不是目录,直接调用copyF2F函数。*/
if ((copyF2F(src, dest)) == )
{
return ;
}
else
{
printf("copy file error\n");
exit();
}
}
else if(isdir(src))
{
if (!isdir(dest))
{
printf("Canot copy the dirent to a file\n");
exit();
}
/*若源文件src和目标文件dest都是目录,直接调用copyD2D函数。*/
else if(isdir(dest) && opt_r)
{
if (copyD2D(src, dest) != )
{
printf("copy catalog error\n");
exit();
}
else
{
return ;
}
}
else
{
printf("copy catalog need option -r\n");
exit();
}
}
else
{
printf("the operation is illegal\n");
exit();
} return ;
}/*该函数用于处理复制目录的情况*/
int copyD2D(char *src_dir, char *dest_dir)
{
DIR *dp = NULL;
struct dirent *dirp;
char tempDest[];
char tempSrc[];
strcpy(tempDest, dest_dir);
strcpy(tempSrc, src_dir); /*使用opendir函数打开src_dir目录,获得指向该目录名字的指针*/
if ((dp = opendir(src_dir)) == NULL)
{
return ;
}
else
{
while((dirp = readdir(dp)))
{
struct stat file_stat;
if (!isdir(dirp->d_name))
{
/*将dirent结构中的d_name成员变量链接到上级目录字符串*/
strcat(tempDest, dirp->d_name);
strcat(tempSrc, dirp->d_name); /*此处转换为文件复制函数的方式处理目录复制*/
copyF2F(tempSrc, tempDest); /*通过字符串拷贝函数,将tempDest和tempSrc还原为上级的目录名*/
strcpy(tempDest, dest_dir);
strcpy(tempSrc, src_dir);
}
}
closedir(dp);
}
return ;
}/*该函数通过read,write等基本的系统函数,完成文件的拷贝工作*/
int copyF2F(char *src_file, char *dest_file)
{
int in_fd, out_fd, n_chars;
char buf[BUFFERSIZE]; /*如果目标文件是一个目录,那么默认是在该目录下建立一个与源文件同名的文件*/
if (isdir(dest_file))
{
char c;
char temp[] = {};
char *r_temp;
int n = strlen(src_file);
int m = ; /*读取源文件的最后一级文件名作为目标文件名*/
while((c = src_file[n-]) != '/')
{
temp[m] = c;
m++;
n--;
}
r_temp = strrev(temp);
strcat(dest_file, r_temp);
} /* 以可读模式打开源文件 */
if ((in_fd = open(src_file, O_RDONLY)) == -)
{
printf("%s file read error!\n", src_file);
return ;
} /* O_WRONLY代表以读写的方式打开目标文件,O_CREAT选项代表若文件不存在则创建,
COPYMORE = 0644,文件所有者可读可写,其他可读 */
if ((out_fd = open(dest_file, O_WRONLY | O_CREAT, COPYMORE)) == -)
{
return ;
} /* 通过read和write系统调用实现文件的复制 */
while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > )
{
if (write(out_fd, buf, n_chars) != n_chars)
{
printf("%s file write error!\n", src_file);
return ;
} if (n_chars == -)
{
printf("%s file read error!\n", src_file);
return ;
}
} if (close(in_fd) == - || close(out_fd) == -)
{
printf("close file error\n");
return ;
} return ;
}/*判断filename是否为目录文件*/
bool isdir(char *filename)
{
struct stat fileInfo;
if (stat(filename, &fileInfo) >= )
{
if (S_ISDIR(fileInfo.st_mode))
{
return true;
}
else
{
return false;
}
}
}char *strrev(char *str)
{
int i = strlen(str) - , j = ; char ch;
while(i > j)
{
ch = str[i];
str[i] = str[j];
str[j] = ch;
i--;
j++;
} return str;
}
运行结果:








