* All string containing ' \n' changed to '\n'
[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 /*-----------------------------------------------------------------------------------*/
34 static char tmp_dir[256],buf[256];
35 /*-----------------------------------------------------------------------------------*/
36 /**
37 * creates a tmp dir for a scilab session 
38 * and fixes the TMPDIR env variable
39 */
40 void C2F(settmpdir)(void)
41 {
42 #ifdef _MSC_VER
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   createdirectory(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 /*-----------------------------------------------------------------------------------*/