TMPDIR is a symbolic link to /private/tmp under MacOS
[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  * Copyright (C) DIGITEO - 2009 - Allan CORNET
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 #include <stdio.h>
14 #include <string.h>
15
16 #ifdef _MSC_VER
17 #include <windows.h>
18 #include <process.h>
19 #endif
20
21 #if defined(__STDC__) || defined(_MSC_VER)
22 #include <stdlib.h>
23 #ifndef _MSC_VER
24 #include <sys/types.h>
25 #include <unistd.h>
26 #endif
27 #else 
28 extern  char  *getenv();
29 #endif
30
31 #ifdef _MSC_VER
32 #include <process.h>
33 #include "strdup_windows.h"
34 #endif
35 #include "MALLOC.h" /* MALLOC */
36 #include "tmpdir.h"
37 #include "localization.h"
38 #include "charEncoding.h"
39 #include "MALLOC.h"
40 #include "PATH_MAX.h"
41 #include "removedir.h"
42 #include "createdirectory.h"
43 /*--------------------------------------------------------------------------*/
44 static char tmp_dir[PATH_MAX+FILENAME_MAX+1];
45 static int first = 0;
46 /*--------------------------------------------------------------------------*/
47 #ifdef _MSC_VER
48 void createScilabTMPDIR(void)
49 {
50         wchar_t wcTmpDirDefault[PATH_MAX];
51         if (!GetTempPathW(PATH_MAX,wcTmpDirDefault))
52         {
53                 MessageBox(NULL,_("Cannot find Windows temporary directory (1)."),_("Error"),MB_ICONERROR);
54                 exit(1);
55         }
56         else
57         {
58                 wchar_t wctmp_dir[PATH_MAX+FILENAME_MAX+1];
59                 static wchar_t bufenv[PATH_MAX + 16];
60                 char *TmpDir = NULL;
61                 swprintf(wctmp_dir,PATH_MAX+FILENAME_MAX+1,L"%sSCI_TMP_%d_",wcTmpDirDefault,(int) _getpid());
62                 if ( CreateDirectoryW(wctmp_dir,NULL) == FALSE)
63                 {
64                         DWORD attribs = GetFileAttributesW (wctmp_dir);
65                         if (attribs & FILE_ATTRIBUTE_DIRECTORY)
66                         {
67                                 /* Repertoire existant */
68                         }
69                         else
70                         {
71                                 #ifdef _DEBUG
72                                 {
73                                         char MsgErr[1024];
74                                         wsprintf(MsgErr,_("Impossible to create : %s"),tmp_dir);
75                                         MessageBox(NULL,MsgErr,_("Error"),MB_ICONERROR);
76                                         exit(1);
77                                 }
78                                 #else
79                                 {
80                                         GetTempPathW(PATH_MAX,wcTmpDirDefault);
81                                         wcscpy(wctmp_dir,wcTmpDirDefault);
82                                         wctmp_dir[wcslen(wctmp_dir)-1]='\0'; /* Remove last \ */
83                                 }
84                                 #endif
85                         }
86                 }
87
88                 swprintf(bufenv,PATH_MAX + 16,L"TMPDIR=%s",wctmp_dir);
89                 _wputenv(bufenv);
90
91                 TmpDir = wide_string_to_UTF8(wctmp_dir);
92                 if (TmpDir)
93                 {
94                         strcpy(tmp_dir, TmpDir);
95                         FREE(TmpDir);
96                 }
97                 else
98                 {
99                         strcpy(tmp_dir, "");
100                 }
101         }
102 }
103 #else
104 void createScilabTMPDIR(void)
105 {
106         if ( first == 0 ) 
107         {
108                 static char bufenv[PATH_MAX + 16];
109                 first++;
110
111 #ifdef __APPLE__
112                 /* /tmp is a symbolic link to /private/tmp under MacOS */
113                 realpath("/tmp/", tmp_dir);
114                 sprintf(tmp_dir,"%s/SD_%d_",tmp_dir, (int) getpid());
115 #else
116                 sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
117 #endif
118                 createdirectory(tmp_dir) ;
119
120                 sprintf(bufenv,"TMPDIR=%s",tmp_dir);
121                 putenv(bufenv);
122         }
123 }
124 #endif
125 /*--------------------------------------------------------------------------*/
126 /**
127 * creates a tmp dir for a scilab session 
128 * and fixes the TMPDIR env variable
129 */
130 void C2F(settmpdir)(void)
131 {
132         createScilabTMPDIR();
133 }
134 /*--------------------------------------------------------------------------*/
135 char *getTMPDIR(void)
136 {
137         return strdup(tmp_dir);
138 }
139 /*--------------------------------------------------------------------------*/
140 /**
141 * remove TMPDIR directory
142 */
143 void C2F(tmpdirc)(void)
144 {
145         removedir(tmp_dir);
146 }
147 /*--------------------------------------------------------------------------*/