replace // by /* */
[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/createdir.h"
7 #include "MALLOC.h" /* MALLOC */
8
9 #ifdef _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 * creates a tmp dir for a scilab session 
36 * and fixes the TMPDIR env variable
37 */
38 void C2F(settmpdir)(void)
39 {
40 #ifdef _MSC_VER
41         #define PATH_MAX 1024
42         char TmpDirDefault[PATH_MAX];
43 #endif
44   static int first =0;
45   if ( first == 0 ) 
46     {
47       first++;
48 #ifdef _MSC_VER 
49           if (!GetTempPath(PATH_MAX,TmpDirDefault))
50           {
51                   MessageBox(NULL,"Don''t find Windows temporary directory","Error",MB_ICONERROR);
52                   exit(1);
53           }
54           else
55           {
56                   sprintf(tmp_dir,"%sSCI_TMP_%d_",TmpDirDefault,(int) _getpid());
57           }
58
59           if ( CreateDirectory(tmp_dir,NULL)==FALSE)
60       {
61                 DWORD attribs=GetFileAttributes (tmp_dir); 
62
63                 if (attribs & FILE_ATTRIBUTE_DIRECTORY)
64                 {
65                   /* Repertoire existant */
66                 }
67                 else
68                 {
69                   #ifdef _DEBUG
70                   char MsgErr[1024];
71                   wsprintf(MsgErr,"Impossible to create : %s",tmp_dir);
72                   MessageBox(NULL,MsgErr,"Error",MB_ICONERROR);
73                   exit(1);
74                   #else
75                   GetTempPath(PATH_MAX,TmpDirDefault);
76                   sprintf(tmp_dir,"%s",TmpDirDefault);
77                   tmp_dir[strlen(tmp_dir)-1]='\0'; /* Remove last \ */
78                   #endif
79                 }
80   }
81 #else 
82   sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
83   CreateDir(tmp_dir) ;
84 #endif 
85   sprintf(buf,"TMPDIR=%s",tmp_dir);
86   putenv(buf);
87     }
88 }
89 /*-----------------------------------------------------------------------------------*/
90 /**
91 * get a reference to tmp_dir 
92 */
93 char *get_sci_tmp_dir(void)
94 {
95   /* just in case */
96   C2F(settmpdir)();
97   return tmp_dir;
98 }
99 /*-----------------------------------------------------------------------------------*/
100 /**
101 * remove TMPDIR directory
102 */
103 void C2F(tmpdirc)(void)
104 {
105   char *tmp_dir2 = get_sci_tmp_dir(); 
106   RemoveDir(tmp_dir2);
107 }
108 /*-----------------------------------------------------------------------------------*/
109 char *getTMPDIR(void)
110 {
111         char *TMPDIR=NULL;
112         TMPDIR=(char*)MALLOC(sizeof(char)*(strlen(tmp_dir)+1));
113         strcpy(TMPDIR,tmp_dir);
114
115         return TMPDIR;
116 }
117 /*-----------------------------------------------------------------------------------*/