Created
May 2, 2021 20:10
-
-
Save andy5995/9377f82b0ef2cb0d290c635c780b4634 to your computer and use it in GitHub Desktop.
recursive mkdir
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
/** | |
* mkdir_with_parents() | |
* | |
* Check for the existence of a dir, and create it if not found. | |
* Also creates parent directories. | |
*/ | |
int mkdir_with_parents (const char *dir, mode_t mode) | |
{ | |
if (exists (dir)) | |
return 0; | |
int res = 0; | |
char tmp[strlen (dir) + 1]; | |
strcpy (tmp, dir); | |
char *parent = dirname (tmp); | |
if (!exists (parent)) | |
res = mkdir_with_parents (parent, mode); | |
if (res) | |
return res; | |
return mkdir (dir, mode); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fails on osx, passes on all versions of gcc and clang on Linux