Recursive Copy Command

本文介绍了一个使用 C 语言实现的递归复制命令 (类似于 CP 命令带 -R 参数) 的源代码示例。该程序能够复制普通文件及目录,并支持错误处理。适用于系统编程课程的学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Recursive Copy Command
  2. (CP command with -R argument)
  3.  
  4. /*Course: system programming  *
  5.  *2008-11-24*
  6.  */
  7. #include <stdio.h>
  8. #include <dirent.h>
  9. #include <errno.h>
  10. #include <sys/stat.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <sys/types.h>
  14. #include <stdlib.h>
  15. #define BUFFER_SIZE 1048576
  16. /* copy regular file from src_path to dst_path*/
  17. void copy_file(char *src_path, char *dst_path)
  18. {
  19.         void copy_dir();
  20.         int from_fd;
  21.         int to_fd;
  22.         int bytes_read;
  23.         int bytes_write;
  24.         char buffer[BUFFER_SIZE];
  25.         char *p;
  26.         if((from_fd = open(src_path, O_RDONLY)) == -1) { /*open source file and error handler*/
  27.                 fprintf(stderr, "Open %s Error:%s/n", src_path, strerror(errno));
  28.                 exit(1);
  29.         }
  30.         if((to_fd = open(dst_path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) == -1) { /*create dst file and error handler*/
  31.                 fprintf(stderr,"Open %s Error:%s/n", dst_path, strerror(errno));
  32.                 exit(1);
  33.         }
  34.         while(bytes_read = read(from_fd, buffer, BUFFER_SIZE)) { /* read file buffer from source file */
  35.                 if((bytes_read == -1) && (errno != EINTR)) { /* can't read source file */
  36.                         fprintf(stderr,"read  %s Error:%s/n", src_path, strerror(errno));
  37.                         break;
  38.                 }
  39.                 else if(bytes_read > 0) {
  40.                         p = buffer;
  41.                         while(bytes_write = write(to_fd, p, bytes_read)) { /* write buffer into dst file */
  42.                                 if((bytes_write == -1) && (errno != EINTR)) {
  43.                                         fprintf(stderr,"write %s Error:%s/n", dst_path, strerror(errno));
  44.                                         break;
  45.                                 }
  46.                                 else if(bytes_write == bytes_read) /* for big file, read and write normally done */
  47.                                         break;
  48.                                 else if(bytes_write > 0) {
  49.                                         p += bytes_write;
  50.                                         bytes_read -= bytes_write;
  51.                                 }
  52.                         }
  53.                         if(bytes_write == -1) {
  54.                                 fprintf(stderr,"write %s Error:%s/n", dst_path, strerror(errno));
  55.                                 break;
  56.                         }
  57.                 }
  58.         }
  59.         close(from_fd);
  60.         close(to_fd);
  61. }
  62. void copy_dir(char *name, char *dst)
  63. {
  64.         DIR *dp;
  65.         DIR *dp1;
  66.         char pp[255];
  67.         char pn[255];
  68.         int i;
  69.         struct stat sbuf;
  70.         struct dirent *dir;
  71.         if((dp = opendir(name)) == NULL) { /* open src dir,here src is not only the input agrument */
  72.                 printf("Open Directory %s Error:%s/n", name, strerror(errno));
  73.                 exit(1);
  74.                 }
  75.         while ((dir = readdir(dp)) != NULL) { /* list all contents(file and dir) of dp dir*/
  76.                 if (dir->d_ino=0)  /* nothing exist */
  77.                         continue;
  78.                      /* get source path and dst path */
  79.                 strcpy(pn,name);
  80.                 strcat(pn,"/");
  81.                 strcat(pn,dir->d_name);
  82.                 strcpy(pp,dst);
  83.                 strcat(pp,"/");
  84.                 strcat(pp,dir->d_name);
  85.                 if (lstat(pn,&sbuf) < 0) { /*get stat of source */
  86.                         perror(pn);
  87.                         closedir(dp);
  88.                         exit(1);
  89.                 }
  90.                                        /* if source is a dir, recursive copying dir*/
  91.                 if ( ((sbuf.st_mode & S_IFMT) != S_IFLNK) &&
  92.                      ((sbuf.st_mode & S_IFMT) == S_IFDIR) &&
  93.                      (strcmp(dir->d_name, ".") != 0) &&
  94.                      (strcmp(dir->d_name, "..") != 0) ) {
  95.                         if((dp1 = opendir(pn)) == NULL) {
  96.                                 printf("Open Directory %s Error:%s/n",pn,strerror(errno));
  97.                                 exit(1);
  98.                         }
  99.                         if((mkdir(pp, 0700)))
  100.                                 break;
  101.                         copy_dir(pn,pp);
  102.                 }
  103.                                                /*if souce is a regular file ,copy it */
  104.                 else if ( ((sbuf.st_mode & S_IFMT) != S_IFLNK) &&
  105.                           ((sbuf.st_mode & S_IFMT) == S_IFREG) &&
  106.                           (strcmp(dir->d_name,".") != 0) &&
  107.                           (strcmp(dir->d_name, "..") != 0) ) {
  108.                            copy_file(pn,pp);
  109.                 }
  110.         }
  111. }
  112. int main(int argc,char **argv)
  113. {
  114.         char *program_name;
  115.         DIR *dp;
  116.          struct stat stat_src;
  117.         struct dirent *dir;
  118.         program_name = argv[0];
  119.         if(argc != 3) { /*argument check */
  120.                 fprintf(stderr,"Usage:%s fromdir todir/n", argv[0]);
  121.                 exit(1);
  122.                      }
  123.         
  124.          if (stat(argv[1], &stat_src) != 0 ){    
  125.                   printf ("Stat %s Error:%s/n", argv[1], strerror(errno));
  126.                   exit(1);
  127.                 }
  128.          if (S_ISREG(stat_src.st_mode)) /*source is a regular file */
  129.              copy_file(argv[1], argv[2]); /*do copy a regular file */
  130.            
  131.          else if(S_ISDIR(stat_src.st_mode))/* source is directory */
  132.                 {
  133.         if((dp = opendir(argv[1])) == NULL) { /*open src dir or error handler*/
  134.                 printf("Open Directory %s Error:%s/n", argv[1], strerror(errno));
  135.                 exit(1);
  136.                      }
  137.         if((mkdir(argv[2], 0777))) { /* make the dst dir */
  138.                 printf("makedir %s is failed /n", argv[2]);
  139.                 exit(1);
  140.                     }
  141.         copy_dir(argv[1], argv[2]); /* do copy a dir */
  142.     
  143.         closedir(dp);
  144.                 }
  145.         exit(0);
  146. }

linux安装kohya_ssWindows Permalink: Windows Windows Pre-requirements Permalink: Windows Pre-requirements To install the necessary dependencies on a Windows system, follow these steps: 1. Install Python 3.10.11. ◦ During the installation process, ensure that you select the option to add Python to the 'PATH' environment variable. 2. Install CUDA 11.8 toolkit. 3. Install Git. 4. Install the Visual Studio 2015, 2017, 2019, and 2022 redistributable. Setup Windows Permalink: Setup Windows To set up the project, follow these steps: 1. Open a terminal and navigate to the desired installation directory. 2. Clone the repository by running the following command: git clone --recursive https://github.com/bmaltais/kohya_ss.git Copy 3. Change into the kohya_ss directory: cd kohya_ss Copy 4. Run one of the following setup script by executing the following command: For systems with only python 3.10.11 installed: .\setup.bat Copy For systems with only more than one python release installed: .\setup-3.10.bat Copy During the accelerate config step, use the default values as proposed during the configuration unless you know your hardware demands otherwise. The amount of VRAM on your GPU does not impact the values used. Optional: CUDNN 8.9.6.50 Permalink: Optional: CUDNN 8.9.6.50 The following steps are optional but will improve the learning speed for owners of NVIDIA 30X0/40X0 GPUs. These steps enable larger training batch sizes and faster training speeds. 1. Run .\setup.bat and select 2. (Optional) Install cudnn files (if you want to use the latest supported cudnn version).最后这一步要怎么弄
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值