linux下c语言实现递归复制文件夹

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>

#define BLOCK 4096
#define MAX_PATH 256

static char buff[BLOCK];
static char path[MAX_PATH];
static bool cp_dir(DIR*);
static struct stat stbuf;

int main(int argc,const char *argv[])
{

   if(argc != 3)
   {
      fprintf(stderr,"invalid arg num!\n");
      exit(EXIT_FAILURE);
   }

   DIR *dp  = opendir(argv[1]);

   if(dp == NULL)
   {
       fprintf(stderr," 1:\t%s\n",strerror(errno));
       exit(EXIT_FAILURE);
   }

   strcpy(path,argv[2]);

   if(stat(argv[1],&stbuf) < 0)
   {
      fprintf(stderr,"2:\t%s \n",strerror(errno));
      exit(EXIT_FAILURE);
   }

   if(mkdir(path,stbuf.st_mode) < 0)
   {
      fprintf(stderr,"3:\t%s\n",strerror(errno));
      exit(EXIT_FAILURE);
   }

   return cp_dir(dp);
}

static struct dirent *dirp;
static bool cp_dir(DIR *dp)
{
    int sfd = dirfd(dp);
    int dfd;
    fchdir(sfd);
    int n = strlen(path);
    path[n++] = '/';
    path[n] = 0;

    while((dirp = readdir(dp)) != NULL)
    {
       if(strcmp(dirp->d_name,".") == 0 || strcmp(dirp->d_name,"..") == 0)
           continue;

       if(stat(dirp->d_name,&stbuf) < 0)
       {
             fprintf(stderr,"4:\t%s\n",strerror(errno));
             return false;
       }

       strcpy(path+n,dirp->d_name);

       if(S_ISDIR(stbuf.st_mode))
       {
           DIR *sdp =  opendir(dirp->d_name);

           if(mkdir(path,stbuf.st_mode) < 0)
           {
               fprintf(stderr,"5:\t%s\n",strerror(errno));
               return false;
           }

           cp_dir(sdp);

           closedir(sdp);
       }
       else
       {
           int cfd = open(dirp->d_name,O_RDONLY);
           int tfd = open(path,O_CREAT|O_EXCL|O_WRONLY,stbuf.st_mode);

           int n;

           while((n = read(cfd,buff,BLOCK)) > 0)
           {
                if(write(tfd,buff,n) < n)
                {
                    fprintf(stderr,"6:\t%s\n",strerror(errno));
                    break;
                }

           }

           close(cfd);
           close(tfd);

       }

    }

    chdir("..");
    path[n-1] = 0;
}

时间: 2024-08-01 17:06:08

linux下c语言实现递归复制文件夹的相关文章

PHP递归复制文件夹以及传输文件夹到其他服务器。

项目中需要复制整个文件夹,有时候还需要将整个文件夹传输到远程服务器. 这里就要递归遍历整个文件夹了,想看递归遍历文件夹的代码. function deepScanDir($dir) { $fileArr = array (); $dirArr = array (); $dir = rtrim($dir, '//'); if (is_dir($dir)) { $dirHandle = opendir($dir); while (false !== ($fileName = readdir($dir

归纳整理Linux下C语言常用的库函数----文件操作

在没有IDE的时候,记住一些常用的库函数的函数名.参数.基本用法及注意事项是很有必要的. 参照Linux_C_HS.chm的目录,我大致将常用的函数分为一下几类: 1. 内存及字符串控制及操作 2. 字符串转换 3. 字符测试 4. 文件操作 5. 时间日期 6. 常用数学函数 7. 文件内容操作 8. 文件权限控制 9. 进程操作 10. 线程操作 11. Socket操作 12. 信号处理 13. 数据结构及算法 这次主要总结的是上面黑色部分,关于文件操作的函数. 系统调用归类 * * 函数

C#递归复制文件夹

/// <param name="sources">原路徑</param> /// <param name="dest">目標路徑</param> private void CopyFile(string sources, string dest) { if (Directory.Exists(sources)) { DirectoryInfo dinfo = new DirectoryInfo(sources);//

Ubuntu下SVN命令行递归加入文件夹文件(免去一个一个的加入 --force)

因为在Linux下一直没有找到好的svn工具(类似于TortiseSVN的).当然eSVN这些也不错,但就是使用上认为还不是很习惯.终于还是选择了svn原始的命令行工具来进行版本号控制操作. 命令行的有些指令使用起来还是比較方便的.如svn checkout.svn commit这些命令基本上使不使用图形界面关系也不是非常大.唯一比較麻烦的命令的就是svn add,由于新添加的文件会分步在不同的文件夹中,有些文件夹中的文件已经存在于受控的svn资料夹,而有些文件所在的文件夹都是新增的.简单的输入

Linux下的C程序,遍历文件夹并统计其中各个类型文件所占百分比

递归遍历一个目录下的所有文件和文件夹,统计各个类型文件所占的百分比 程序代码a.cpp(编译命令:g++ a.cpp -o a) #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <unistd.h> #include <stdlib.h> #include <dirent.h> #include <string.h> stru

linux下rsync的配置以实现文件夹的同步

http://blog.csdn.net/cymm_liu/article/details/31347531 目标:把[88]_A主机的/data/home/web同步到[27]_B主机的/data/home/qy目录 B主机作为rsync服务器 #检查系统是否安装了rsync软件包[[email protected] home]# rpm -qa |greprsync                 rsync-2.6.9如果没有,就去yum 或者apt-get install或者去官网服务器

linux下删除3分钟之前指定文件夹下的指定类型文件

#!/bin/sh DELDIR="/root/crontab_test/test" #3 minute ago DELTIME="-mmin +3" #delete file type FILETYPE1="'*.txt'" FILETYPE2="'*.log'" #write to tmp file rm crontab_del_3min -f touch crontab_del_3min # every minute d

LINUX下C语言编程调用其他函数、链接头文件以及库文件

LINUX下C语言编程经常需要链接其他函数,而其他函数一般都放在另外.c文件中,或者打包放在一个库文件里面,我需要在main函数中调用这些函数,主要有如下几种方法: 1.当需要调用函数的个数比较少时,可以直接在main函数中包含该文件,比如一个文件夹下包含add.c和main.c文件: 方法一: 文件add.c定义两个整数相加的函数,code如下: #include <stdio.h> #include <math.h> int add(int a,int b) { int z;

LINUX下C语言编程基础

实验二 Linux下C语言编程基础 一.实验目的 1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用gdb调试技术 6. 熟悉makefile基本原理及语法规范 7. 掌握静态库和动态库的生成 二.实验步骤 1. 快捷键 Ubuntu中: 2. vim VIM是一个非常好的文本编辑器,很多专业程序员使用VIM编辑代码,即使以后你不编写程序,只要跟文本打交道,都应该学学VIM,可以浏览参考一下普通人