Created
December 13, 2020 11:44
-
-
Save mohan43u/d2177aecbb4d3ec1f664193bdadd89c5 to your computer and use it in GitHub Desktop.
simple mkdir -p implementation in C
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
+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