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

This commit is contained in:
8nlight 2023-10-04 20:00:50 +03:00
parent d8a1e77e63
commit 1da0bcfd58
4 changed files with 91 additions and 7 deletions

64
src/mkdir.c Normal file
View File

@ -0,0 +1,64 @@
#include <sys/stat.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int do_mkdir(const char *path) {
if (mkdir(path, 0777)) {
fprintf(stderr, "mkdir: %s %s\n", path, strerror(errno));
return 1;
}
return 0;
}
int do_parents(const char *path) {
if (path[0] == '.' || path[0] == '/')
return 0;
char *path2 = strdup(path);
if (!path2) {
fprintf(stderr, "mkdir: %s %s\n", path, strerror(errno));
return 1;
}
const char *par = dirname(path2);
do_parents(par);
do_mkdir(path2);
free(path2);
return 0;
}
int main(const int argc, const char **argv) {
unsigned int flag = 0;
int i;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-')
break;
else if (!strcmp(argv[i], "-p"))
flag = 1;
else if (!strcmp(argv[i], "-h")) {
printf("mkdir [-p (make parent dir)] [dir1 dir2...]");
return 0;
}
}
for (; i < argc; i++) {
if (flag) {
if (do_parents(argv[i]))
return 1;
}
else {
if (do_mkdir(argv[i]))
return 1;
}
}
return 0;
}

20
src/mkfifo.c Normal file
View File

@ -0,0 +1,20 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/stat.h>
int main(const int argc, const char **argv) {
if (argc == 1) {
printf("mkfifo: missing operand\n");
return 1;
}
for (int i = 1; i < argc; i++) {
if (mkfifo(argv[i], 0666)) {
fprintf(stderr, "mkfifo: %s %s\n", argv[i], strerror(errno));
return 1;
}
}
return 0;
}

View File

@ -8,7 +8,7 @@
int get_stat(const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
fprintf(stderr, "unable to stat %s: %s\n", path, strerror(errno));
fprintf(stderr, "rm: unable to stat %s: %s\n", path, strerror(errno));
return 1;
}
@ -21,7 +21,7 @@ int rmtree(const char *path) {
if (S_ISDIR(stat_path.st_mode) == 0) {
if (unlink(path) < 0) {
fprintf(stderr, "%s: is not directory\n", path);
fprintf(stderr, "rm: %s: is not directory\n", path);
return -1;
}
@ -30,7 +30,7 @@ int rmtree(const char *path) {
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "%s: Can`t open directory\n", path);
fprintf(stderr, "rm: %s: Can`t open directory\n", path);
return -1;
}
@ -41,7 +41,7 @@ int rmtree(const char *path) {
char *full_path = malloc(strlen(path) + strlen(ep->d_name) + 1);
if (full_path == NULL) {
fprintf(stderr, "malloc() returned NULL\n");
fprintf(stderr, "rm: malloc() returned NULL\n");
return 1;
}
@ -53,14 +53,14 @@ int rmtree(const char *path) {
closedir(dir);
if (rmdir(path) < 0)
fprintf(stderr, "%s: can`t remove a directory\n", path);
fprintf(stderr, "rm: %s: can`t remove a directory\n", path);
return 0;
}
int main(const int argc, char **argv) {
if (argc == 1) {
fprintf(stderr, "rm: missing operand\n");
printf("rm: missing operand\n");
return 1;
}

View File

@ -30,7 +30,7 @@ int touch(const char *path) {
int main(const int argc, const char **argv) {
if (argc == 1) {
fprintf(stderr, "touch: missing operand\n");
printf("touch: missing operand\n");
return 1;
}