commit 3277417475572fb6cf9c7db71797f6cc2fee08d9
parent 548365d3b9a55bab7fafc5d91fda2dcc6219b5ef
Author: phoebos <ben@bvnf.space>
Date: Thu, 30 Sep 2021 21:36:24 +0100
mkdir: add
Diffstat:
2 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -6,6 +6,7 @@ BINS = \
ed \
false \
ls \
+ mkdir \
tee \
true \
wc \
diff --git a/mkdir.c b/mkdir.c
@@ -0,0 +1,46 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv) {
+ int c;
+ int flag_p = 0;
+ mode_t mode = 0755;
+ while ((c = getopt(argc, argv, "pm:")) != -1) {
+ switch (c) {
+ case 'p':
+ flag_p = 1;
+ break;
+ case 'm':
+ errno = 0;
+ mode = (mode_t) strtol(optarg, NULL, 8);
+ if (errno != 0) {
+ fprintf(stderr, "mkdir: %s: %s\n", optarg, strerror(errno));
+ return 1;
+ }
+ fprintf(stderr, "got mode = %d\n", mode);
+ break;
+ }
+ }
+ argc -= optind;
+ if (argc < 1) {
+ fprintf(stderr, "usage: %s file...\n", *argv);
+ return 1;
+ }
+ argv += optind - 1;
+
+ while (*++argv) {
+ if (mkdir(*argv, mode) == -1) {
+ if (errno == EEXIST && flag_p)
+ continue;
+ fprintf(stderr, "mkdir: %s: %s\n", *argv, strerror(errno));
+ return 1;
+ }
+ }
+
+ return 0;
+}