menu: simplify usage for clients
[barebox-mini2440.git] / lib / make_directory.c
blob274bc146cf3b7bf94fb9f2bbfbf485e84f88522e
2 #include <string.h>
3 #include <errno.h>
4 #ifdef __BAREBOX__
5 #include <fs.h>
6 #include <malloc.h>
7 #include <common.h>
8 #endif
10 int make_directory(const char *dir)
12 char *s = strdup(dir);
13 char *path = s;
14 char c;
16 do {
17 c = 0;
19 /* Bypass leading non-'/'s and then subsequent '/'s. */
20 while (*s) {
21 if (*s == '/') {
22 do {
23 ++s;
24 } while (*s == '/');
25 c = *s; /* Save the current char */
26 *s = 0; /* and replace it with nul. */
27 break;
29 ++s;
32 if (mkdir(path, 0777) < 0) {
34 /* If we failed for any other reason than the directory
35 * already exists, output a diagnostic and return -1.*/
36 #ifdef __BAREBOX__
37 if (errno != -EEXIST)
38 #else
39 if (errno != EEXIST)
40 #endif
41 break;
43 if (!c)
44 goto out;
46 /* Remove any inserted nul from the path (recursive mode). */
47 *s = c;
49 } while (1);
51 out:
52 free(path);
53 return errno;
55 #ifdef __BAREBOX__
56 EXPORT_SYMBOL(make_directory);
57 #endif