在linux环境下,用C递归复制目录
时间: 2024-02-18 07:04:40 浏览: 29
以下是使用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` 函数获取文件信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)