Skip to content

Instantly share code, notes, and snippets.

@mohan43u
Created December 13, 2020 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohan43u/d2177aecbb4d3ec1f664193bdadd89c5 to your computer and use it in GitHub Desktop.
Save mohan43u/d2177aecbb4d3ec1f664193bdadd89c5 to your computer and use it in GitHub Desktop.
simple mkdir -p implementation in C
+static void mkdirpath(const char *path, mode_t mode) {
+ char *d = NULL;
+ char *i = NULL;
+
+ if(path == NULL) return;
+ d = strdup(path);
+ i = d;
+ while(i != NULL) {
+ if( *i == '/') i++;
+ i = strchr(i, '/');
+ if(i == NULL) break;
+ *i = 0;
+ if(mkdir(d, mode) != 0 && errno != EEXIST) {
+ dprintf(2, "mkdirp: failed to mkdir %s: %s\n", d, strerror(errno));
+ break;
+ }
+ *i = '/';
+ }
+ ncv_free(d);
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment