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