micro-utils/coreutils/cp.c
2023-10-21 17:16:34 +03:00

166 lines
3.1 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 <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_buffer(int mode, int ifd, int ofd, const char *dst) {
off_t len = lseek(ifd, 0, SEEK_END);
if (len < 0)
return 1;
else if (len == 0) {
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;
int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode);
if (ofd < 0) {
close(ifd);
return 1;
}
if (write_buffer(mode, ifd, ofd, dst))
fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno));
close(ifd);
close(ofd);
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) {
struct stat stat_path;
if (get_stat(src, &stat_path))
return 1;
if (S_ISDIR(stat_path.st_mode) == 0) {
if (copy(stat_path.st_mode, src, dst)) {
fprintf(stderr, "cp: %s: copy() failed (%s)\n", src, strerror(errno));
return 1;
}
return 0;
}
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;
}
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 = make_path(src, ep->d_name);
if (src_path == NULL)
continue;
char *dst_path = make_path(dst, ep->d_name);
if (dst_path == NULL) {
free(src_path);
continue;
}
cptree(src_path, dst_path);
free(src_path);
free(dst_path);
}
closedir(dir);
return 0;
}
int main(const int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "--help")) {
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++) {
char *new_path = make_path(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;
}