Correction warnings -w4 compiler
[scilab.git] / scilab / modules / core / src / c / tmpdir.c
1 /* Copyright INRIA/ENPC */
2
3 #include "machine.h"
4 #include <stdio.h>
5
6
7 #include "MALLOC.h" /* MALLOC */
8
9 #if _MSC_VER
10 #include <windows.h>
11 #endif
12 #if defined(__STDC__) || defined(_MSC_VER)
13 #include <stdlib.h>
14 #ifndef _MSC_VER
15 #include <sys/types.h>
16 #include <unistd.h>
17 #endif
18 #else 
19 extern  char  *getenv();
20 #endif
21
22 #if (defined _MSC_VER)
23 #define putenv(x) _putenv(x)
24 #endif
25
26 #if defined(_MSC_VER)
27 #include <process.h>
28 #endif
29 #include "prompt.h"
30 #include "tmpdir.h"
31
32 static char tmp_dir[256],buf[256];
33
34
35 /****************************
36  * creates a tmp dir for a scilab session 
37  * and fixes the TMPDIR env variable
38  ****************************/
39
40 void C2F(settmpdir)(void)
41 {
42 #if _MSC_VER
43         #define PATH_MAX 1024
44         char TmpDirDefault[PATH_MAX];
45 #endif
46   static int first =0;
47   if ( first == 0 ) 
48     {
49       first++;
50 #ifdef _MSC_VER 
51           if (!GetTempPath(PATH_MAX,TmpDirDefault))
52           {
53                   MessageBox(NULL,"Don''t find Windows temporary directory","Error",MB_ICONERROR);
54                   exit(1);
55           }
56           else
57           {
58                   sprintf(tmp_dir,"%sSCI_TMP_%d_",TmpDirDefault,(int) _getpid());
59           }
60
61           if ( CreateDirectory(tmp_dir,NULL)==FALSE)
62       {
63                 DWORD attribs=GetFileAttributes (tmp_dir); 
64
65                 if (attribs & FILE_ATTRIBUTE_DIRECTORY)
66                 {
67                   // Repertoire existant
68                 }
69                 else
70                 {
71                   #ifdef _DEBUG
72                   char MsgErr[1024];
73                   wsprintf(MsgErr,"Impossible to create : %s",tmp_dir);
74                   MessageBox(NULL,MsgErr,"Error",MB_ICONERROR);
75                   exit(1);
76                   #else
77                   GetTempPath(PATH_MAX,TmpDirDefault);
78                   sprintf(tmp_dir,"%s",TmpDirDefault);
79                   tmp_dir[strlen(tmp_dir)-1]='\0'; /* Remove last \ */
80                   #endif
81                 }
82   }
83 #else 
84   sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
85   sprintf(buf,"umask 000;if test ! -d %s; then mkdir %s; fi ",tmp_dir,tmp_dir);
86   system(buf);
87 #endif 
88   sprintf(buf,"TMPDIR=%s",tmp_dir);
89   putenv(buf);
90     }
91 }
92
93 /****************************
94  * get a reference to tmp_dir 
95  ****************************/
96
97 char *get_sci_tmp_dir(void)
98 {
99   /* just in case */
100   C2F(settmpdir)();
101   return tmp_dir;
102 }
103
104
105 #if _MSC_VER
106 /* Remove directory and subdirectories */
107 /* A.C INRIA 2005 */
108 int DeleteDirectory(char *refcstrRootDirectory)
109 {
110         BOOL bDeleteSubdirectories=TRUE;
111         BOOL bSubdirectory = FALSE;
112         HANDLE hFile;
113         char *strFilePath=NULL;
114         char *strPattern=NULL;
115         WIN32_FIND_DATA FileInformation;
116         DWORD dwError;
117
118         strPattern = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5));
119         sprintf(strPattern,"%s\\*.*",refcstrRootDirectory);
120
121         hFile = FindFirstFile(strPattern, &FileInformation);
122         if (strPattern) { FREE(strPattern);strPattern=NULL;}
123
124         if(hFile != INVALID_HANDLE_VALUE)
125         {
126                 do
127                 {
128                         if(FileInformation.cFileName[0] != '.')
129                         {
130                                 if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
131                                 strFilePath = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5+strlen(FileInformation.cFileName)));
132                                 sprintf(strFilePath,"%s\\%s",refcstrRootDirectory,FileInformation.cFileName);
133
134                                 if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
135                                 {
136                                         if(bDeleteSubdirectories)
137                                         {
138                                                 int iRC = DeleteDirectory(strFilePath);
139                                                 if(iRC) return iRC;
140                                         }
141                                         else bSubdirectory = TRUE;
142                                 }
143                                 else
144                                 {
145                                         if(SetFileAttributes(strFilePath,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
146                                         if(DeleteFile(strFilePath) == FALSE) return GetLastError();
147                                 }
148                         }
149                 } while(FindNextFile(hFile, &FileInformation) == TRUE);
150
151                 FindClose(hFile);
152
153                 dwError = GetLastError();
154                 if(dwError != ERROR_NO_MORE_FILES) return dwError;
155                 else
156                 {
157                         if(!bSubdirectory)
158                         {
159                                 if(SetFileAttributes(refcstrRootDirectory,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
160                                 if(RemoveDirectory(refcstrRootDirectory) == FALSE)      return GetLastError();
161                         }
162                 }
163         }
164
165         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
166         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
167
168         return 0;
169 }
170 #endif
171
172 /*************************************************
173  * remove TMPDIR and dynamic link temporary files 
174  *************************************************/
175
176 #if (defined(hppa))
177   extern void hppa_sci_unlink_shared();
178 #endif
179
180 void C2F(tmpdirc)(void)
181 {
182   char *tmp_dir2 = get_sci_tmp_dir(); 
183 #ifdef _MSC_VER 
184   DeleteDirectory(tmp_dir2);
185 #else 
186 #if (defined(hppa))
187   hppa_sci_unlink_shared();
188 #endif
189   sprintf(buf,"rm -f -r %s >/dev/null  2>/dev/null",tmp_dir2);
190   system(buf);
191   sprintf(buf,"rm -f -r /tmp/%d.metanet.* > /dev/null  2>/dev/null",
192           (int) getpid());
193   system(buf);
194 #endif 
195 }