Avoid dependency on GLIBC_2.4 (__realpath_chk)
[scilab.git] / scilab / modules / core / src / c / tmpdir.c
index 69d9a17..2026d16 100644 (file)
@@ -2,11 +2,12 @@
  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  * Copyright (C) INRIA
  * Copyright (C) DIGITEO - 2009 - Allan CORNET
- * 
+ * Copyright (C) DIGITEO - 2010 - Sylvestre LEDRU
+ *
  * This file must be used under the terms of the CeCILL.
  * This source file is licensed as described in the file COPYING, which
- * you should have received as part of this distribution.  The terms
- * are also available at    
+ * you should have received as part of this distribution. The terms
+ * are also available at
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
 #endif
 
 #if defined(__STDC__) || defined(_MSC_VER)
+#if defined(__linux__)
+#define __USE_FORTIFY_LEVEL 0 /* Avoid dependency on GLIBC_2.4 (__realpath_chk) */
+#endif
 #include <stdlib.h>
 #ifndef _MSC_VER
 #include <sys/types.h>
 #include <unistd.h>
 #endif
-#else 
+#else
 extern  char  *getenv();
 #endif
 
+#include <errno.h>
+
 #ifdef _MSC_VER
 #include <process.h>
 #include "strdup_windows.h"
@@ -41,100 +47,124 @@ extern  char  *getenv();
 #include "removedir.h"
 #include "createdirectory.h"
 /*--------------------------------------------------------------------------*/
-static char tmp_dir[PATH_MAX+FILENAME_MAX+1];
-static int first = 0;
+static char tmp_dir[PATH_MAX + FILENAME_MAX + 1];
+static int alreadyCreated = 0;
 /*--------------------------------------------------------------------------*/
 #ifdef _MSC_VER
 void createScilabTMPDIR(void)
 {
-       wchar_t wcTmpDirDefault[PATH_MAX];
-       if (!GetTempPathW(PATH_MAX,wcTmpDirDefault))
-       {
-               MessageBox(NULL,_("Cannot find Windows temporary directory (1)."),_("Error"),MB_ICONERROR);
-               exit(1);
-       }
-       else
-       {
-               wchar_t wctmp_dir[PATH_MAX+FILENAME_MAX+1];
-               static wchar_t bufenv[PATH_MAX + 16];
-               char *TmpDir = NULL;
-               swprintf(wctmp_dir,PATH_MAX+FILENAME_MAX+1,L"%sSCI_TMP_%d_",wcTmpDirDefault,(int) _getpid());
-               if ( CreateDirectoryW(wctmp_dir,NULL) == FALSE)
-               {
-                       DWORD attribs = GetFileAttributesW (wctmp_dir);
-                       if (attribs & FILE_ATTRIBUTE_DIRECTORY)
-                       {
-                               /* Repertoire existant */
-                       }
-                       else
-                       {
-                               #ifdef _DEBUG
-                               {
-                                       char MsgErr[1024];
-                                       wsprintf(MsgErr,_("Impossible to create : %s"),tmp_dir);
-                                       MessageBox(NULL,MsgErr,_("Error"),MB_ICONERROR);
-                                       exit(1);
-                               }
-                               #else
-                               {
-                                       GetTempPathW(PATH_MAX,wcTmpDirDefault);
-                                       wcscpy(wctmp_dir,wcTmpDirDefault);
-                                       wctmp_dir[wcslen(wctmp_dir)-1]='\0'; /* Remove last \ */
-                               }
-                               #endif
-                       }
-               }
+    wchar_t wcTmpDirDefault[PATH_MAX];
+    if (!GetTempPathW(PATH_MAX, wcTmpDirDefault))
+    {
+        MessageBox(NULL, _("Cannot find Windows temporary directory (1)."), _("Error"), MB_ICONERROR);
+        exit(1);
+    }
+    else
+    {
+        wchar_t wctmp_dir[PATH_MAX + FILENAME_MAX + 1];
+        static wchar_t bufenv[PATH_MAX + 16];
+        char *TmpDir = NULL;
+        swprintf(wctmp_dir, PATH_MAX + FILENAME_MAX + 1, L"%sSCI_TMP_%d_", wcTmpDirDefault, (int) _getpid());
+        if ( CreateDirectoryW(wctmp_dir, NULL) == FALSE)
+        {
+            DWORD attribs = GetFileAttributesW (wctmp_dir);
+            if (attribs & FILE_ATTRIBUTE_DIRECTORY)
+            {
+                /* Repertoire existant */
+            }
+            else
+            {
+#ifdef _DEBUG
+                {
+                    char MsgErr[1024];
+                    wsprintf(MsgErr, _("Impossible to create : %s"), tmp_dir);
+                    MessageBox(NULL, MsgErr, _("Error"), MB_ICONERROR);
+                    exit(1);
+                }
+#else
+                {
+                    GetTempPathW(PATH_MAX, wcTmpDirDefault);
+                    wcscpy(wctmp_dir, wcTmpDirDefault);
+                    wctmp_dir[wcslen(wctmp_dir) - 1] = '\0'; /* Remove last \ */
+                }
+#endif
+            }
+        }
 
-               swprintf(bufenv,PATH_MAX + 16,L"TMPDIR=%s",wctmp_dir);
-               _wputenv(bufenv);
+        swprintf(bufenv, PATH_MAX + 16, L"TMPDIR=%s", wctmp_dir);
+        _wputenv(bufenv);
 
-               TmpDir = wide_string_to_UTF8(wctmp_dir);
-               if (TmpDir)
-               {
-                       strcpy(tmp_dir, TmpDir);
-                       FREE(TmpDir);
-               }
-               else
-               {
-                       strcpy(tmp_dir, "");
-               }
-       }
+        TmpDir = wide_string_to_UTF8(wctmp_dir);
+        if (TmpDir)
+        {
+            strcpy(tmp_dir, TmpDir);
+            FREE(TmpDir);
+        }
+        else
+        {
+            strcpy(tmp_dir, "");
+        }
+    }
 }
 #else
+
 void createScilabTMPDIR(void)
 {
-       if ( first == 0 ) 
-       {
-               static char bufenv[PATH_MAX + 16];
-               first++;
+    char *tmpdir;
 
-#ifdef __APPLE__
-                /* /tmp is a symbolic link to /private/tmp under MacOS */
-                realpath("/tmp/", tmp_dir);
-               sprintf(tmp_dir,"%s/SD_%d_",tmp_dir, (int) getpid());
-#else
-               sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
-#endif
-               createdirectory(tmp_dir) ;
+    if ( alreadyCreated == 0 )
+    {
+        static char bufenv[PATH_MAX + 16];
+        alreadyCreated++;
+        /* If the env variable TMPDIR is set, honor this preference */
+        if ((tmpdir = getenv("TMPDIR")) != NULL &&
+                strlen(tmpdir) < (PATH_MAX) && strstr(tmpdir, "SCI_TMP_") == NULL)
+        {
+            /* TMPDIR does not contains SCI_TMP. Using TMPDIR */
+            strcpy(tmp_dir, tmpdir);
+        }
+        else
+        {
+            /* Two cases where we can be here:
+             *  - When TMPDIR is not set (all cases)
+             *  - If SCI_TMP_* is in the TMPDIR variable, switch to the default
+            */
+            strcpy(tmp_dir, "/tmp");
+        }
+
+        /* XXXXXX will be randomized by mkdtemp */
 
-               sprintf(bufenv,"TMPDIR=%s",tmp_dir);
-               putenv(bufenv);
-       }
+        char *tmp_dir_strdup[PATH_MAX];
+        char *res = realpath(tmp_dir, tmp_dir_strdup);
+        if (!res)
+        {
+            fprintf(stderr, _("Warning: Could not resolve the realpath of %s.\n"), tmp_dir);
+        }
+        sprintf(tmp_dir, "%s/SCI_TMP_%d_XXXXXX", tmp_dir_strdup, (int) getpid());
+
+        if (mkdtemp(tmp_dir) == NULL)
+        {
+            fprintf(stderr, _("Error: Could not create %s: %s\n"), tmp_dir, strerror(errno));
+        }
+
+        sprintf(bufenv, "TMPDIR=%s", tmp_dir);
+        putenv(bufenv);
+    }
 }
 #endif
 /*--------------------------------------------------------------------------*/
 /**
-* creates a tmp dir for a scilab session 
+* creates a tmp dir for a scilab session
 * and fixes the TMPDIR env variable
 */
 void C2F(settmpdir)(void)
 {
-       createScilabTMPDIR();
+    createScilabTMPDIR();
 }
 /*--------------------------------------------------------------------------*/
 char *getTMPDIR(void)
 {
-       return strdup(tmp_dir);
+    return strdup(tmp_dir);
 }
 /*--------------------------------------------------------------------------*/
 /**
@@ -142,11 +172,11 @@ char *getTMPDIR(void)
 */
 void C2F(tmpdirc)(void)
 {
-       removedir(tmp_dir);
+    removedir(tmp_dir);
 }
 /*--------------------------------------------------------------------------*/
 wchar_t *getTMPDIRW(void)
 {
-       return to_wide_string(tmp_dir);
+    return to_wide_string(tmp_dir);
 }
-/*--------------------------------------------------------------------------*/
\ No newline at end of file
+/*--------------------------------------------------------------------------*/