161 lines
2.8 KiB
C
161 lines
2.8 KiB
C
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <libgen.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <dirent.h>
|
|
#include <sys/stat.h>
|
|
|
|
char *make_path(const char *src, const char *dst) {
|
|
size_t len = strlen(src) + strlen(dst) + 2;
|
|
char *full_path = malloc(len + 1);
|
|
if (full_path == NULL) {
|
|
fprintf(stderr, "cp: malloc() returned NULL\n");
|
|
return NULL;
|
|
}
|
|
|
|
snprintf(full_path, len, "%s/%s", src, dst);
|
|
return full_path;
|
|
}
|
|
|
|
|
|
int write_to_file(const char *src, const char *dst) {
|
|
int ifd = open(src, O_RDONLY);
|
|
if (ifd < 0)
|
|
return 1;
|
|
|
|
int ofd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
|
if (ofd < 0) {
|
|
close(ifd);
|
|
return 1;
|
|
}
|
|
|
|
char buf[4096];
|
|
ssize_t len = 0;
|
|
while ((len = read(ifd, buf, sizeof(buf))))
|
|
write(ofd, buf, len);
|
|
|
|
close(ifd);
|
|
close(ofd);
|
|
return 0;
|
|
}
|
|
|
|
int copyf(const char *src, const char *dst) {
|
|
char *copy = strdup(src);
|
|
if (!copy)
|
|
return 1;
|
|
|
|
char *bname = basename(copy);
|
|
char *new_path = make_path(bname, dst);
|
|
if (new_path == NULL)
|
|
return 1;
|
|
|
|
int ret = write_to_file(src, new_path);
|
|
|
|
free(new_path);
|
|
free(copy);
|
|
|
|
return ret;
|
|
}
|
|
|
|
int copy(const char *src, const char *dst){
|
|
if (write_to_file(src, dst)) {
|
|
if (copyf(src, dst)) {
|
|
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int get_stat(const char *path, struct stat *stat_path) {
|
|
if (stat(path, stat_path)) {
|
|
fprintf(stderr, "cp: unable to stat %s: %s\n", path, strerror(errno));
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int cptree(const char *src, const char *dst) {
|
|
printf("%s %s\n", src, dst);
|
|
|
|
struct stat stat_path;
|
|
get_stat(src, &stat_path);
|
|
|
|
if (S_ISDIR(stat_path.st_mode) == 0) {
|
|
if (copy(src, dst)) {
|
|
fprintf(stderr, "cp: %s: is not directory\n", src);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
else {
|
|
if (mkdir(dst, 0777) < 0) {
|
|
fprintf(stderr, "cp: %s\n", strerror(errno));
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
DIR *dir = opendir(src);
|
|
if (dir == NULL) {
|
|
fprintf(stderr, "cp: %s: Can`t open directory\n", src);
|
|
return -1;
|
|
}
|
|
|
|
struct dirent *ep;
|
|
while ((ep = readdir(dir)) != NULL) {
|
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, ".."))
|
|
continue;
|
|
|
|
char *src_path = make_path(src, ep->d_name);
|
|
if (src_path == NULL)
|
|
return 1;
|
|
|
|
char *dst_path = make_path(dst, ep->d_name);
|
|
if (dst_path == NULL)
|
|
return 1;
|
|
|
|
cptree(src_path, dst_path);
|
|
|
|
free(src_path);
|
|
free(dst_path);
|
|
}
|
|
|
|
closedir(dir);
|
|
return 0;
|
|
}
|
|
|
|
int main(const int argc, const char **argv) {
|
|
/* Recursion */
|
|
unsigned int r_flag;
|
|
|
|
int i;
|
|
for (i = 1; i < argc; i++) {
|
|
if (argv[i][0] != '-')
|
|
break;
|
|
|
|
else if (!strcmp(argv[i], "-r"))
|
|
r_flag = 1;
|
|
|
|
else if (!strcmp(argv[i], "-h")) {
|
|
printf("cp [-r] [Src] [Dst]\n");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
if (argc - i >= 2) {
|
|
if (r_flag)
|
|
return cptree(argv[i], argv[argc - 1]);
|
|
else
|
|
return copy(argv[i], argv[argc - 1]);
|
|
}
|
|
|
|
return 0;
|
|
}
|