Warning fixed
[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                   #endif
77
78                   GetTempPath(PATH_MAX,TmpDirDefault);
79                   sprintf(tmp_dir,"%s",TmpDirDefault);
80                   tmp_dir[strlen(tmp_dir)-1]='\0'; /* Remove last \ */
81
82                 }
83   }
84 #else 
85   sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
86   sprintf(buf,"umask 000;if test ! -d %s; then mkdir %s; fi ",tmp_dir,tmp_dir);
87   system(buf);
88 #endif 
89   sprintf(buf,"TMPDIR=%s",tmp_dir);
90   putenv(buf);
91     }
92 }
93
94 /****************************
95  * get a reference to tmp_dir 
96  ****************************/
97
98 char *get_sci_tmp_dir(void)
99 {
100   /* just in case */
101   C2F(settmpdir)();
102   return tmp_dir;
103 }
104
105
106 #if _MSC_VER
107 /* Remove directory and subdirectories */
108 /* A.C INRIA 2005 */
109 int DeleteDirectory(char *refcstrRootDirectory)
110 {
111         BOOL bDeleteSubdirectories=TRUE;
112         BOOL bSubdirectory = FALSE;
113         HANDLE hFile;
114         char *strFilePath=NULL;
115         char *strPattern=NULL;
116         WIN32_FIND_DATA FileInformation;
117         DWORD dwError;
118
119         strPattern = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5));
120         sprintf(strPattern,"%s\\*.*",refcstrRootDirectory);
121
122         hFile = FindFirstFile(strPattern, &FileInformation);
123         if (strPattern) { FREE(strPattern);strPattern=NULL;}
124
125         if(hFile != INVALID_HANDLE_VALUE)
126         {
127                 do
128                 {
129                         if(FileInformation.cFileName[0] != '.')
130                         {
131                                 if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
132                                 strFilePath = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5+strlen(FileInformation.cFileName)));
133                                 sprintf(strFilePath,"%s\\%s",refcstrRootDirectory,FileInformation.cFileName);
134
135                                 if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
136                                 {
137                                         if(bDeleteSubdirectories)
138                                         {
139                                                 int iRC = DeleteDirectory(strFilePath);
140                                                 if(iRC) return iRC;
141                                         }
142                                         else bSubdirectory = TRUE;
143                                 }
144                                 else
145                                 {
146                                         if(SetFileAttributes(strFilePath,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
147                                         if(DeleteFile(strFilePath) == FALSE) return GetLastError();
148                                 }
149                         }
150                 } while(FindNextFile(hFile, &FileInformation) == TRUE);
151
152                 FindClose(hFile);
153
154                 dwError = GetLastError();
155                 if(dwError != ERROR_NO_MORE_FILES) return dwError;
156                 else
157                 {
158                         if(!bSubdirectory)
159                         {
160                                 if(SetFileAttributes(refcstrRootDirectory,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
161                                 if(RemoveDirectory(refcstrRootDirectory) == FALSE)      return GetLastError();
162                         }
163                 }
164         }
165
166         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
167         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
168
169         return 0;
170 }
171 #endif
172
173 /*************************************************
174  * remove TMPDIR and dynamic link temporary files 
175  *************************************************/
176
177 #if (defined(hppa))
178   extern void hppa_sci_unlink_shared();
179 #endif
180
181 void C2F(tmpdirc)(void)
182 {
183   char *tmp_dir2 = get_sci_tmp_dir(); 
184 #ifdef _MSC_VER 
185   DeleteDirectory(tmp_dir2);
186 #else 
187 #if (defined(hppa))
188   hppa_sci_unlink_shared();
189 #endif
190   sprintf(buf,"rm -f -r %s >/dev/null  2>/dev/null",tmp_dir2);
191   system(buf);
192   sprintf(buf,"rm -f -r /tmp/%d.metanet.* > /dev/null  2>/dev/null",
193           (int) getpid());
194   system(buf);
195 #endif 
196 }