* bug #3131: similar to commit 3be48509d9fd633e7637ba5bc8203f564897ec81
Sylvestre Ledru [Wed, 13 May 2009 11:02:14 +0000 (13:02 +0200)]
but this one is dealing with the C realpath function.
the second argument of realpath is the full real path, there is no way
to know before the size of the result
This case is handled by the option -D_FORTIFY_SOURCE=2 which is added by
default in gcc under Ubuntu which is the operating system used to build
Scilab.
-D_FORTIFY_SOURCE=2 is changing realpath to __realpath_chk to perform
this
operation. However, the problem is that __realpath_chk is only available
in glibc >= 2.4
* add some error messages/feedbacks when an error is found in realpath

scilab/modules/fileio/sci_gateway/c/sci_fullpath.c
scilab/modules/fileio/src/c/filesmanagement.c

index 4153a0e..dbbffd0 100644 (file)
@@ -14,6 +14,7 @@
 #ifndef _MSC_VER
 #include <sys/param.h>
 #endif
+#include <limits.h>
 #include <stdlib.h>
 #include "gw_fileio.h"
 #include "stack-c.h"
 #include "PATH_MAX.h"
 #include "charEncoding.h"
 /*--------------------------------------------------------------------------*/
-#ifndef _MSC_VER
-#define _fullpath(a,r,l)        realpath(r,a)
-#endif
-/*--------------------------------------------------------------------------*/
 int C2F(sci_fullpath)(char *fname,unsigned long fname_len)
 {
        Rhs=Max(Rhs,0);
@@ -36,20 +33,24 @@ int C2F(sci_fullpath)(char *fname,unsigned long fname_len)
        if (GetType(1) == sci_strings)
        {
                static int l1,n1,m1;
-               char *relPath = NULL;
+               char relPath[PATH_MAX];
                char szTemp[bsiz];
-               char fullpath[PATH_MAX*4];
+               char *fullpath=NULL;
                
                GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
                /* Bug 3089 */
-               relPath = UTFToLocale(cstk(l1), szTemp);
-
-               if( _fullpath( fullpath, relPath, PATH_MAX*4 ) != NULL )
+               strcpy(relPath,UTFToLocale(cstk(l1), szTemp));
+#ifdef _MSC_VER
+               if( _fullpath( fullpath, relPath, MAXPATHLEN ) != NULL )
+#else
+                 fullpath=realpath( relPath, NULL );
+               if (fullpath != NULL )
+#endif
                {
                        char *Output=NULL;
                        Output=(char*)MALLOC((strlen(fullpath)+1)*sizeof(char));
                        strcpy(Output,fullpath);
-
+                       FREE(fullpath);
                        n1=1;
                        CreateVarFromPtr( Rhs+1,STRING_DATATYPE,(m1=(int)strlen(Output), &m1),&n1,&Output);
                        LhsVar(1) = Rhs+1;
index c8efddd..17b455b 100644 (file)
@@ -14,6 +14,7 @@
 #endif
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
 #include "PATH_MAX.h"
 #include "filesmanagement.h"
 #include "core_math.h" /* Min Max */
 #include "strdup_windows.h"
 #endif
 /*--------------------------------------------------------------------------*/
-#ifndef _MSC_VER
-#define _fullpath(a,r,l) realpath(r,a)
-#endif
-/*--------------------------------------------------------------------------*/
 typedef struct {
        FILE *ftformat;
        int ftswap; /* swap status for each file */
@@ -120,11 +117,11 @@ char* GetFileNameOpenedInScilab(int Id)
        return NULL;
 }
 /*--------------------------------------------------------------------------*/
-BOOL SetFileNameOpenedInScilab(int Id,char *name)
+BOOL SetFileNameOpenedInScilab(int Id, char *name)
 {
        BOOL bOK=FALSE;
        char *ptrName=NULL;
-       char fullpath[PATH_MAX*4];
+       char *fullpath=NULL;
 
        /* A exception for Id 5 and 6 */
        /* no filename */
@@ -138,9 +135,15 @@ BOOL SetFileNameOpenedInScilab(int Id,char *name)
        }
        else
        {
-               if( _fullpath( fullpath, name, PATH_MAX*4 ) != NULL )
+#ifdef _MSC_VER
+               if( _fullpath( fullpath, name, PATH_MAX ) != NULL )
+#else
+                 fullpath=realpath( name, NULL );
+                 if (fullpath != NULL )
+#endif
                {
                        ptrName = strdup(fullpath);
+                       FREE(fullpath);
                        if (ptrName)
                        {
                                bOK=TRUE;
@@ -148,11 +151,12 @@ BOOL SetFileNameOpenedInScilab(int Id,char *name)
                }
                else
                {
-                       ptrName = strdup(name);
-                       if (ptrName)
-                       {
-                               bOK=TRUE;
-                       }
+                 fprintf(stderr, _("An error occurred while trying to retrieve the realpath of %s: %s\n"),name,strerror(errno));
+                 ptrName = strdup(name);
+                 if (ptrName)
+                   {
+                     bOK=TRUE;
+                   }
                }
        }
        ScilabFileList[Id].ftname = ptrName;
@@ -234,22 +238,30 @@ BOOL IsAlreadyOpenedInScilab(char *filename)
 {
        if (ScilabFileList)
        {
-               char fullpath[PATH_MAX*4];
+         char *fullpath=NULL;
                int i=0;
-
-               if( _fullpath( fullpath, filename, PATH_MAX*4 ) == NULL )
+#ifdef _MSC_VER
+               if( _fullpath( fullpath, filename, PATH_MAX ) == NULL )
+#else
+                 fullpath = realpath( filename, NULL);
+                 if( fullpath == NULL )
+#endif
                {
-                       /* if we are a problem */
-                       strcpy(fullpath,filename);
+                  fprintf(stderr, _("An error occurred while trying to retrieve the realpath of %s: %s\n"),filename, strerror(errno));
+                 /* if we have a problem */
+                 strcpy(fullpath,filename);
                }
 
                for (i=0;i<CurrentMaxFiles;i++)
                {
                        if ( (ScilabFileList[i].ftformat) && ScilabFileList[i].ftname)
                        {
-                               if (strcmp(ScilabFileList[i].ftname,fullpath) == 0) return TRUE;
+                         if (strcmp(ScilabFileList[i].ftname,fullpath) == 0) {
+                           return TRUE;
+                         }
                        }
                }
+               FREE(fullpath);
        }
        return FALSE;
 }