简体   繁体   English

Linux cp命令实现将多个文件复制到目录

[英]Linux cp command implementation to copy multiple files to a Directory

I'm currently learning systems programming and came across an implementation of the Linux cp command in C. This implementation though from my understanding allows for copying the contents of one file to another file in the same directory and also copying a file into a Directory in the current directory. 我目前正在学习系统编程,遇到了C语言中Linux cp命令的实现。尽管从我的理解来看,该实现允许将一个文件的内容复制到同一目录中的另一个文件,也可以将文件复制到目录中的Directory中。当前目录。

How could you change this code to allow for multiple files to be copied to a directory at once (ie "copy f1.txt f2.txt f3.txt /dirInCurrentDir") or even ("copy d1/d2/d3/f1 d4/d5/d6/f2 d ") which would copy the 2 files to directory d. 您如何更改此代码以允许将多个文件一次复制到一个目录(即“ copy f1.txt f2.txt f3.txt / dirInCurrentDir”)甚至(“ copy d1 / d2 / d3 / f1 d4 / d5 / d6 / f2 d“),它将2个文件复制到目录d。 I know that the changes would have to occur in main() but how could you add to the if-else statement? 我知道必须在main()中进行更改,但是如何添加到if-else语句中呢?

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

#define BUFFERSIZE 1024
#define COPYMORE 0644

void oops(char *, char *);
int copyFiles(char *src, char *dest);
int dostat(char *filename);
int mode_isReg(struct stat info);


int main(int ac, char *av[])
{
  /* checks args */
  if(ac != 3)
  {
    fprintf(stderr, "usage: %s source destination\n", *av);
    exit(1);
  }


   char *src = av[1];
   char *dest = av[2];


   if( src[0] != '/' && dest[0] != '/' )//cp1 file1.txt file2.txt
   {
       copyFiles(src, dest);
   }
   else if( src[0] != '/' && dest[0] == '/' )//cp1 file1.txt /dir 
   {
      int i;
      for(i=1; i<=strlen(dest); i++)
      {
          dest[(i-1)] = dest[i];
      }
      strcat(dest, "/");
      strcat(dest, src);


      copyFiles(src, dest);
  }

  else
  {
      fprintf(stderr, "usage: cp1 source destination\n");
      exit(1);
  }
}



int dostat(char *filename)
{
    struct stat fileInfo;

    //printf("Next File %s\n", filename);
    if(stat(filename, &fileInfo) >=0)
    if(S_ISREG(fileInfo.st_mode))
    return 1;
    else return 0;

    return;
}




int copyFiles(char *source, char *destination)
{
  int in_fd, out_fd, n_chars;
  char buf[BUFFERSIZE];


  /* open files */
  if( (in_fd=open(source, O_RDONLY)) == -1 )
  {
    oops("Cannot open ", source);
  }


  if( (out_fd=creat(destination, COPYMORE)) == -1 )
  {
    oops("Cannot create ", destination);
  }


  /* copy files */
  while( (n_chars = read(in_fd, buf, BUFFERSIZE)) > 0 )
  {
    if( write(out_fd, buf, n_chars) != n_chars )
    {
      oops("Write error to ", destination);
    }


    if( n_chars == -1 )
    {
      oops("Read error from ", source);
    }
  }


    /* close files */
    if( close(in_fd) == -1 || close(out_fd) == -1 )
    {
      oops("Error closing files", "");
    }


    return 1;
}


  void oops(char *s1, char *s2)
  {
    fprintf(stderr, "Error: %s ", s1);
    perror(s2);
    exit(1);
  }

You would loop through all the argument values (from av[1] to av[ac - 2]) and copy it to the destination argument, which would be av[ac - 1]. 您将遍历所有参数值(从av [1]到av [ac-2]),并将其复制到目标参数,即av [ac-1]。

In your case, you would pass av[i] and av[ac - 1] to the copyFiles function, where i would be your loop index. 在您的情况下,您可以将av [i]和av [ac-1]传递给copyFiles函数,其中i是您的循环索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM