Avoid dependency on GLIBC_2.4 (__realpath_chk)
[scilab.git] / scilab / modules / core / src / c / tmpdir.c
index 015288b..2026d16 100644 (file)
-/* Copyright INRIA/ENPC */
-
-#include "machine.h"
+/*
+ * 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 <string.h>
 
-
-#include "MALLOC.h" /* MALLOC */
-
-#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];
-#endif
-  static int first =0;
-  if ( first == 0 ) 
+    wchar_t wcTmpDirDefault[PATH_MAX];
+    if (!GetTempPathW(PATH_MAX, wcTmpDirDefault))
     {
-      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());
-         }
-
-         if ( CreateDirectory(tmp_dir,NULL)==FALSE)
-      {
-               DWORD attribs=GetFileAttributes (tmp_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);
-                 #endif
-
-                 GetTempPath(PATH_MAX,TmpDirDefault);
-                 sprintf(tmp_dir,"%s",TmpDirDefault);
-                 tmp_dir[strlen(tmp_dir)-1]='\0'; /* Remove last \ */
-
-               }
-  }
-#else 
-  sprintf(tmp_dir,"/tmp/SD_%d_",(int) getpid());
-  sprintf(buf,"umask 000;if test ! -d %s; then mkdir %s; fi ",tmp_dir,tmp_dir);
-  system(buf);
-#endif 
-  sprintf(buf,"TMPDIR=%s",tmp_dir);
-  putenv(buf);
+        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);
+
+        TmpDir = wide_string_to_UTF8(wctmp_dir);
+        if (TmpDir)
+        {
+            strcpy(tmp_dir, TmpDir);
+            FREE(TmpDir);
+        }
+        else
+        {
+            strcpy(tmp_dir, "");
+        }
     }
 }
+#else
 
-/****************************
- * get a reference to tmp_dir 
- ****************************/
+void createScilabTMPDIR(void)
+{
+    char *tmpdir;
 
-char *get_sci_tmp_dir(void)
+    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 */
+
+        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
+* and fixes the TMPDIR env variable
+*/
+void C2F(settmpdir)(void)
 {
-  /* just in case */
-  C2F(settmpdir)();
-  return tmp_dir;
+    createScilabTMPDIR();
 }
-
-
-#if _MSC_VER
-/* Remove directory and subdirectories */
-/* A.C INRIA 2005 */
-int DeleteDirectory(char *refcstrRootDirectory)
+/*--------------------------------------------------------------------------*/
+char *getTMPDIR(void)
 {
-       BOOL bDeleteSubdirectories=TRUE;
-       BOOL bSubdirectory = FALSE;
-       HANDLE hFile;
-       char *strFilePath=NULL;
-       char *strPattern=NULL;
-       WIN32_FIND_DATA FileInformation;
-       DWORD dwError;
-
-       strPattern = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5));
-       sprintf(strPattern,"%s\\*.*",refcstrRootDirectory);
-
-       hFile = FindFirstFile(strPattern, &FileInformation);
-       if (strPattern) { FREE(strPattern);strPattern=NULL;}
-
-       if(hFile != INVALID_HANDLE_VALUE)
-       {
-               do
-               {
-                       if(FileInformation.cFileName[0] != '.')
-                       {
-                               if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
-                               strFilePath = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5+strlen(FileInformation.cFileName)));
-                               sprintf(strFilePath,"%s\\%s",refcstrRootDirectory,FileInformation.cFileName);
-
-                               if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
-                               {
-                                       if(bDeleteSubdirectories)
-                                       {
-                                               int iRC = DeleteDirectory(strFilePath);
-                                               if(iRC) return iRC;
-                                       }
-                                       else bSubdirectory = TRUE;
-                               }
-                               else
-                               {
-                                       if(SetFileAttributes(strFilePath,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
-                                       if(DeleteFile(strFilePath) == FALSE) return GetLastError();
-                               }
-                       }
-               } while(FindNextFile(hFile, &FileInformation) == TRUE);
-
-               FindClose(hFile);
-
-               dwError = GetLastError();
-               if(dwError != ERROR_NO_MORE_FILES) return dwError;
-               else
-               {
-                       if(!bSubdirectory)
-                       {
-                               if(SetFileAttributes(refcstrRootDirectory,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
-                               if(RemoveDirectory(refcstrRootDirectory) == FALSE)      return GetLastError();
-                       }
-               }
-       }
-
-       if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
-       if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
-
-       return 0;
+    return strdup(tmp_dir);
 }
-#endif
-
-/*************************************************
- * remove TMPDIR and dynamic link temporary files 
- *************************************************/
-
-#if (defined(hppa))
-  extern void hppa_sci_unlink_shared();
-#endif
-
+/*--------------------------------------------------------------------------*/
+/**
+* remove TMPDIR directory
+*/
 void C2F(tmpdirc)(void)
 {
-  char *tmp_dir2 = get_sci_tmp_dir(); 
-#ifdef _MSC_VER 
-  DeleteDirectory(tmp_dir2);
-#else 
-#if (defined(hppa))
-  hppa_sci_unlink_shared();
-#endif
-  sprintf(buf,"rm -f -r %s >/dev/null  2>/dev/null",tmp_dir2);
-  system(buf);
-  sprintf(buf,"rm -f -r /tmp/%d.metanet.* > /dev/null  2>/dev/null",
-         (int) getpid());
-  system(buf);
-#endif 
+    removedir(tmp_dir);
+}
+/*--------------------------------------------------------------------------*/
+wchar_t *getTMPDIRW(void)
+{
+    return to_wide_string(tmp_dir);
 }
+/*--------------------------------------------------------------------------*/