2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) DIGITEO - 2009 - Allan CORNET
5 * Copyright (C) DIGITEO - 2010 - Sylvestre LEDRU
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
22 #if defined(__STDC__) || defined(_MSC_VER)
23 #if defined(__linux__)
24 #define __USE_FORTIFY_LEVEL 0 /* Avoid dependency on GLIBC_2.4 (__realpath_chk) */
28 #include <sys/types.h>
32 extern char *getenv();
39 #include "strdup_windows.h"
41 #include "MALLOC.h" /* MALLOC */
43 #include "localization.h"
44 #include "charEncoding.h"
47 #include "removedir.h"
48 #include "createdirectory.h"
49 /*--------------------------------------------------------------------------*/
50 static char tmp_dir[PATH_MAX + FILENAME_MAX + 1];
51 static int alreadyCreated = 0;
52 /*--------------------------------------------------------------------------*/
54 void createScilabTMPDIR(void)
56 wchar_t wcTmpDirDefault[PATH_MAX];
57 if (!GetTempPathW(PATH_MAX, wcTmpDirDefault))
59 MessageBox(NULL, _("Cannot find Windows temporary directory (1)."), _("Error"), MB_ICONERROR);
64 wchar_t wctmp_dir[PATH_MAX + FILENAME_MAX + 1];
65 static wchar_t bufenv[PATH_MAX + 16];
67 swprintf(wctmp_dir, PATH_MAX + FILENAME_MAX + 1, L"%sSCI_TMP_%d_", wcTmpDirDefault, (int) _getpid());
68 if ( CreateDirectoryW(wctmp_dir, NULL) == FALSE)
70 DWORD attribs = GetFileAttributesW (wctmp_dir);
71 if (attribs & FILE_ATTRIBUTE_DIRECTORY)
73 /* Repertoire existant */
80 wsprintf(MsgErr, _("Impossible to create : %s"), tmp_dir);
81 MessageBox(NULL, MsgErr, _("Error"), MB_ICONERROR);
86 GetTempPathW(PATH_MAX, wcTmpDirDefault);
87 wcscpy(wctmp_dir, wcTmpDirDefault);
88 wctmp_dir[wcslen(wctmp_dir) - 1] = '\0'; /* Remove last \ */
94 swprintf(bufenv, PATH_MAX + 16, L"TMPDIR=%s", wctmp_dir);
97 TmpDir = wide_string_to_UTF8(wctmp_dir);
100 strcpy(tmp_dir, TmpDir);
111 void createScilabTMPDIR(void)
115 if ( alreadyCreated == 0 )
117 static char bufenv[PATH_MAX + 16];
119 /* If the env variable TMPDIR is set, honor this preference */
120 if ((tmpdir = getenv("TMPDIR")) != NULL &&
121 strlen(tmpdir) < (PATH_MAX) && strstr(tmpdir, "SCI_TMP_") == NULL)
123 /* TMPDIR does not contains SCI_TMP. Using TMPDIR */
124 strcpy(tmp_dir, tmpdir);
128 /* Two cases where we can be here:
129 * - When TMPDIR is not set (all cases)
130 * - If SCI_TMP_* is in the TMPDIR variable, switch to the default
132 strcpy(tmp_dir, "/tmp");
135 /* XXXXXX will be randomized by mkdtemp */
137 char *tmp_dir_strdup[PATH_MAX];
138 char *res = realpath(tmp_dir, tmp_dir_strdup);
141 fprintf(stderr, _("Warning: Could not resolve the realpath of %s.\n"), tmp_dir);
143 sprintf(tmp_dir, "%s/SCI_TMP_%d_XXXXXX", tmp_dir_strdup, (int) getpid());
145 if (mkdtemp(tmp_dir) == NULL)
147 fprintf(stderr, _("Error: Could not create %s: %s\n"), tmp_dir, strerror(errno));
150 sprintf(bufenv, "TMPDIR=%s", tmp_dir);
155 /*--------------------------------------------------------------------------*/
157 * creates a tmp dir for a scilab session
158 * and fixes the TMPDIR env variable
160 void C2F(settmpdir)(void)
162 createScilabTMPDIR();
164 /*--------------------------------------------------------------------------*/
165 char *getTMPDIR(void)
167 return strdup(tmp_dir);
169 /*--------------------------------------------------------------------------*/
171 * remove TMPDIR directory
173 void C2F(tmpdirc)(void)
177 /*--------------------------------------------------------------------------*/
178 wchar_t *getTMPDIRW(void)
180 return to_wide_string(tmp_dir);
182 /*--------------------------------------------------------------------------*/