File indexing completed on 2026-04-09 07:49:13
0001
0002
0003
0004 #include <cstring>
0005 #include <cstdlib>
0006 #include <cstdio>
0007 #include <sys/stat.h>
0008 #include <errno.h>
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 int mkdirp(const char* path, int mode_ = 0 );
0019
0020
0021 int mkdirp(const char* path_, int mode_ )
0022 {
0023 mode_t default_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH ;
0024 mode_t mode = mode_ == 0 ? default_mode : mode_ ;
0025
0026 char* path = strdup(path_);
0027 char* p = path ;
0028 int rc = 0 ;
0029
0030 while (*p != '\0' && rc == 0)
0031 {
0032 p++;
0033 while(*p != '\0' && *p != '/') p++;
0034 char v = *p;
0035 *p = '\0' ;
0036 printf("%s\n", path );
0037 rc = mkdir(path, mode) == -1 && errno != EEXIST ? 1 : 0 ;
0038 *p = v;
0039 }
0040 free(path);
0041 return rc ;
0042 }
0043
0044 int main(int argc, char** argv)
0045 {
0046 const char* path = "/tmp/red/green/blue" ;
0047 int mode = 0777 ;
0048 int rc = mkdirp(path, mode);
0049 return rc ;
0050 }
0051
0052
0053