micro-utils/src/coreutils/cp/cp.c

214 lines
3.7 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/mman.h>
#include "make_path.h"
#include "get_stat.h"
char *f_flag = "cp";
char r_flag;
int write_buffer(int mode, int ifd, int ofd, const char *dst) {
off_t len = lseek(ifd, 0, SEEK_END);
if (len <= 0) {
if (len < 0)
return 1;
int fd = open(dst, O_CREAT | O_RDONLY, mode);
if (fd < 0)
return 1;
close(fd);
return 0;
}
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;
return 0;
}
int copy(int mode, const char *src, const char *dst) {
int ifd = open(src, O_RDONLY);
if (ifd < 0)
return 1;
/* test ./testdir/ -> test ./testdir/test */
char *dup = NULL;
char *new_path = (char *)dst;
int flag = 0;
int ret = 0;
int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode);
if (ofd < 0) {
ret = 1;
dup = strdup(src);
if (dup == NULL)
goto CLOSE;
flag = 1;
new_path = NULL;
new_path = mu_make_path(f_flag, dst, basename(dup));
if (new_path == NULL)
goto CLOSE;
if (!strcmp(new_path, src) || !strcmp(new_path + 2, src))
goto CLOSE;
ofd = open(new_path, O_CREAT | O_TRUNC | O_RDWR, mode);
if (ofd < 0)
goto CLOSE;
ret = 0;
}
if (write_buffer(mode, ifd, ofd, new_path))
if (f_flag)
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
close(ofd);
CLOSE:
close(ifd);
if (flag) {
if (dup != NULL)
free(dup);
if (new_path != NULL)
free(new_path);
}
return ret;
}
int cptree(const char *src, const char *dst) {
struct stat stat_path;
if (mu_get_lstat(f_flag, src, &stat_path))
return 1;
if (!S_ISDIR(stat_path.st_mode)) {
if (copy(stat_path.st_mode, src, dst)) {
if (f_flag)
fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno));
return 1;
}
return 0;
}
else if (!r_flag)
return 0;
else if (mkdir(dst, 0777) < 0)
if (f_flag)
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;
}
int ret = 0;
struct dirent *ep;
while ((ep = readdir(dir)) != NULL) {
if (!strcmp(ep->d_name, ".") || !strcmp(ep->d_name, "..") || !strcmp(dst, ep->d_name))
continue;
char *src_path = mu_make_path(f_flag, src, ep->d_name);
if (src_path == NULL)
continue;
char *dst_path = mu_make_path(f_flag, dst, ep->d_name);
if (dst_path == NULL) {
free(src_path);
continue;
}
if (cptree(src_path, dst_path))
ret = 1;
free(src_path);
free(dst_path);
}
closedir(dir);
return ret;
}
int main(int argc, char **argv) {
int opt;
while ((opt = getopt(argc, argv, "frR")) != -1) {
switch (opt) {
case 'f':
f_flag = NULL;
break;
case 'r':
r_flag = 1;
break;
default:
printf("cp [rf] [src1 src2...] [dst]\n\t-f Silent\n\t-r Recursive\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc < 2) {
fprintf(stderr, "cp: missing operand\n");
return 1;
}
int ret = 0;
if (!access(argv[argc - 1], F_OK))
goto IF_EXSIST;
if (argc == 2)
ret = cptree(argv[0], argv[argc - 1]);
else {
IF_EXSIST:
for (int i = 0; i < argc - 1; i++) {
char *new_path = mu_make_path(f_flag, argv[argc - 1], basename(argv[i]));
if (new_path == NULL)
return 1;
if (cptree(argv[i], new_path))
ret = 1;
free(new_path);
}
}
return ret;
}