linux c copy 目录下,linux下 目录到目录的复制 C语言实现

#include

#include

#include

#include

#include

int mycplink(char *name1, char *name2) //复制符号链接

{

char buf[1024]="";

int ret;

ret=readlink(name1, buf, sizeof(buf));

if(ret!=0)

{

symlink(buf , name2 );

}

return 0;

}

int mycp(char *dir1, char *dir2) //复制普通文件

{

int fd1;

int fd2;

int ret;

int sts;

char buf[1024];

fd1=open(dir1, O_RDONLY);

fd2=open(dir2, O_WRONLY|O_CREAT|O_TRUNC, 0666);

while(1)

{

bzero(buf, sizeof(buf));

ret= read(fd1, buf, sizeof(buf)-1 );

if(ret<=0)

break;

sts=write(fd2, buf, ret);

while(sts

{

sts+=write(fd2, buf+sts, ret-sts);

}

}

close(fd1);

close(fd2);

struct stat info;

ret=stat(dir1, &info);

if(ret!=0)

{

perror("stat");

return 1;

}

chmod(dir2, info.st_mode);

return 0;

}

int mycpdir(char *name1, char *name2) //复制目录

{

int ret;

DIR *dir1;

DIR *dir2;

struct dirent *drr;

struct stat info;

struct stat dirinfo;

char buf1[1024];

char buf2[1024];

lstat(name1, &info); //获取name1的信息

dir1=opendir(name1);

if(dir1==NULL)

{

perror("opendir1");

return 1;

}

dir2=opendir(name2); //打开name2, 打开失败则创建

if(dir2==NULL)

{

mkdir(name2, info.st_mode);

dir2=opendir(name2);

}

while(1)

{

bzero(buf2, sizeof(buf2));

bzero(buf2, sizeof(buf2));

drr=readdir(dir1);

if(drr==NULL)break;

if(*drr->d_name=='.') //过滤掉 "." ".."

continue;

sprintf(buf1, "%s/%s", name1, drr->d_name); //构造完整的相对路径

sprintf(buf2, "%s/%s", name2, drr->d_name);

ret=lstat(buf1, &dirinfo);

if(ret<0)

{

perror("lstat\n");

break;

}

if(S_ISDIR(dirinfo.st_mode)) //如果是目录

{

mycpdir(buf1 , buf2);

}

else if(S_ISREG(dirinfo.st_mode)) //如果是普通文件

{

mycp(buf1, buf2);

}

else if(S_ISLNK(dirinfo.st_mode)) //如果是符号链接

{

mycplink(buf1, buf2);

}

}

closedir(dir1);

closedir(dir2);

return 0;

}

int main(int argc, char **argv)

{

if(argc!=3)

goto argc_err;

mycpdir(argv[1], argv[2]); //输入两个路径

return 0;

argc_err:

printf("argc!=3\n");

return 1;

}自己写的,代码比较乱。时间比较晚了,今天就写到这。以后有时间写一个更好点的版本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言Linux环境下递归复制目录的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <dirent.h> #include <sys/stat.h> int copy_file(const char *src_path, const char *dst_path) { FILE *src_file, *dst_file; char buf[BUFSIZ]; int size; if ((src_file = fopen(src_path, "rb")) == NULL) { fprintf(stderr, "Failed to open source file %s: %s\n", src_path, strerror(errno)); return -1; } if ((dst_file = fopen(dst_path, "wb")) == NULL) { fprintf(stderr, "Failed to open destination file %s: %s\n", dst_path, strerror(errno)); fclose(src_file); return -1; } while ((size = fread(buf, 1, BUFSIZ, src_file)) > 0) { if (fwrite(buf, 1, size, dst_file) != size) { fprintf(stderr, "Failed to write to destination file %s: %s\n", dst_path, strerror(errno)); fclose(src_file); fclose(dst_file); return -1; } } fclose(src_file); fclose(dst_file); return 0; } int copy_dir(const char *src_path, const char *dst_path) { DIR *src_dir; struct dirent *dir_ent; struct stat st; char src_file_path[PATH_MAX], dst_file_path[PATH_MAX]; if ((src_dir = opendir(src_path)) == NULL) { fprintf(stderr, "Failed to open source directory %s: %s\n", src_path, strerror(errno)); return -1; } if (mkdir(dst_path, 0755) == -1) { if (errno != EEXIST) { fprintf(stderr, "Failed to create destination directory %s: %s\n", dst_path, strerror(errno)); closedir(src_dir); return -1; } } while ((dir_ent = readdir(src_dir)) != NULL) { if (strcmp(dir_ent->d_name, ".") == 0 || strcmp(dir_ent->d_name, "..") == 0) { continue; } snprintf(src_file_path, PATH_MAX, "%s/%s", src_path, dir_ent->d_name); snprintf(dst_file_path, PATH_MAX, "%s/%s", dst_path, dir_ent->d_name); if (lstat(src_file_path, &st) == -1) { fprintf(stderr, "Failed to get information of source file %s: %s\n", src_file_path, strerror(errno)); closedir(src_dir); return -1; } if (S_ISREG(st.st_mode)) { if (copy_file(src_file_path, dst_file_path) == -1) { closedir(src_dir); return -1; } } else if (S_ISDIR(st.st_mode)) { if (copy_dir(src_file_path, dst_file_path) == -1) { closedir(src_dir); return -1; } } } closedir(src_dir); return 0; } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <source_dir> <destination_dir>\n", argv[0]); return 1; } if (copy_dir(argv[1], argv[2]) == -1) { return 1; } return 0; } ``` 该程序的核心是 `copy_dir` 函数,该函数使用递归的方式复制目录。如果遇到一个文件,则调用 `copy_file` 函数复制文件;如果遇到一个目录,则递归调用 `copy_dir` 函数复制目录复制文件实现使用了标准的文件读写操作,复制目录实现使用了标准的目录操作和 `lstat` 函数获取文件信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值