2023-10-14 17:18:08 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2023-10-17 13:50:40 +03:00
|
|
|
#include <sys/mman.h>
|
2023-10-31 12:53:32 +03:00
|
|
|
#include "make_path.h"
|
|
|
|
#include "get_stat.h"
|
2023-10-16 20:21:47 +03:00
|
|
|
|
2023-10-21 17:16:34 +03:00
|
|
|
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
|
2023-10-17 19:45:31 +03:00
|
|
|
off_t len = lseek(ifd, 0, SEEK_END);
|
2023-10-24 21:25:29 +03:00
|
|
|
if (len <= 0) {
|
|
|
|
if (len < 0)
|
|
|
|
return 1;
|
2023-10-17 19:45:31 +03:00
|
|
|
|
2023-10-21 17:16:34 +03:00
|
|
|
int fd = open(dst, O_CREAT | O_RDONLY, mode);
|
2023-10-18 21:44:56 +03:00
|
|
|
if (fd < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:45:31 +03:00
|
|
|
if (ftruncate(ofd, len) < 0)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
void *buf_in = mmap(NULL, len, PROT_READ, MAP_SHARED, ifd, 0);
|
|
|
|
if (buf_in == MAP_FAILED)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
void *buf_out = mmap(NULL, len, PROT_WRITE, MAP_SHARED, ofd, 0);
|
|
|
|
if (buf_out == MAP_FAILED)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
memcpy(buf_out, buf_in, len);
|
|
|
|
|
|
|
|
if (munmap(buf_in, len) || munmap(buf_out, len))
|
|
|
|
return 1;
|
2023-10-17 15:37:41 +03:00
|
|
|
|
|
|
|
return 0;
|
2023-10-16 20:21:47 +03:00
|
|
|
|
2023-10-17 15:37:41 +03:00
|
|
|
}
|
|
|
|
|
2023-10-21 17:16:34 +03:00
|
|
|
int copy(int mode, const char *src, const char *dst) {
|
2023-10-14 17:18:08 +03:00
|
|
|
int ifd = open(src, O_RDONLY);
|
|
|
|
if (ifd < 0)
|
|
|
|
return 1;
|
|
|
|
|
2023-10-24 21:25:29 +03:00
|
|
|
/* test ./testdir/ -> test ./testdir/test */
|
|
|
|
char *dup = NULL;
|
|
|
|
char *new_path = (char *)dst;
|
|
|
|
int flag = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-10-21 17:16:34 +03:00
|
|
|
int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode);
|
2023-10-14 17:18:08 +03:00
|
|
|
if (ofd < 0) {
|
2023-10-24 21:25:29 +03:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
dup = strdup(src);
|
|
|
|
if (dup == NULL)
|
|
|
|
goto CLOSE;
|
|
|
|
|
|
|
|
flag = 1;
|
|
|
|
new_path = NULL;
|
|
|
|
|
2023-10-31 12:53:32 +03:00
|
|
|
new_path = mu_make_path("cp", dst, basename(dup));
|
2023-10-24 21:25:29 +03:00
|
|
|
if (new_path == NULL)
|
|
|
|
goto CLOSE;
|
|
|
|
|
2023-10-24 21:40:36 +03:00
|
|
|
if (!strcmp(new_path, src) || !strcmp(new_path + 2, src))
|
|
|
|
goto CLOSE;
|
|
|
|
|
2023-10-24 21:25:29 +03:00
|
|
|
ofd = open(new_path, O_CREAT | O_TRUNC | O_RDWR, mode);
|
|
|
|
if (ofd < 0)
|
|
|
|
goto CLOSE;
|
|
|
|
|
|
|
|
ret = 0;
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
2023-10-24 21:25:29 +03:00
|
|
|
if (write_buffer(mode, ifd, ofd, new_path))
|
2023-10-17 15:37:41 +03:00
|
|
|
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
|
2023-10-17 13:50:40 +03:00
|
|
|
|
2023-10-24 21:25:29 +03:00
|
|
|
|
2023-10-14 17:18:08 +03:00
|
|
|
close(ofd);
|
2023-10-24 21:25:29 +03:00
|
|
|
|
|
|
|
CLOSE:
|
|
|
|
close(ifd);
|
|
|
|
if (flag) {
|
|
|
|
if (dup != NULL)
|
|
|
|
free(dup);
|
|
|
|
|
|
|
|
if (new_path != NULL)
|
|
|
|
free(new_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int cptree(const char *src, const char *dst) {
|
|
|
|
struct stat stat_path;
|
2023-10-31 12:53:32 +03:00
|
|
|
if (mu_get_lstat("cp", src, &stat_path))
|
2023-10-17 19:45:31 +03:00
|
|
|
return 1;
|
2023-10-14 17:18:08 +03:00
|
|
|
|
2023-10-24 21:25:29 +03:00
|
|
|
if (!S_ISDIR(stat_path.st_mode)) {
|
2023-10-21 17:16:34 +03:00
|
|
|
if (copy(stat_path.st_mode, src, dst)) {
|
2023-10-24 21:25:29 +03:00
|
|
|
fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno));
|
2023-10-16 20:25:42 +03:00
|
|
|
return 1;
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-17 19:45:31 +03:00
|
|
|
else if (mkdir(dst, 0777) < 0)
|
2023-10-17 15:37:41 +03:00
|
|
|
fprintf(stderr, "cp: %s\n", strerror(errno));
|
2023-10-16 20:21:47 +03:00
|
|
|
|
2023-10-14 17:18:08 +03:00
|
|
|
DIR *dir = opendir(src);
|
|
|
|
if (dir == NULL) {
|
|
|
|
fprintf(stderr, "cp: %s: Can`t open directory\n", src);
|
2023-10-17 15:37:41 +03:00
|
|
|
return 1;
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
2023-11-01 14:17:25 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
2023-10-14 17:18:08 +03:00
|
|
|
struct dirent *ep;
|
|
|
|
while ((ep = readdir(dir)) != NULL) {
|
2023-10-17 20:27:56 +03:00
|
|
|
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name))
|
2023-10-14 17:18:08 +03:00
|
|
|
continue;
|
|
|
|
|
2023-10-31 12:53:32 +03:00
|
|
|
char *src_path = mu_make_path("cp", src, ep->d_name);
|
2023-10-16 20:21:47 +03:00
|
|
|
if (src_path == NULL)
|
2023-10-21 15:05:49 +03:00
|
|
|
continue;
|
2023-10-16 20:21:47 +03:00
|
|
|
|
2023-10-31 12:53:32 +03:00
|
|
|
char *dst_path = mu_make_path("cp", dst, ep->d_name);
|
2023-10-21 15:05:49 +03:00
|
|
|
if (dst_path == NULL) {
|
|
|
|
free(src_path);
|
|
|
|
continue;
|
|
|
|
}
|
2023-10-14 17:18:08 +03:00
|
|
|
|
2023-11-01 14:17:25 +03:00
|
|
|
if (cptree(src_path, dst_path))
|
|
|
|
ret = 1;
|
2023-10-14 17:18:08 +03:00
|
|
|
|
2023-10-16 20:21:47 +03:00
|
|
|
free(src_path);
|
|
|
|
free(dst_path);
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dir);
|
2023-11-01 14:17:25 +03:00
|
|
|
return ret;
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
2023-10-20 17:32:50 +03:00
|
|
|
int main(const int argc, char **argv) {
|
2023-10-17 19:45:31 +03:00
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] != '-')
|
|
|
|
break;
|
|
|
|
|
2023-10-21 15:05:49 +03:00
|
|
|
else if (!strcmp(argv[i], "--help")) {
|
2023-10-17 19:45:31 +03:00
|
|
|
printf("cp [Src] [Dst]\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret = 0;
|
|
|
|
if (argc - i == 2)
|
|
|
|
ret = cptree(argv[i], argv[argc - 1]);
|
|
|
|
|
|
|
|
else {
|
|
|
|
for (; i < argc - 1; i++) {
|
2023-10-31 12:53:32 +03:00
|
|
|
char *new_path = mu_make_path("cp", argv[argc - 1], basename(argv[i]));
|
2023-10-17 19:45:31 +03:00
|
|
|
if (new_path == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2023-10-17 20:32:07 +03:00
|
|
|
if (cptree(argv[i], new_path))
|
2023-10-17 19:45:31 +03:00
|
|
|
ret = 1;
|
|
|
|
|
|
|
|
free(new_path);
|
|
|
|
}
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|
|
|
|
|
2023-10-17 19:45:31 +03:00
|
|
|
return ret;
|
2023-10-14 17:18:08 +03:00
|
|
|
}
|