diff options
author | Sascha Hauer <sha@octopus.labnet.pengutronix.de> | 2007-09-27 11:59:18 +0200 |
---|---|---|
committer | Sascha Hauer <sha@octopus.labnet.pengutronix.de> | 2007-09-27 11:59:18 +0200 |
commit | 46d570539510929210511b16b066c82ffe737c0a (patch) | |
tree | c933db29e37a0ab00140a54bb67f3baeb723dc7d /lib | |
parent | 2d02f7c0f007153ce883b9ab100b771c2b1bed65 (diff) | |
download | barebox-46d570539510929210511b16b066c82ffe737c0a.tar.gz barebox-46d570539510929210511b16b066c82ffe737c0a.tar.xz |
implement mkdir -p
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/make_directory.c | 54 |
2 files changed, 55 insertions, 0 deletions
diff --git a/lib/Makefile b/lib/Makefile index 2ee3d20c18..423e49a663 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -14,6 +14,7 @@ obj-y += kfifo.o obj-y += libbb.o obj-y += libgen.o obj-y += recursive_action.o +obj-y += make_directory.o obj-$(CONFIG_BZLIB) += bzlib.o bzlib_crctable.o bzlib_decompress.o bzlib_huffman.o bzlib_randtable.o obj-$(CONFIG_ZLIB) += zlib.o gunzip.o obj-$(CONFIG_CRC32) += crc32.o diff --git a/lib/make_directory.c b/lib/make_directory.c new file mode 100644 index 0000000000..cf02e0bff2 --- /dev/null +++ b/lib/make_directory.c @@ -0,0 +1,54 @@ + +#include <string.h> +#include <errno.h> +#ifdef __U_BOOT__ +#include <fs.h> +#include <malloc.h> +#endif + +int make_directory(const char *dir) +{ + char *s = strdup(dir); + char *path = s; + char c; + + do { + c = 0; + + /* Bypass leading non-'/'s and then subsequent '/'s. */ + while (*s) { + if (*s == '/') { + do { + ++s; + } while (*s == '/'); + c = *s; /* Save the current char */ + *s = 0; /* and replace it with nul. */ + break; + } + ++s; + } + + if (mkdir(path, 0777) < 0) { + + /* If we failed for any other reason than the directory + * already exists, output a diagnostic and return -1.*/ +#ifdef __U_BOOT__ + if (errno != -EEXIST) +#else + if (errno != EEXIST) +#endif + break; + } + if (!c) + goto out; + + /* Remove any inserted nul from the path (recursive mode). */ + *s = c; + + } while (1); + +out: + free(path); + return errno; +} + |