OSDN Git Service

VER0.1.0
[lib1stclass/main.git] / rmkdir.c
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <dirent.h>
4 #include <string.h>
5
6 #ifdef __cplusplus
7 #include "1stclass.hpp"
8 int firstclass::rmkdir(const string in_dir){
9   char *check_dir=NULL;
10   try{
11   check_dir=new char[sizeof(char)*(in_dir.length()+1)];
12   safe_strcat(check_dir, in_dir.c_str());
13 #else
14 #include "1stclass.h"
15 int rmkdir(const char *check_dir){
16 #endif
17
18   struct stat st;
19   char *token_p=NULL;
20   char dir_path[512]="";
21   char moto[512]="";
22   if(stat(check_dir, &st)!=0){
23     if(mkdir(check_dir, 0755)==0){
24     }
25     else{
26       safe_strcat(moto, check_dir);
27       token_p=strtok(moto, "/");
28       safe_strcat(dir_path, "/");
29       safe_strcat(dir_path, token_p);
30       if(stat(dir_path, &st)!=0){
31         if(mkdir(dir_path, 0755)==0){
32         }
33         else{
34 #ifdef __cplusplus
35   delete check_dir;
36 #endif
37           return 1;
38         }
39       }
40       while((token_p=strtok(NULL,"/"))!=NULL){
41         safe_strcat(dir_path, "/");
42         safe_strcat(dir_path, token_p);
43         if(stat(dir_path, &st)!=0){
44           if(mkdir(dir_path, 0755)==0){
45           }
46           else{
47 #ifdef __cplusplus
48   delete check_dir;
49 #endif
50             return 1;
51           }
52         }
53       }
54     }
55   }
56 #ifdef __cplusplus
57   delete check_dir;
58   }
59   catch(...){
60     if(check_dir!=NULL)
61       delete check_dir;
62     return 1;
63   }
64 #endif
65   return 0;
66 }