summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-03 09:32:33 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-03 09:32:33 -0400
commitafd96cc52e5c1ec955527cce5c33b4cd1ad0d8f9 (patch)
tree06b3799849a33ba5bf390d640774ce58ac060450
parentf39f1caeea5c63f6d1b4d35f52bb5bafeb4d67d4 (diff)
support -p, but it needs work
-rw-r--r--mkdir.c25
1 files 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 <errno.h>
#include <locale.h>
+#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@@ -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);