Commets, ifdef ...
[scilab.git] / scilab / modules / core / src / c / tmpdir.c
1 /*-----------------------------------------------------------------------------------*/
2 /* Scilab */
3 /*-----------------------------------------------------------------------------------*/
4 #include <stdio.h>
5 #include "machine.h"
6 #include "../../../io/includes/directories.h"
7 #include <string.h>
8 #include "MALLOC.h" /* MALLOC */
9
10 #ifdef _MSC_VER
11 #include <windows.h>
12 #endif
13 #if defined(__STDC__) || defined(_MSC_VER)
14 #include <stdlib.h>
15 #ifndef _MSC_VER
16 #include <sys/types.h>
17 #include <unistd.h>
18 #endif
19 #else 
20 extern  char  *getenv();
21 #endif
22
23 #ifdef _MSC_VER
24 #define putenv(x) _putenv(x)
25 #endif
26
27 #ifdef _MSC_VER
28 #include <process.h>
29 #endif
30 #include "prompt.h"
31 #include "tmpdir.h"
32 /*-----------------------------------------------------------------------------------*/
33 static char tmp_dir[256],buf[256];
34 /*-----------------------------------------------------------------------------------*/
35 /**
36 * creates a tmp dir for a scilab session 
37 * and fixes the TMPDIR env variable
38 */
39 void C2F(settmpdir)(void)
40 {
41 #ifdef _MSC_VER
42         #define PATH_MAX 1024
43         char TmpDirDefault[PATH_MAX];
44 #endif
45   static int first =0;
46   if ( first == 0 ) 
47     {
48       first++;
49 #ifdef _MSC_VER 
50           if (!GetTempPath(PATH_MAX,TmpDirDefault))
51           {
52                   MessageBox(NULL,"Don''t find Windows temporary directory","Error",MB_ICONERROR);
53                   exit(1);
54           }
55           else
56           {
57                   sprintf(tmp_dir,"%sSCI_TMP_%d_",TmpDirDefault,(int) _getpid());
58           }
59
60           if ( CreateDirectory(tmp_dir,NULL)==FALSE)
61       {
62                 DWORD attribs=GetFileAttributes (tmp_dir); 
63
64                 if (attribs & FILE_ATTRIBUTE_DIRECTORY)
65                 {
66                   /* Repertoire existant */
67                 }
68                 else
69                 {
70                   #ifdef _DEBUG
71                   char MsgErr[1024];
72                   wsprintf(MsgErr,"Impossible to create : %s",tmp_dir);
73                   MessageBox(NULL,MsgErr,"Error",MB_ICONERROR);
74                   exit(1);
75                   #else
76                   GetTempPath(PATH_MAX,TmpDirDefault);
77                   sprintf(tmp_dir,"%s",TmpDirDefault);
78                   tmp_dir[strlen(tmp_dir)-1]='\0'; /* Remove last \ */
79                   #endif
80                 }
81   }
82 #else 
83   sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
84   CreateDir(tmp_dir) ;
85 #endif 
86   sprintf(buf,"TMPDIR=%s",tmp_dir);
87   putenv(buf);
88     }
89 }
90 /*-----------------------------------------------------------------------------------*/
91 /**
92 * get a reference to tmp_dir 
93 * @return the name of the directory
94 */
95 char *get_sci_tmp_dir(void)
96 {
97   /* just in case */
98   C2F(settmpdir)();
99   return tmp_dir;
100 }
101 /*-----------------------------------------------------------------------------------*/
102 /**
103 * remove TMPDIR directory
104 */
105 void C2F(tmpdirc)(void)
106 {
107   char *tmp_dir2 = get_sci_tmp_dir(); 
108   RemoveDir(tmp_dir2);
109 }
110 /*-----------------------------------------------------------------------------------*/
111 char *getTMPDIR(void)
112 {
113         char *TMPDIR=NULL;
114         TMPDIR=(char*)MALLOC(sizeof(char)*(strlen(tmp_dir)+1));
115         strcpy(TMPDIR,tmp_dir);
116
117         return TMPDIR;
118 }
119 /*-----------------------------------------------------------------------------------*/