diff options
author | Georgi Chorbadzhiyski <georgi@unixsol.org> | 2012-03-05 22:19:36 -0600 |
---|---|---|
committer | Georgi Chorbadzhiyski <georgi@unixsol.org> | 2012-03-05 22:19:36 -0600 |
commit | 057aa04da8b39a72e38eedddb9ed27e61b25ed68 (patch) | |
tree | f3bd6909899753a0d1fbc87d09f3ec9bb034de1b /toys | |
parent | 4250d52c84c8055525b14ce3687bffc52d6300ad (diff) | |
download | toybox-057aa04da8b39a72e38eedddb9ed27e61b25ed68.tar.gz |
Add mkdir.
Diffstat (limited to 'toys')
-rw-r--r-- | toys/mkdir.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/toys/mkdir.c b/toys/mkdir.c new file mode 100644 index 00000000..4c9f5451 --- /dev/null +++ b/toys/mkdir.c @@ -0,0 +1,68 @@ +/* vi: set sw=4 ts=4: + * + * mkdir.c - Make directories + * + * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> + * + * See http://pubs.opengroup.org/onlinepubs/009695399/utilities/mkdir.html + * + * TODO: Add -m + +USE_MKDIR(NEWTOY(mkdir, "<1p", TOYFLAG_BIN)) + +config MKDIR + bool "mkdir" + default y + help + usage: mkdir [-p] [dirname...] + Create one or more directories. + + -p make parent directories as needed. +*/ + +#include "toys.h" + + +static int create_dir(const char *dir, mode_t mode) { + int ret = 0; + unsigned int i; + + // Shortcut + if (strchr(dir, '/') == NULL || !toys.optflags) + return mkdir(dir, mode); + + char *d = strdup(dir); + if (!d) + return -1; + unsigned int dlen = strlen(dir); + + // Skip first char (it can be /) + for (i = 1; i < dlen; i++) { + if (d[i] != '/') + continue; + d[i] = '\0'; + ret = mkdir(d, mode); + d[i] = '/'; + if (ret < 0 && errno != EEXIST) + goto OUT; + } + ret = mkdir(d, mode); +OUT: + free(d); + return ret; +} + +void mkdir_main(void) +{ + char **s; + mode_t umask_val = umask(0); + mode_t dir_mode = (0777 & ~umask_val) | (S_IWUSR | S_IXUSR); + umask(umask_val); + + for (s=toys.optargs; *s; s++) { + if (create_dir(*s, dir_mode) != 0) { + fprintf(stderr, "mkdir: cannot create directory '%s': %s\n", *s, strerror(errno)); + toys.exitval = 1; + } + } +} |