Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jonasfj
Created December 4, 2013 23:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonasfj/7797272 to your computer and use it in GitHub Desktop.
Save jonasfj/7797272 to your computer and use it in GitHub Desktop.
A quick and dirty implementation of `mkdir -p` for C++, enjoy.
#ifndef MKDIRP_H
#define MKDIRP_H
#include <sys/stat.h>
#include <errno.h>
#define DEFAULT_MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
/** Utility function to create directory tree */
bool mkdirp(const char* path, mode_t mode = DEFAULT_MODE) {
// Invalid string
if(path[0] == '\0') {
return false;
}
// const cast for hack
char* p = const_cast<char*>(path);
// Find next slash mkdir() it and until we're at end of string
while (*p != '\0') {
// Skip first character
p++;
// Find first slash or end
while(*p != '\0' && *p != '/') p++;
// Remember value from p
char v = *p;
// Write end of string at p
*p = '\0';
// Create folder from path to '\0' inserted at p
if(mkdir(path, mode) != 0 && errno != EEXIST) {
*p = v;
return false;
}
// Restore path to it's former glory
*p = v;
}
return true;
}
#endif // MKDIRP_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment