aboutsummaryrefslogtreecommitdiffstats
path: root/toolbox
diff options
context:
space:
mode:
authorAnthony Newnam <anthony.newnam@garmin.com>2010-07-06 18:18:35 -0500
committerAnthony Newnam <anthony.newnam@garmin.com>2010-07-14 13:29:42 -0500
commitb55de6798507178dc7a50570108b435afa9a8346 (patch)
treee981a055a3689648414329237b39ee724f259000 /toolbox
parent230cb33fd1ab335c6f808c72db891993b00110a0 (diff)
downloadsystem_core-b55de6798507178dc7a50570108b435afa9a8346.tar.gz
system_core-b55de6798507178dc7a50570108b435afa9a8346.tar.bz2
system_core-b55de6798507178dc7a50570108b435afa9a8346.zip
Add -p support to mkdir
Change-Id: Ia0298ccc6b476d6bd6756471c05ef2a345312734
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/mkdir.c61
1 files changed, 55 insertions, 6 deletions
diff --git a/toolbox/mkdir.c b/toolbox/mkdir.c
index 121adab2..656970a7 100644
--- a/toolbox/mkdir.c
+++ b/toolbox/mkdir.c
@@ -2,10 +2,14 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
+#include <sys/limits.h>
+#include <sys/stat.h>
static int usage()
{
- fprintf(stderr,"mkdir <target>\n");
+ fprintf(stderr,"mkdir [OPTION] <target>\n");
+ fprintf(stderr," --help display usage and exit\n");
+ fprintf(stderr," -p, --parents create parent directories as needed\n");
return -1;
}
@@ -13,15 +17,60 @@ int mkdir_main(int argc, char *argv[])
{
int symbolic = 0;
int ret;
- if(argc < 2) return usage();
+ if(argc < 2 || strcmp(argv[1], "--help") == 0) {
+ return usage();
+ }
+
+ int recursive = (strcmp(argv[1], "-p") == 0 ||
+ strcmp(argv[1], "--parents") == 0) ? 1 : 0;
+
+ if(recursive && argc < 3) {
+ // -p specified without a path
+ return usage();
+ }
+
+ if(recursive) {
+ argc--;
+ argv++;
+ }
+
+ char currpath[PATH_MAX], *pathpiece;
+ struct stat st;
while(argc > 1) {
argc--;
argv++;
- ret = mkdir(argv[0], 0777);
- if(ret < 0) {
- fprintf(stderr, "mkdir failed for %s, %s\n", argv[0], strerror(errno));
- return ret;
+ if(recursive) {
+ // reset path
+ strcpy(currpath, "");
+ // create the pieces of the path along the way
+ pathpiece = strtok(argv[0], "/");
+ if(argv[0][0] == '/') {
+ // prepend / if needed
+ strcat(currpath, "/");
+ }
+ while(pathpiece != NULL) {
+ if(strlen(currpath) + strlen(pathpiece) + 2/*NUL and slash*/ > PATH_MAX) {
+ fprintf(stderr, "Invalid path specified: too long\n");
+ return 1;
+ }
+ strcat(currpath, pathpiece);
+ strcat(currpath, "/");
+ if(stat(currpath, &st) != 0) {
+ ret = mkdir(currpath, 0777);
+ if(ret < 0) {
+ fprintf(stderr, "mkdir failed for %s, %s\n", currpath, strerror(errno));
+ return ret;
+ }
+ }
+ pathpiece = strtok(NULL, "/");
+ }
+ } else {
+ ret = mkdir(argv[0], 0777);
+ if(ret < 0) {
+ fprintf(stderr, "mkdir failed for %s, %s\n", argv[0], strerror(errno));
+ return ret;
+ }
}
}