2017-05-20 6 views
0

현재 시스템 프로그래밍을 배우고 있으며 C에서 Linux cp 명령을 구현했습니다.이 구현을 통해 이해할 수있는 한 파일의 내용을 다른 파일로 복사 할 수 있습니다 파일을 같은 디렉토리에 복사하고 파일을 현재 디렉토리의 디렉토리에 복사합니다.여러 파일을 디렉토리에 복사하는 Linux cp 명령 구현

여러 파일을 한 번에 디렉토리에 복사 할 수 있도록이 코드를 어떻게 바꿀 수 있습니까 (예 : "copy f1.txt f2.txt f3.txt/dirInCurrentDir") 또는 "copy d1/d2/d3/f1 d4/d5/d6/f2 d ")를 사용하여 두 파일을 디렉토리 d에 복사합니다. 변경 사항이 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); 
    } 

답변

0

모든 인수 값을 통해 루프 (AV으로부터 [1] [교류 - 2 AV하는) 것이며,이 AV 될 대상 인수 복사 [AC - 1].

귀하의 경우 av [i]와 av [ac - 1]을 copyFiles 함수에 전달합니다. 여기에서 루프 색인이됩니다.

+0

그래서이 else if 문을 다른 것으로 바꾸었고 한 파일의 내용을 다른 파일로 복사하거나 파일을 dirInCurrentDirectory로 복사하는 경우에 작동합니다. 파일을 dir에 복사하려면 (./cp2 f1.txt d/f1.txt) 그래야합니다. 그렇지 않으면 메신저 여전히 디렉토리에 mutiple 파일을 복사 할 수없는 꽤 꽤 철수가 어떻게 반복에서 그것의 결함. 여기 내 루프'else if (src [0]! = '/'&& dest [0] == '/') { \t int i; (i = 1; i == strlen (src); i ++) { \tav [ac-2] = av [ac-1]에 대한 \t; \t} \t strcat (dest, "/"); \t strcat (dest, av [i]); \t copyFile (av [i], av [ac-1]); }' – Kim94