linux 目录拷贝程序实现

目录拷贝程序思路:

一、验证参数

目录拷贝程序参数必定包含源目录路径和目标位置

二、调用函数执行源目录到目标位置的拷贝

三、在目标路径创建被拷贝目录的最顶层位置

四、递归遍历目录

a. 对于目录中的子目录,更改源目录和目标位置,调用函数执行拷贝

b. 对于目录中的文件,执行文件拷贝

目录拷贝程序实现:

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>

#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<string.h>

void cp_file(char *src_file, char *dst_file){
       FILE *src, *dst;
       char buf[255];
       printf("src file: %s\n", src_file);
       printf("dst file: %s\n", dst_file);
       src = fopen(src_file, "r");
       dst = fopen(dst_file, "w");
       if(NULL == src){ 
               printf("srcd error\n");
               exit(EXIT_FAILURE);
       }
       if(NULL == dst){
               printf("dst error\n");
               exit(EXIT_FAILURE);
       }
       while(1){
               if(fgets(buf, 255, src)){
                       fputs(buf, dst);
               } else
                       break;
	       }
       fclose(src);
       fclose(dst);
}

void cp_dir(char *src_path, char *dst_path){

	DIR *srcdir = opendir(src_path);
	if(NULL == srcdir){
		printf("open dir %s error, pls check it.\n", src_path);
		exit(EXIT_FAILURE);
	}

	struct dirent *tmp = NULL;

	char sub_dir_path[255];
	size_t length = strlen(src_path);
	memset(sub_dir_path, '\0', 255);
	strcpy(sub_dir_path, src_path);
	sub_dir_path[length] = '/';
	sub_dir_path[length + 1] = '\0';

	char dst_dir_path[255];
	size_t dstlen = strlen(dst_path);
	memset(dst_dir_path, '\0', 255);
	strcpy(dst_dir_path, dst_path);
	dst_dir_path[dstlen] = '/';
	dst_dir_path[dstlen + 1] = '\0';

	char *dirname = strrchr(src_path, '/');
	printf("dirname %s\n", dirname);
	strcat(dst_dir_path, dirname);
	int dst_pre_path_length = strlen(dst_dir_path);
	mkdir(dst_dir_path, S_IRUSR|S_IWUSR|S_IXUSR);

	while(1){
		tmp = readdir(srcdir);
		if(NULL == tmp) break;
		if(strcmp(".", tmp->d_name) == 0 ||
			strcmp("..", tmp->d_name) == 0)
			continue;
		//1:get source path (file or director)
		strcat(sub_dir_path, tmp->d_name);
		if(tmp->d_type ==  DT_DIR){
			printf("sub dir name is: %s\n", tmp->d_name);
			cp_dir(sub_dir_path, dst_dir_path); 
		}else {
			//TODO: copy file
			//set dst file path
			strcat(dst_dir_path, "/");
			strcat(dst_dir_path, tmp->d_name);
			printf("src: %s \ndst: %s\n", sub_dir_path, dst_dir_path);
			cp_file(sub_dir_path, dst_dir_path);
			dst_dir_path[dst_pre_path_length] = '\0';
		}
		// Reset prefix of source path (file or director)
		sub_dir_path[length + 1] = '\0';
	}
	closedir(srcdir);
}

int main(int argc, char **argv){
	if(3 != argc){
		printf("USAGE: %s src_dir, dst_disr\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	cp_dir(argv[1], argv[2]);
	//cp_file(argv[1], argv[2]);
	return 0;
}


  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值