update C2F(getenvc)
[scilab.git] / scilab / modules / io / src / c / createdir.c
1 /*-----------------------------------------------------------------------------------*/\r
2 /* INRIA 2007 */\r
3 /* Allan CORNET */\r
4 /*-----------------------------------------------------------------------------------*/ \r
5 #ifndef _MSC_VER\r
6         #include <sys/stat.h>\r
7         #include <sys/types.h>\r
8         #include <fcntl.h>\r
9         #include <unistd.h> \r
10 #else\r
11         #include <Windows.h>\r
12 #endif\r
13 #include <stdio.h>\r
14 #include "createdir.h"\r
15 #include "MALLOC.h"\r
16 /*-----------------------------------------------------------------------------------*/ \r
17 #define DIRMODE 0777\r
18 /*-----------------------------------------------------------------------------------*/ \r
19 #if _MSC_VER\r
20 int DeleteDirectory(char *refcstrRootDirectory);\r
21 #endif\r
22 /*-----------------------------------------------------------------------------------*/ \r
23 BOOL ExistDir(char * path)\r
24 {\r
25         BOOL bOK=FALSE;\r
26         #ifndef _MSC_VER\r
27                 BOOL bOK=FALSE;\r
28                 struct stat buf;\r
29                 if (path == NULL) return FALSE;\r
30 \r
31                 if ( (stat (path, &buf)) && (S_ISDIR (buf.st_mode)) )\r
32                 {\r
33                         bOK=TRUE;\r
34                 }\r
35         #else\r
36                 WIN32_FIND_DATA ffd;\r
37                 HANDLE sh = FindFirstFile(path, &ffd);\r
38 \r
39                 if(INVALID_HANDLE_VALUE == sh) return FALSE;\r
40                 if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) bOK=TRUE;\r
41         #endif\r
42 \r
43         return bOK;\r
44 }\r
45 /*-----------------------------------------------------------------------------------*/ \r
46 BOOL CreateDir(const char* path) \r
47 {\r
48         BOOL bOK=FALSE;\r
49         #ifndef _MSC_VER\r
50                 struct stat statBuf;\r
51 \r
52                 if  (!ExistPath(path)) \r
53                 {\r
54                         if (mkdir(path, DIRMODE) == 0) \r
55                         {\r
56                                 bOK=TRUE;\r
57                         }\r
58                 }\r
59         #else\r
60                 if (CreateDirectory(path,NULL)) bOK=TRUE;\r
61         #endif\r
62         return bOK;\r
63 }\r
64 /*-----------------------------------------------------------------------------------*/ \r
65 BOOL RemoveDir(char *path)\r
66 {\r
67         BOOL bOK=FALSE;\r
68         if (ExistDir(path))\r
69         {\r
70                 #ifndef _MSC_VER\r
71                         \r
72                         if (rmdir(path) == 0 ) \r
73                         {\r
74                                 bOK=TRUE;\r
75                         }\r
76                         else\r
77                         {\r
78                                 char buf[1024];\r
79                                 sprintf(buf,"rm -rf %s >/dev/null  2>/dev/null",path);\r
80                                 system(buf);\r
81                         }\r
82                 #else\r
83                         DeleteDirectory(path); \r
84                         if (!ExistDir(path)) bOK=TRUE;\r
85                 #endif\r
86         }\r
87         return bOK;\r
88 }\r
89 /*-----------------------------------------------------------------------------------*/ \r
90 #if _MSC_VER\r
91 int DeleteDirectory(char *refcstrRootDirectory)\r
92 {\r
93         BOOL bDeleteSubdirectories=TRUE;\r
94         BOOL bSubdirectory = FALSE;\r
95         HANDLE hFile;\r
96         char *strFilePath=NULL;\r
97         char *strPattern=NULL;\r
98         WIN32_FIND_DATA FileInformation;\r
99         DWORD dwError;\r
100 \r
101         strPattern = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5));\r
102         sprintf(strPattern,"%s\\*.*",refcstrRootDirectory);\r
103 \r
104         hFile = FindFirstFile(strPattern, &FileInformation);\r
105         if (strPattern) { FREE(strPattern);strPattern=NULL;}\r
106 \r
107         if(hFile != INVALID_HANDLE_VALUE)\r
108         {\r
109                 do\r
110                 {\r
111                         if(FileInformation.cFileName[0] != '.')\r
112                         {\r
113                                 if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}\r
114                                 strFilePath = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5+strlen(FileInformation.cFileName)));\r
115                                 sprintf(strFilePath,"%s\\%s",refcstrRootDirectory,FileInformation.cFileName);\r
116 \r
117                                 if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)\r
118                                 {\r
119                                         if(bDeleteSubdirectories)\r
120                                         {\r
121                                                 int iRC = DeleteDirectory(strFilePath);\r
122                                                 if(iRC) return iRC;\r
123                                         }\r
124                                         else bSubdirectory = TRUE;\r
125                                 }\r
126                                 else\r
127                                 {\r
128                                         if(SetFileAttributes(strFilePath,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();\r
129                                         if(DeleteFile(strFilePath) == FALSE) return GetLastError();\r
130                                 }\r
131                         }\r
132                 } while(FindNextFile(hFile, &FileInformation) == TRUE);\r
133 \r
134                 FindClose(hFile);\r
135 \r
136                 dwError = GetLastError();\r
137                 if(dwError != ERROR_NO_MORE_FILES) return dwError;\r
138                 else\r
139                 {\r
140                         if(!bSubdirectory)\r
141                         {\r
142                                 if(SetFileAttributes(refcstrRootDirectory,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();\r
143                                 if(RemoveDirectory(refcstrRootDirectory) == FALSE)      return GetLastError();\r
144                         }\r
145                 }\r
146         }\r
147 \r
148         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}\r
149         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}\r
150 \r
151         return 0;\r
152 }\r
153 #endif\r
154 /*-----------------------------------------------------------------------------------*/ \r