Last active
December 5, 2022 09:13
-
-
Save Reobos/0cbedcd3e64132243a39 to your computer and use it in GitHub Desktop.
Revisions
-
Kyle Chisholm revised this gist
Jul 5, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,9 @@ /* recursive mkdir based on http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html */ #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #define PATH_MAX_STRING_SIZE 256 -
Kyle Chisholm revised this gist
Jul 5, 2018 . 1 changed file with 13 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -14,19 +14,27 @@ int mkdir_p(const char *dir, const mode_t mode) { char *p = NULL; struct stat sb; size_t len; /* copy path */ len = strnlen (dir, PATH_MAX_STRING_SIZE); if (len == 0 || len == PATH_MAX_STRING_SIZE) { return -1; } memcpy (tmp, dir, len); tmp[len] = '\0'; /* remove trailing slash */ if(tmp[len - 1] == '/') { tmp[len - 1] = '\0'; } /* check if path exists and is a directory */ if (stat (tmp, &sb) == 0) { if (S_ISDIR (sb.st_mode)) { return 0; } } /* recursive mkdir */ for(p = tmp + 1; *p; p++) { if(*p == '/') { -
Kyle Chisholm created this gist
Mar 9, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ /* recursive mkdir based on http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html */ #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <string.h> #define PATH_MAX_STRING_SIZE 256 /* recursive mkdir */ int mkdir_p(const char *dir, const mode_t mode) { char tmp[PATH_MAX_STRING_SIZE]; char *p = NULL; struct stat sb; size_t len; /* copy path */ strncpy(tmp, dir, sizeof(tmp)); len = strlen(tmp); if (len >= sizeof(tmp)) { return -1; } /* remove trailing slash */ if(tmp[len - 1] == '/') { tmp[len - 1] = 0; } /* recursive mkdir */ for(p = tmp + 1; *p; p++) { if(*p == '/') { *p = 0; /* test path */ if (stat(tmp, &sb) != 0) { /* path does not exist - create directory */ if (mkdir(tmp, mode) < 0) { return -1; } } else if (!S_ISDIR(sb.st_mode)) { /* not a directory */ return -1; } *p = '/'; } } /* test path */ if (stat(tmp, &sb) != 0) { /* path does not exist - create directory */ if (mkdir(tmp, mode) < 0) { return -1; } } else if (!S_ISDIR(sb.st_mode)) { /* not a directory */ return -1; } return 0; }