coreutils

custom mini coreutils
git clone git://git.evenfri.duckdns.org/coreutils.git
Log | Files | Refs | README

commit 8bfb2debce084249194437fd0c92e5be69111b73
parent 3769747bdd8693a69b921b7f72d4813756c7f568
Author: evenfri <evenfri256@gmail.com>
Date:   Tue, 11 Aug 2026 02:07:40 +0800

add mkdir

Diffstat:
Amkdir.c | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmkfile | 1+
2 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/mkdir.c b/mkdir.c @@ -0,0 +1,82 @@ +#include <libc.h> + +#define eexist 17 + +int has_p = 0; + +size_t strspn(const char *s1, const char *s2) { + const char *p = s1, *spanp; + char c, sc; + +cont: + c = *p++; + for (spanp = s2; (sc = *spanp++) != 0;) + if (sc == c) + goto cont; + + return (p - 1 - s1); +} + +size_t strcspn(const char *s1, const char *s2) { + const char *p, *spanp; + char c, sc; + + for (p = s1;;) { + c = *p++; + spanp = s2; + do { + if ((sc = *spanp++) == c) + return (p - 1 - s1); + } while (sc != 0); + } + return 0; +} + +int main(int argc, char *argv[]) { + if (argc < 2) { + write(stdout, "using: mkdir [options] <path>\n", 30); + return 1; + } + + + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-p") == 0) { + has_p = 1; + continue; + } + + char *path = argv[i]; + + if (has_p) { + char *slash = path; + + for (;;) { + slash += strspn(slash, "/"); + slash += strcspn(slash, "/"); + + if (*slash == '\0') { + if (*path != '\0') { + int r = mkdir(path, 0777); + if (r < 0 && errno != eexist) + return errno; + } + break; + } + *slash = '\0'; + if (*path != '\0') { + int r = mkdir(path, 0777); + if (r < 0 && errno != eexist) + return errno; + } + *slash = '/'; + slash++; + } + } else { + int r = mkdir(path, 0777); + if (r < 0 && errno != eexist) + return errno; + } + } + + return 0; +} diff --git a/mkfile b/mkfile @@ -14,6 +14,7 @@ targ = init \ free \ sh \ touch \ + mkdir \ <$PLAN9/src/mkmany