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)
25 #include <sys/types.h>
29 extern char *getenv();
36 #include "strdup_windows.h"
38 #include "MALLOC.h" /* MALLOC */
40 #include "localization.h"
41 #include "charEncoding.h"
44 #include "removedir.h"
45 #include "createdirectory.h"
46 /*--------------------------------------------------------------------------*/
47 static char tmp_dir[PATH_MAX + FILENAME_MAX + 1];
48 static int alreadyCreated = 0;
49 /*--------------------------------------------------------------------------*/
51 void createScilabTMPDIR(void)
53 wchar_t wcTmpDirDefault[PATH_MAX];
54 if (!GetTempPathW(PATH_MAX, wcTmpDirDefault))
56 MessageBox(NULL, _("Cannot find Windows temporary directory (1)."), _("Error"), MB_ICONERROR);
61 wchar_t wctmp_dir[PATH_MAX + FILENAME_MAX + 1];
62 static wchar_t bufenv[PATH_MAX + 16];
64 swprintf(wctmp_dir, PATH_MAX + FILENAME_MAX + 1, L"%sSCI_TMP_%d_", wcTmpDirDefault, (int) _getpid());
65 if ( CreateDirectoryW(wctmp_dir, NULL) == FALSE)
67 DWORD attribs = GetFileAttributesW (wctmp_dir);
68 if (attribs & FILE_ATTRIBUTE_DIRECTORY)
70 /* Repertoire existant */
77 wsprintf(MsgErr, _("Impossible to create : %s"), tmp_dir);
78 MessageBox(NULL, MsgErr, _("Error"), MB_ICONERROR);
83 GetTempPathW(PATH_MAX, wcTmpDirDefault);
84 wcscpy(wctmp_dir, wcTmpDirDefault);
85 wctmp_dir[wcslen(wctmp_dir) - 1] = '\0'; /* Remove last \ */
91 swprintf(bufenv, PATH_MAX + 16, L"TMPDIR=%s", wctmp_dir);
94 TmpDir = wide_string_to_UTF8(wctmp_dir);
97 strcpy(tmp_dir, TmpDir);
108 void createScilabTMPDIR(void)
112 if ( alreadyCreated == 0 )
114 static char bufenv[PATH_MAX + 16];
116 /* If the env variable TMPDIR is set, honor this preference */
117 if ((tmpdir = getenv("TMPDIR")) != NULL &&
118 strlen(tmpdir) < (PATH_MAX) && strstr(tmpdir, "SCI_TMP_") == NULL)
120 /* TMPDIR does not contains SCI_TMP. Using TMPDIR */
121 strcpy(tmp_dir, tmpdir);
125 /* Two cases where we can be here:
126 * - When TMPDIR is not set (all cases)
127 * - If SCI_TMP_* is in the TMPDIR variable, switch to the default
129 strcpy(tmp_dir, "/tmp");
132 /* XXXXXX will be randomized by mkdtemp */
134 char *tmp_dir_strdup[PATH_MAX];
135 char *res = realpath(tmp_dir, tmp_dir_strdup);
138 fprintf(stderr, _("Warning: Could not resolve the realpath of %s.\n"), tmp_dir);
140 sprintf(tmp_dir, "%s/SCI_TMP_%d_XXXXXX", tmp_dir_strdup, (int) getpid());
142 if (mkdtemp(tmp_dir) == NULL)
144 fprintf(stderr, _("Error: Could not create %s: %s\n"), tmp_dir, strerror(errno));
147 sprintf(bufenv, "TMPDIR=%s", tmp_dir);
152 /*--------------------------------------------------------------------------*/
154 * creates a tmp dir for a scilab session
155 * and fixes the TMPDIR env variable
157 void C2F(settmpdir)(void)
159 createScilabTMPDIR();
161 /*--------------------------------------------------------------------------*/
162 char *getTMPDIR(void)
164 return strdup(tmp_dir);
166 /*--------------------------------------------------------------------------*/
168 * remove TMPDIR directory
170 void C2F(tmpdirc)(void)
174 /*--------------------------------------------------------------------------*/
175 wchar_t *getTMPDIRW(void)
177 return to_wide_string(tmp_dir);
179 /*--------------------------------------------------------------------------*/