目录cp

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
typedef void (*PF)(char* name,char* destname);
void dirwalk(char* dir , PF fcn, char* dest)
{
    char name[1024] = {0};
    char destname[1024] = {0};
    struct dirent* dp = NULL;
    DIR* dfd = NULL;
    if(NULL == (dfd = opendir(dir)))
    {
        fprintf(stderr,"dirwalk: cannot open %s",dir);
        return;
    }
    while(dp = readdir(dfd))
    {
        if(!strcmp(dp->d_name,".")|| !strcmp(dp->d_name,".."))
        {
            continue;
        }
        if(strlen(dir) + strlen(dp->d_name) + 2 > sizeof(name))
        {
            fprintf(stderr,"dirwalk:name %s %s too long\n",dir,dp->d_name);
        }
        else
        {
  sprintf(name,"%s/%s",dir,dp->d_name);
  sprintf(destname,"%s/%s",dest,dp->d_name);//???
            fcn(name,destname);
        }

    }
    closedir(dfd);
}
void cp_file(char * src_path,char* dest_path)
{
    int fd = open(src_path, O_RDONLY);
    int n;
    int buf[1024];
    if(fd < 0)
    {
        perror("open file");
        exit(1);
    }
    int fd1 = open(dest_path, O_RDWR | O_TRUNC | O_CREAT, 0644);
    while((n =read(fd, buf, 100)) != 0){
        write(fd1, buf, n);
    }
    close(fd);
}
void fsize(char * name,char * destname)
{
    struct stat stbuf;
    if(-1 == stat(name, &stbuf))
    {
        fprintf(stderr, "fsize: cannot access %s\n",name);
        return;
    }
    else
    {
        if(S_ISDIR(stbuf.st_mode))
        {
            //创建目录
            mkdir(destname,0755);//???
            dirwalk(name,fsize,destname);//???
        }
        else
        {
            cp_file(name,destname);
        //printf("%8ld %s\n",stbuf.st_size, name);
        }
    }
}

int main(int argc, char* argv[])
{
    dirwalk(argv[1],fsize,argv[2]);
    //fsize(argv[1],argv[2]);//???

}

转载于:https://www.cnblogs.com/jade-L/p/3238487.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值