Use the global define PATH_MAX
[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         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 * @return the name of the directory
93 */
94 char *get_sci_tmp_dir(void)
95 {
96   /* just in case */
97   C2F(settmpdir)();
98   return tmp_dir;
99 }
100 /*-----------------------------------------------------------------------------------*/
101 /**
102 * remove TMPDIR directory
103 */
104 void C2F(tmpdirc)(void)
105 {
106   char *tmp_dir2 = get_sci_tmp_dir(); 
107   RemoveDir(tmp_dir2);
108 }
109 /*-----------------------------------------------------------------------------------*/
110 char *getTMPDIR(void)
111 {
112         char *TMPDIR=NULL;
113         TMPDIR=(char*)MALLOC(sizeof(char)*(strlen(tmp_dir)+1));
114         strcpy(TMPDIR,tmp_dir);
115
116         return TMPDIR;
117 }
118 /*-----------------------------------------------------------------------------------*/