| @@ -181,37 +181,23 @@ char* r_readdir(r_dir_t *rdir) |
| 181 | 181 | | return NULL; |
| 182 | 182 | | } |
| 183 | 183 | | |
| 184 | | − | int r_mkdir(const char *path) |
| 184 | + | int r_mkdir(char *path) |
| 185 | 185 | | { |
| 186 | | − | char *dir, *d; |
| 187 | | − | struct stat stats; |
| 188 | | − | int err = 0; |
| 186 | + | char c, *s = path; |
| 187 | + | struct stat st; |
| 189 | 188 | | |
| 190 | | − | if (*path == '\0') |
| 191 | | − | return -1; |
| 192 | | − | |
| 193 | | − | if (stat(path, &stats) == 0) |
| 194 | | − | return S_ISDIR(stats.st_mode) ? 0 : -1; |
| 195 | | − | |
| 196 | | − | d = dir = (char*) emalloc(strlen(path) + 1); |
| 197 | | − | strcpy(dir, path); |
| 198 | | − | |
| 199 | | − | while (d != NULL && err == 0) { |
| 200 | | − | d = strchr(d + 1, '/'); |
| 201 | | − | if (d != NULL) |
| 202 | | − | *d = '\0'; |
| 203 | | − | if (access(dir, F_OK) < 0 && errno == ENOENT) { |
| 204 | | − | if (mkdir(dir, 0755) < 0) { |
| 205 | | − | error(0, errno, "%s", dir); |
| 206 | | − | err = -1; |
| 207 | | − | } |
| 208 | | − | } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) { |
| 209 | | − | err = -1; |
| 189 | + | while (*s != '\0') { |
| 190 | + | if (*s == '/') { |
| 191 | + | s++; |
| 192 | + | continue; |
| 210 | 193 | | } |
| 211 | | − | if (d != NULL) |
| 212 | | − | *d = '/'; |
| 194 | + | for (; *s != '\0' && *s != '/'; s++); |
| 195 | + | c = *s; |
| 196 | + | *s = '\0'; |
| 197 | + | if (mkdir(path, 0755) == -1) |
| 198 | + | if (errno != EEXIST || stat(path, &st) == -1 || !S_ISDIR(st.st_mode)) |
| 199 | + | return -1; |
| 200 | + | *s = c; |
| 213 | 201 | | } |
| 214 | | − | free(dir); |
| 215 | | − | |
| 216 | | − | return err; |
| 202 | + | return 0; |
| 217 | 203 | | } |