2007-09-27 09:59:18 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2009-12-15 08:11:09 +00:00
|
|
|
#ifdef __BAREBOX__
|
2007-09-27 09:59:18 +00:00
|
|
|
#include <fs.h>
|
2016-04-11 14:12:51 +00:00
|
|
|
#include <libfile.h>
|
2007-09-27 09:59:18 +00:00
|
|
|
#include <malloc.h>
|
2007-10-07 12:27:24 +00:00
|
|
|
#include <common.h>
|
2015-11-17 12:14:57 +00:00
|
|
|
#define STATIC
|
|
|
|
#else
|
|
|
|
#define STATIC static inline
|
2007-09-27 09:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2015-11-17 12:14:57 +00:00
|
|
|
STATIC int make_directory(const char *dir)
|
2007-09-27 09:59:18 +00:00
|
|
|
{
|
|
|
|
char *s = strdup(dir);
|
|
|
|
char *path = s;
|
|
|
|
char c;
|
2012-05-13 10:43:58 +00:00
|
|
|
int ret = 0;
|
2007-09-27 09:59:18 +00:00
|
|
|
|
|
|
|
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.*/
|
2012-05-13 10:43:58 +00:00
|
|
|
if (errno != EEXIST) {
|
|
|
|
ret = -errno;
|
2007-09-27 09:59:18 +00:00
|
|
|
break;
|
2012-05-13 10:43:58 +00:00
|
|
|
}
|
2007-09-27 09:59:18 +00:00
|
|
|
}
|
|
|
|
if (!c)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Remove any inserted nul from the path (recursive mode). */
|
|
|
|
*s = c;
|
|
|
|
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(path);
|
2012-05-13 10:43:58 +00:00
|
|
|
if (ret)
|
|
|
|
errno = -ret;
|
|
|
|
return ret;
|
2007-09-27 09:59:18 +00:00
|
|
|
}
|
2009-12-15 08:11:09 +00:00
|
|
|
#ifdef __BAREBOX__
|
2007-10-07 12:27:24 +00:00
|
|
|
EXPORT_SYMBOL(make_directory);
|
|
|
|
#endif
|