micro-utils/coreutils/cp.c

141 lines
2.5 KiB
C
Raw Normal View History

#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>
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 = malloc(strlen(dst) + strlen(bname) + 1);
if (!new_path)
return 1;
sprintf(new_path, "%s/%s", dst, bname);
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) {
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;
}
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;
size_t len = strlen(src) + strlen(ep->d_name) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {
fprintf(stderr, "rm: malloc() returned NULL\n");
return 1;
}
snprintf(full_path, len, "%s/%s", src, ep->d_name);
copy(full_path, dst);
free(full_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;
}