Linux,  Programming

[Linux] cp 명령어 구현

/*
 * cp       [src file]  [dest file]
 * argv[0]  argv[1]     argv[2]
 */

#include <stdio.h>
#define BUF_SIZE 1024

int main(int argc, char **argv) {
    FILE *src, *dst;
    int ret;
    char buffer[BUF_SIZE];
    if(argc != 3) {
        printf("ERROR\n");
        printf("Usage: cp [source] [destination]\n");
        return -1; 
    }   

    src = fopen(argv[1], "r");
    dst = fopen(argv[2], "w");

    while(ret = fread(buffer, 1, sizeof(buffer), src)) {
        fwrite(buffer, 1, ret, dst);
    }   
    fclose(src);
    fclose(dst);

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *