From afd96cc52e5c1ec955527cce5c33b4cd1ad0d8f9 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 3 Aug 2019 09:32:33 -0400 Subject: support -p, but it needs work --- mkdir.c | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/mkdir.c b/mkdir.c index 1d43b1c..9ae2dca 100644 --- a/mkdir.c +++ b/mkdir.c @@ -22,9 +22,10 @@ * SOFTWARE. */ -#define _POSIX_C_SOURCE 2 +#define _XOPEN_SOURCE 500 #include #include +#include #include #include #include @@ -40,8 +41,26 @@ static int translate_mode(const char *s) static int mk_dir(char *path, int mode, int flags) { - /* TODO: parents */ + if (flags & PARENTS) { + char parent[strlen(path) + 1]; + strcpy(parent, path); + char *p = dirname(parent); + if (!strcmp(p, "/") || !strcmp(p, ".") || !strcmp(p, "..")) { + goto MKDIR; + } + + struct stat st; + if (stat(p, &st) == 0 && !S_ISDIR(st.st_mode)) { + fprintf(stderr, "mkdir: %s: %s\n", path, strerror(EEXIST)); + return 1; + } + + if (mk_dir(p, mode, flags) != 0) { + return 1; + } + } + MKDIR: if (mkdir(path, mode) != 0) { fprintf(stderr, "mkdir: %s: %s\n", path, strerror(errno)); return 1; @@ -63,7 +82,7 @@ int main(int argc, char *argv[]) int flags = 0; int c; - while ((c = getopt(argc, argv, "mp:")) != -1) { + while ((c = getopt(argc, argv, "m:p")) != -1) { switch (c) { case 'm': mode = translate_mode(optarg); -- cgit v1.2.1