implement mkdir -p
This commit is contained in:
parent
2d02f7c0f0
commit
46d5705395
|
@ -100,6 +100,9 @@ ssize_t write(int fd, const void *buf, size_t count);
|
||||||
|
|
||||||
off_t lseek(int fildes, off_t offset, int whence);
|
off_t lseek(int fildes, off_t offset, int whence);
|
||||||
int mkdir (const char *pathname, mode_t mode);
|
int mkdir (const char *pathname, mode_t mode);
|
||||||
|
|
||||||
|
/* Create a directory and its parents */
|
||||||
|
int make_directory(const char *pathname);
|
||||||
int rmdir (const char *pathname);
|
int rmdir (const char *pathname);
|
||||||
|
|
||||||
const char *getcwd(void);
|
const char *getcwd(void);
|
||||||
|
|
|
@ -14,6 +14,7 @@ obj-y += kfifo.o
|
||||||
obj-y += libbb.o
|
obj-y += libbb.o
|
||||||
obj-y += libgen.o
|
obj-y += libgen.o
|
||||||
obj-y += recursive_action.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_BZLIB) += bzlib.o bzlib_crctable.o bzlib_decompress.o bzlib_huffman.o bzlib_randtable.o
|
||||||
obj-$(CONFIG_ZLIB) += zlib.o gunzip.o
|
obj-$(CONFIG_ZLIB) += zlib.o gunzip.o
|
||||||
obj-$(CONFIG_CRC32) += crc32.o
|
obj-$(CONFIG_CRC32) += crc32.o
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue