Comment line goes on 80 chars only
[scilab.git] / scilab / modules / core / src / c / tmpdir.c
1 /*--------------------------------------------------------------------------*/
2 /* Scilab */
3 /*--------------------------------------------------------------------------*/
4 #include <stdio.h>
5 #include "machine.h"
6 #include "../../../fileio/includes/removedir.h"
7 #include "../../../fileio/includes/createdirectory.h"
8 #include <string.h>
9 #include "MALLOC.h" /* MALLOC */
10
11 #ifdef _MSC_VER
12 #include <windows.h>
13 #endif
14 #if defined(__STDC__) || defined(_MSC_VER)
15 #include <stdlib.h>
16 #ifndef _MSC_VER
17 #include <sys/types.h>
18 #include <unistd.h>
19 #endif
20 #else 
21 extern  char  *getenv();
22 #endif
23
24 #ifdef _MSC_VER
25 #define putenv(x) _putenv(x)
26 #endif
27
28 #ifdef _MSC_VER
29 #include <process.h>
30 #endif
31 #include "prompt.h"
32 #include "tmpdir.h"
33 #include "localization.h"
34 /*--------------------------------------------------------------------------*/
35 static char tmp_dir[256],buf[256];
36 /*--------------------------------------------------------------------------*/
37 /**
38 * creates a tmp dir for a scilab session 
39 * and fixes the TMPDIR env variable
40 */
41 void C2F(settmpdir)(void)
42 {
43 #ifdef _MSC_VER
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   createdirectory(tmp_dir) ;
86 #endif 
87   sprintf(buf,"TMPDIR=%s",tmp_dir);
88   putenv(buf);
89     }
90 }
91 /*--------------------------------------------------------------------------*/
92 /**
93 * get a reference to tmp_dir 
94 * @return the name of the directory
95 */
96 char *get_sci_tmp_dir(void)
97 {
98   /* just in case */
99   C2F(settmpdir)();
100   return tmp_dir;
101 }
102 /*--------------------------------------------------------------------------*/
103 /**
104 * remove TMPDIR directory
105 */
106 void C2F(tmpdirc)(void)
107 {
108   char *tmp_dir2 = get_sci_tmp_dir(); 
109   removedir(tmp_dir2);
110 }
111 /*--------------------------------------------------------------------------*/
112 char *getTMPDIR(void)
113 {
114         char *TMPDIR=NULL;
115         TMPDIR=(char*)MALLOC(sizeof(char)*(strlen(tmp_dir)+1));
116         strcpy(TMPDIR,tmp_dir);
117
118         return TMPDIR;
119 }
120 /*--------------------------------------------------------------------------*/