Skip to content

Instantly share code, notes, and snippets.

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 andy5995/9377f82b0ef2cb0d290c635c780b4634 to your computer and use it in GitHub Desktop.
Save andy5995/9377f82b0ef2cb0d290c635c780b4634 to your computer and use it in GitHub Desktop.
recursive mkdir
/**
* 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);
}
@andy5995
Copy link
Author

andy5995 commented May 2, 2021

fails on osx, passes on all versions of gcc and clang on Linux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment