Загрузить файлы в «coreutils»

This commit is contained in:
8nlight 2023-10-14 17:18:08 +03:00
parent c42c6e7a3c
commit 4ad86cbbd4
2 changed files with 146 additions and 0 deletions

6
coreutils/clear.c Normal file
View File

@ -0,0 +1,6 @@
#include <stdio.h>
int main(void) {
puts("\033c");
return 0;
}

140
coreutils/cp.c Normal file
View File

@ -0,0 +1,140 @@
#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;
}