9
0
Fork 0

implement mkdir -p

This commit is contained in:
Sascha Hauer 2007-09-27 11:59:18 +02:00
parent 2d02f7c0f0
commit 46d5705395
3 changed files with 58 additions and 0 deletions

View File

@ -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);
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);
const char *getcwd(void);

View File

@ -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

54
lib/make_directory.c Normal file
View File

@ -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;
}