Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <unistd.h>
- void copy_file(char *origin, char *dest)
- {
- FILE *input = fopen(origin, "r");
- FILE *output = fopen(dest, "w");
- char content[256];
- while (fgets(content, sizeof(content), input) != NULL)
- fputs(content, output);
- }
- void copy_directory(char origin[], char dest[])
- {
- DIR *directory;
- struct dirent *contents = NULL;
- struct stat dir = {0};
- short i;
- if ((directory = opendir(origin)) == NULL) {
- fprintf(stderr, "couldn't open directory\n");
- exit(-1);
- }
- if (stat(dest, &dir) != -1)
- chdir(dest);
- else {
- mkdir(dest, 0777);
- chdir(dest);
- }
- for (i = 1; i < strlen(contents->d_name); i++) {
- while ((contents = readdir(directory)) != NULL)
- copy_file(&contents->d_name[i], dest);
- }
- closedir(directory);
- }
- int main(int argc, char *argv[])
- {
- if (argc < 2) {
- fprintf(stderr, "Usage: cp [-r] origin dest\n");
- exit(-1);
- }
- else {
- if (strncmp(argv[1], "-r", 2) == 0)
- copy_directory(argv[2], argv[3]);
- else
- copy_file(argv[1], argv[2]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement