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