Skip to content

Instantly share code, notes, and snippets.

@jwilm
Created April 14, 2015 21:48
Show Gist options
  • Save jwilm/9628e1f09ee225307343 to your computer and use it in GitHub Desktop.
Save jwilm/9628e1f09ee225307343 to your computer and use it in GitHub Desktop.
c mkdirp
// 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