This commit is contained in:
Your Name 2023-10-17 15:37:41 +03:00
parent 07742fcdb9
commit ae75be2088

View File

@ -21,9 +21,18 @@ char *make_path(const char *src, const char *dst) {
return full_path;
}
int write_buffer(int ifd, int ofd) {
char buf[4096];
size_t bytes = 0;
while ((bytes = read(ifd, buf, sizeof(buf))) > 0)
if (write(ofd, buf, bytes) != bytes)
return 1;
int write_to_file(const char *src, const char *dst) {
int ret = 0;
return 0;
}
int copy(const char *src, const char *dst) {
int ifd = open(src, O_RDONLY);
if (ifd < 0)
return 1;
@ -34,55 +43,11 @@ int write_to_file(const char *src, const char *dst) {
return 1;
}
off_t len = lseek(ifd, 0, SEEK_END);
if (len <= 0)
goto EXIT;
if (write_buffer(ifd, ofd))
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
void *buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, ifd, 0);
if (buf == MAP_FAILED) {
ret = 1;
goto EXIT;
}
write(ofd, buf, len);
if (munmap(buf, len)) {
ret = 0;
goto EXIT;
}
EXIT:
close(ifd);
close(ofd);
return ret;
}
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;
}
@ -99,6 +64,7 @@ int cptree(const char *src, const char *dst) {
struct stat stat_path;
get_stat(src, &stat_path);
printf("%s %s\n", src, dst);
if (S_ISDIR(stat_path.st_mode) == 0) {
if (copy(src, dst)) {
fprintf(stderr, "cp: %s: copy() failed (%s)\n", src, strerror(errno));
@ -108,17 +74,14 @@ int cptree(const char *src, const char *dst) {
return 0;
}
else {
if (mkdir(dst, 0777) < 0) {
fprintf(stderr, "cp: %s\n", strerror(errno));
return 1;
}
else if (mkdir(dst, 0777) < 0) {
fprintf(stderr, "cp: %s\n", strerror(errno));
}
DIR *dir = opendir(src);
if (dir == NULL) {
fprintf(stderr, "cp: %s: Can`t open directory\n", src);
return -1;
return 1;
}
struct dirent *ep;