Avoid dependency on GLIBC_2.4 (__realpath_chk)
[scilab.git] / scilab / modules / core / src / c / tmpdir.c
index 1a411f3..2026d16 100644 (file)
-/*-----------------------------------------------------------------------------------*/
-/* Scilab */
-/*-----------------------------------------------------------------------------------*/
+/*
+ * 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
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
 #include <stdio.h>
-#include "machine.h"
-#include "../../../io/includes/createdir.h"
-#include "MALLOC.h" /* MALLOC */
+#include <string.h>
 
-#if _MSC_VER
+#ifdef _MSC_VER
 #include <windows.h>
+#include <process.h>
 #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
 
-#if (defined _MSC_VER)
-#define putenv(x) _putenv(x)
-#endif
+#include <errno.h>
 
-#if defined(_MSC_VER)
+#ifdef _MSC_VER
 #include <process.h>
+#include "strdup_windows.h"
 #endif
-#include "prompt.h"
+#include "MALLOC.h" /* MALLOC */
 #include "tmpdir.h"
-/*-----------------------------------------------------------------------------------*/
-static char tmp_dir[256],buf[256];
-/*-----------------------------------------------------------------------------------*/
-/**
-* creates a tmp dir for a scilab session 
-* and fixes the TMPDIR env variable
-*/
-void C2F(settmpdir)(void)
+#include "localization.h"
+#include "charEncoding.h"
+#include "MALLOC.h"
+#include "PATH_MAX.h"
+#include "removedir.h"
+#include "createdirectory.h"
+/*--------------------------------------------------------------------------*/
+static char tmp_dir[PATH_MAX + FILENAME_MAX + 1];
+static int alreadyCreated = 0;
+/*--------------------------------------------------------------------------*/
+#ifdef _MSC_VER
+void createScilabTMPDIR(void)
 {
-#if _MSC_VER
-       #define PATH_MAX 1024
-       char TmpDirDefault[PATH_MAX];
+    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
-  static int first =0;
-  if ( first == 0 ) 
+            }
+        }
+
+        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, "");
+        }
+    }
+}
+#else
+
+void createScilabTMPDIR(void)
+{
+    char *tmpdir;
+
+    if ( alreadyCreated == 0 )
     {
-      first++;
-#ifdef _MSC_VER 
-         if (!GetTempPath(PATH_MAX,TmpDirDefault))
-         {
-                 MessageBox(NULL,"Don''t find Windows temporary directory","Error",MB_ICONERROR);
-                 exit(1);
-         }
-         else
-         {
-                 sprintf(tmp_dir,"%sSCI_TMP_%d_",TmpDirDefault,(int) _getpid());
-         }
+        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 */
+
+        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 ( CreateDirectory(tmp_dir,NULL)==FALSE)
-      {
-               DWORD attribs=GetFileAttributes (tmp_dir); 
+        if (mkdtemp(tmp_dir) == NULL)
+        {
+            fprintf(stderr, _("Error: Could not create %s: %s\n"), tmp_dir, strerror(errno));
+        }
 
-               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
-                 GetTempPath(PATH_MAX,TmpDirDefault);
-                 sprintf(tmp_dir,"%s",TmpDirDefault);
-                 tmp_dir[strlen(tmp_dir)-1]='\0'; /* Remove last \ */
-                 #endif
-               }
-  }
-#else 
-  sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
-  CreateDir(tmp_dir) ;
-#endif 
-  sprintf(buf,"TMPDIR=%s",tmp_dir);
-  putenv(buf);
+        sprintf(bufenv, "TMPDIR=%s", tmp_dir);
+        putenv(bufenv);
     }
 }
-/*-----------------------------------------------------------------------------------*/
+#endif
+/*--------------------------------------------------------------------------*/
 /**
-* get a reference to tmp_dir 
+* creates a tmp dir for a scilab session
+* and fixes the TMPDIR env variable
 */
-char *get_sci_tmp_dir(void)
+void C2F(settmpdir)(void)
 {
-  /* just in case */
-  C2F(settmpdir)();
-  return tmp_dir;
+    createScilabTMPDIR();
 }
-/*-----------------------------------------------------------------------------------*/
+/*--------------------------------------------------------------------------*/
+char *getTMPDIR(void)
+{
+    return strdup(tmp_dir);
+}
+/*--------------------------------------------------------------------------*/
 /**
 * remove TMPDIR directory
 */
 void C2F(tmpdirc)(void)
 {
-  char *tmp_dir2 = get_sci_tmp_dir(); 
-  RemoveDir(tmp_dir2);
+    removedir(tmp_dir);
+}
+/*--------------------------------------------------------------------------*/
+wchar_t *getTMPDIRW(void)
+{
+    return to_wide_string(tmp_dir);
 }
-/*-----------------------------------------------------------------------------------*/
+/*--------------------------------------------------------------------------*/