6188685091
Normally errno contains a positive error value. A certain unnamed developer mixed this up while implementing U-Boot-v2. Also, normally errno is never set to zero by any library function. This patch fixes this. Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
59 lines
937 B
C
59 lines
937 B
C
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
#ifdef __BAREBOX__
|
|
#include <fs.h>
|
|
#include <malloc.h>
|
|
#include <common.h>
|
|
#endif
|
|
|
|
int make_directory(const char *dir)
|
|
{
|
|
char *s = strdup(dir);
|
|
char *path = s;
|
|
char c;
|
|
int ret = 0;
|
|
|
|
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.*/
|
|
if (errno != EEXIST) {
|
|
ret = -errno;
|
|
break;
|
|
}
|
|
}
|
|
if (!c)
|
|
goto out;
|
|
|
|
/* Remove any inserted nul from the path (recursive mode). */
|
|
*s = c;
|
|
|
|
} while (1);
|
|
|
|
out:
|
|
free(path);
|
|
if (ret)
|
|
errno = -ret;
|
|
return ret;
|
|
}
|
|
#ifdef __BAREBOX__
|
|
EXPORT_SYMBOL(make_directory);
|
|
#endif
|