Created
April 14, 2015 21:48
-
-
Save jwilm/9628e1f09ee225307343 to your computer and use it in GitHub Desktop.
c mkdirp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// assumes path of the form foo/bar/baz.png | |
// This function is not very generic | |
int mkdirp(const char *path) { | |
char *buf = (char*)alloca(strlen(path)); | |
char *last = strrchr(path, '/'); | |
int res = 0; | |
// Bail if there's no directories to make | |
if (last == NULL) { | |
return 0; | |
} | |
for (char *cur = strchr(path, '/'); cur != NULL; cur=strchr(cur + 1, '/')) { | |
memset(buf, 0, strlen(path)); | |
size_t len = cur - path; | |
strncpy(buf, path, len); | |
buf[len] = '\0'; | |
if ((res = mkdir(buf, 0700)) == -1) { | |
// Error | |
return res; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment