strdup
Allan Cornet [Tue, 15 Apr 2008 12:59:06 +0000 (12:59 +0000)]
16 files changed:
scilab/modules/core/sci_gateway/c/sci_getversion.c
scilab/modules/core/sci_gateway/c/sci_what.c
scilab/modules/core/sci_gateway/c/sci_who.c
scilab/modules/core/src/c/CallScilab.c
scilab/modules/core/src/c/GetXmlFileEncoding.c
scilab/modules/core/src/c/SendScilabJobs.c
scilab/modules/core/src/c/commandwords.c
scilab/modules/core/src/c/filesmanagement.c
scilab/modules/core/src/c/getcommandlineargs.c
scilab/modules/core/src/c/getmodules.c
scilab/modules/core/src/c/hashtable_core.c
scilab/modules/core/src/c/librarieslist.c
scilab/modules/core/src/c/libraryinfo.c
scilab/modules/core/src/c/loadversion.c
scilab/modules/core/src/c/stackinfo.c
scilab/modules/core/src/c/tmpdir.c

index c023378..767b533 100644 (file)
@@ -24,6 +24,9 @@
 #include "withtk.h"
 #include "localization.h"
 #include "with_pvm.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 static int getversion_no_rhs(void);
 static int getversion_one_rhs(void);
@@ -268,8 +271,7 @@ static int getversion_two_rhs(void)
                        if (strcmp(ParamRhs1,"scilab") == 0)
                        {
                                char *output=NULL ;
-                               output=(char*)MALLOC((strlen(SCI_VERSION_STRING)+1)*sizeof(char));
-                               strcpy(output,SCI_VERSION_STRING);
+                               output = strdup(SCI_VERSION_STRING);
 
                                n1=1;
                                CreateVarFromPtr(Rhs+ 1,STRING_DATATYPE,(m1=(int)strlen(output), &m1),&n1,&output);
@@ -291,9 +293,8 @@ static int getversion_two_rhs(void)
 
                                if (getversionmodule(ParamRhs1,&version_module_major,&version_module_minor,&version_module_maintenance,versionstring,&version_module_revision))
                                {
-                                       char *output=NULL ;
-                                       output=(char*)MALLOC((LineMax+1)*sizeof(char));
-                                       strcpy(output,versionstring);
+                                       char *output = NULL ;
+                                       output = strdup(versionstring);
 
                                        n1=1;
                                        CreateVarFromPtr(Rhs+ 1,STRING_DATATYPE,(m1=(int)strlen(output), &m1),&n1,&output);
index 5b40c7c..f22fe90 100644 (file)
@@ -9,6 +9,7 @@
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
+#include <string.h>
 #include <stdio.h>
 #include "gw_core.h"
 #include "machine.h"
@@ -18,6 +19,9 @@
 #include "Scierror.h"
 #include "commandwords.h"
 #include "localization.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 static char **LocalFunctionsTab=NULL;
 static int SizeLocalFunctionsTab=0;
@@ -173,8 +177,7 @@ static int CreateLocalFunctionsTab(void)
                {
                        if ( !IsACommand(LocalFunctionsTabTmp[i]) ) 
                        {
-                               LocalFunctionsTab[j]=(char*)MALLOC( (strlen(LocalFunctionsTabTmp[i])+1)*sizeof(char) );
-                               strcpy(LocalFunctionsTab[j],LocalFunctionsTabTmp[i]);
+                               LocalFunctionsTab[j] = strdup(LocalFunctionsTabTmp[i]);
                                j++;
                        }
                }
index 1255808..46a99a8 100644 (file)
@@ -9,6 +9,7 @@
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
+#include <string.h>
 #include "gw_core.h"
 #include "machine.h"
 #include "stack-c.h"
@@ -17,6 +18,9 @@
 #include "sciprint.h"
 #include "localization.h"
 #include "Scierror.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 struct VariableStruct
 {
@@ -336,8 +340,7 @@ static BOOL FreeVariableStructArray(struct VariableStruct* Vstruct,int lenStruct
 
         for (i=0;i<lenStructArray;i++)
         {
-                Tab[i] = (char*)MALLOC(sizeof(char)*(strlen(Vstruct[i].NameVariable)+1));
-                strcpy(Tab[i],Vstruct[i].NameVariable);
+                Tab[i] = strdup(Vstruct[i].NameVariable);
                 Size[i]=Vstruct[i].SizeVariable;
         }
 
@@ -385,8 +388,7 @@ static BOOL FreeVariableStructArray(struct VariableStruct* Vstruct,int lenStruct
         {
                 if (strlen(Vstruct[i].NameVariable)>0)
                 {
-                        LocalTab[i] = (char*)MALLOC(sizeof(char)*strlen(Vstruct[i].NameVariable)+1);
-                        strcpy(LocalTab[i],Vstruct[i].NameVariable);
+                        LocalTab[i] = strdup(Vstruct[i].NameVariable);
                 }
         }
 
index 698fc5a..bc73a82 100644 (file)
 #ifdef _MSC_VER
 #include "../../../windows_tools/src/c/scilab_windows/SetScilabEnvironmentVariables.h"
 #include "../../../windows_tools/src/c/scilab_windows/getScilabDirectory.h"
+#include "strdup_windows.h"
 #endif
 #include "localization.h"
 
 #ifdef _MSC_VER
 #define putenv _putenv
 #endif
-
 /*--------------------------------------------------------------------------*/
 static int StartScilabIsOK=FALSE;
 /*--------------------------------------------------------------------------*/
@@ -97,13 +97,11 @@ int StartScilab(char *SCIpath,char *ScilabStartup,int *Stacksize)
 
        if (ScilabStartup==NULL)
        {
-               ScilabStartupUsed=(char*)MALLOC((strlen(DEFAULTSCILABSTARTUP)+1)*sizeof(char));
-               strcpy(ScilabStartupUsed,DEFAULTSCILABSTARTUP);
+               ScilabStartupUsed = strdup(DEFAULTSCILABSTARTUP);
        }
        else
        {
-               ScilabStartupUsed=(char*)MALLOC((strlen(DEFAULTSCILABSTARTUP)+1)*sizeof(char));
-               strcpy(ScilabStartupUsed,ScilabStartup);
+               ScilabStartupUsed = strdup(ScilabStartup);
        }
 
        if (Stacksize==NULL)
index 5b92172..c648142 100644 (file)
@@ -9,9 +9,13 @@
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
+#include <string.h>
 #include "GetXmlFileEncoding.h"
 #include "libxml/xmlreader.h"
 #include "MALLOC.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/ 
 char *GetXmlFileEncoding(const char *filename)
 {
@@ -20,8 +24,7 @@ char *GetXmlFileEncoding(const char *filename)
        xmlDocPtr doc = NULL;
 
        /* default */
-       encoding=(char *)MALLOC(sizeof(char)*(strlen(DEFAULT_ENCODING)+1));
-       strcpy(encoding,DEFAULT_ENCODING);
+       encoding = strdup(DEFAULT_ENCODING);
 
        doc = xmlParseFile (filename);
        if (doc) 
@@ -29,8 +32,7 @@ char *GetXmlFileEncoding(const char *filename)
                if (doc->encoding)
                {
                        if (encoding) {FREE(encoding);encoding=NULL;}
-                       encoding=(char *)MALLOC(sizeof(char)*(strlen(doc->encoding)+1));
-                       strcpy(encoding,doc->encoding);
+                       encoding = strdup(doc->encoding);
                }
        }
 
index 32da1a2..8119025 100644 (file)
@@ -13,6 +13,9 @@
 #include "MALLOC.h"
 #include "scirun.h"
 #include "localization.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove);
 static BOOL RemoveComments(char *line);
@@ -101,10 +104,9 @@ static BOOL SetLastJob(char *JOB)
 
        if (JOB)
        {
-               lastjob = MALLOC(sizeof(char)*(strlen(JOB)+1)); 
+               lastjob = strdup(JOB);
                if (lastjob)
                {
-                       strcpy(lastjob,JOB);
                        return TRUE;
                }
        }
@@ -149,12 +151,8 @@ int SendScilabJobs(char **jobs,int numberjobs)
                                if (jobs[i])
                                {
                                        nbcharsjobs = nbcharsjobs+(int)strlen(jobs[i]);
-                                       LOCALJOBS[i]=(char*)MALLOC( sizeof(char)*(strlen(jobs[i])+BUFFERSECURITYSIZE) );
-                                       if (LOCALJOBS[i])
-                                       {
-                                               strcpy(LOCALJOBS[i],jobs[i]);
-                                       }
-                                       else
+                                       LOCALJOBS[i] = strdup(jobs[i]);
+                                       if (LOCALJOBS[i] == NULL)
                                        {
                                                CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
                                                fprintf(stderr,"Error : SendScilabJobs (1) 'LOCALJOBS[%d] MALLOC'.\n",i);       
index e79af58..5ed993a 100644 (file)
 #include "machine.h" 
 #include "commandwords.h"
 #include "MALLOC.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
+
 /*--------------------------------------------------------------------------*/ 
 /* if you update command.f , please update this file too ... */
 /*--------------------------------------------------------------------------*/ 
@@ -49,13 +53,7 @@ char **getcommandkeywords(int *sizearray)
                int i = 0;
                for (i = 0;i < nbrCommands; i++)
                {
-                       char *commandword = NULL;
-                       commandword = (char*)MALLOC(sizeof(char)*(strlen(CommandWords[i])+1));
-                       if (commandword) 
-                       {
-                               strcpy(commandword,CommandWords[i]);
-                               keywords[i] = commandword;
-                       }
+                       keywords[i] = strdup(CommandWords[i]);
                }
                *sizearray =  nbrCommands;
 
index 2fa1fd4..fa541ba 100644 (file)
 #include "machine.h"
 #include "filesmanagement.h"
 #include "core_math.h" /* Min Max */
-#include "MALLOC.h" 
+#include "MALLOC.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 #ifndef _MSC_VER
 #define _fullpath(a,r,l) realpath(r,a)
@@ -114,10 +117,9 @@ BOOL SetFileNameOpenedInScilab(int Id,char *name)
        /* no filename */
        if ( strcmp(name,"") == 0 )
        {
-               ptrName=(char*)MALLOC(sizeof(char)*(strlen(name)+1));
+               ptrName = strdup(name);
                if (ptrName)
                {
-                       strcpy(ptrName,name);
                        bOK=TRUE;
                }
        }
@@ -125,19 +127,17 @@ BOOL SetFileNameOpenedInScilab(int Id,char *name)
        {
                if( _fullpath( fullpath, name, PATH_MAX*4 ) != NULL )
                {
-                       ptrName=(char*)MALLOC(sizeof(char)*(strlen(fullpath)+1));
+                       ptrName = strdup(fullpath);
                        if (ptrName) 
                        {
-                               strcpy(ptrName,fullpath);
                                bOK=TRUE;
                        }
                }
                else
                {
-                       ptrName=(char*)MALLOC(sizeof(char)*(strlen(name)+1));
+                       ptrName = strdup(name);
                        if (ptrName) 
                        {
-                               strcpy(ptrName,name);
                                bOK=TRUE;
                        }
                }
index 2896d8f..00850bd 100644 (file)
@@ -13,8 +13,9 @@
 #include <string.h>
 #include "getcommandlineargs.h"
 #include "MALLOC.h"
-/*--------------------------------------------------------------------------*/ 
-
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/ 
 static int scilab_nbargs = 0;
 static char *scilab_argv[MAXCMDTOKENS];
@@ -34,8 +35,7 @@ char ** getCommandLineArgs(int *nbargs)
                {
                        for (i = 0;i < scilab_nbargs;i++)
                        {
-                               argv[i] = (char *)MALLOC(sizeof(char)*(strlen(scilab_argv[i])+1));
-                               if (argv[i]) strcpy(argv[i],scilab_argv[i]);
+                               argv[i] = strdup(scilab_argv[i]);
                        }
                }
        }
index d2cfd93..c5e475d 100644 (file)
@@ -11,6 +11,7 @@
  */
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <libxml/xpath.h>
 #include <libxml/xmlreader.h>
 #include "getmodules.h"
@@ -24,6 +25,9 @@
 #include "GetXmlFileEncoding.h"
 #include "scilabDefaults.h"
 #include "../../../fileio/includes/FileExist.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/ 
 static struct MODULESLIST *ScilabModules=NULL;
 /*--------------------------------------------------------------------------*/ 
@@ -180,8 +184,7 @@ static BOOL AppendModules(char *xmlfilename)
                                                { 
                                                        /* we found the tag name */
                                                        const char *str=(const char*)attrib->children->content;
-                                                       name=(char*)MALLOC(sizeof(char)*(strlen((const char*)str)+1));
-                                                       if (name) strcpy(name,str);
+                                                       name = strdup(str);
                                                }
                                                else if (xmlStrEqual (attrib->name, (const xmlChar*) "activate"))
                                                { 
@@ -203,8 +206,7 @@ static BOOL AppendModules(char *xmlfilename)
 
                                                        ScilabModules->numberofModules=indice+1;
                                                        
-                                                       ScilabModules->ModuleList[indice]=(char*)MALLOC(sizeof(char)*(strlen(name)+1));
-                                                       strcpy(ScilabModules->ModuleList[indice],name);
+                                                       ScilabModules->ModuleList[indice]= strdup(name);
                                                        indice++;
                                                }
                                                else
index a794181..d7cb6f7 100644 (file)
@@ -15,6 +15,9 @@
 #include "hashtable_core.h"
 #include "getfunctionslist.h"
 #include "existfunction.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 static _ENTRY   * htable = NULL;
 static unsigned   hashtableSize;
@@ -216,8 +219,7 @@ char **GetFunctionsList(int *sizeList)
        {
                if (htable[i].entry.namefunction)
                {
-                       ListFunctions[j]=(char*)MALLOC(sizeof(char)*(strlen(htable[i].entry.namefunction)+1));
-                       strcpy(ListFunctions[j],htable[i].entry.namefunction);
+                       ListFunctions[j] = strdup(htable[i].entry.namefunction);
                        j++;
                }
        }
index fe45c18..e6a0774 100644 (file)
@@ -16,6 +16,9 @@
 #include "librarieslist.h"
 #include "stackinfo.h"
 #include "MALLOC.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 static int getnumberoflibraries(void);
 /*--------------------------------------------------------------------------*/
@@ -42,9 +45,7 @@ char **getlibrarieslist(int *sizearray)
                                int *header = istk( iadr(*Lstk(fin)));
                                if ( (header) && (header[0] == sci_lib ) ) 
                                {
-
-                                       librarieslist[i]= (char*)MALLOC(sizeof(char)*(strlen(NameVariable)+1));
-                                       strcpy(librarieslist[i],NameVariable);
+                                       librarieslist[i] = strdup(NameVariable);
                                        i++;
                                }
                        }
index 9e58c48..fc75d94 100644 (file)
@@ -23,6 +23,9 @@
 #include "MALLOC.h"
 #include "cvstr.h"
 #include "cluni0.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 char *getlibrarypath(char *libraryname)
 {
@@ -81,8 +84,7 @@ char **getlistmacrosfromlibrary(char *libraryname,int *sizearray)
                                        if (macroslist) macroslist = (char**)REALLOC(macroslist,sizeof(char*)*(nbElements+1));
                                        else macroslist =(char**)MALLOC(sizeof(char*)*(nbElements+1));
 
-                                       macroslist[nbElements] = (char*)MALLOC(sizeof(char)*(strlen(line)+1));
-                                       if (macroslist[nbElements]) strcpy(macroslist[nbElements],line);
+                                       macroslist[nbElements] = strdup(line);
                                        nbElements++;
                                }
                                fclose(pFile);
index 2fed3b7..3c84076 100644 (file)
@@ -21,6 +21,9 @@
 #include "localization.h"
 #include "stricmp.h"
 #include "../../../fileio/includes/FileExist.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/ 
 BOOL getversionmodule(char *modulename,
                                          int *sci_version_major,
@@ -106,9 +109,8 @@ BOOL getversionmodule(char *modulename,
                                                        else if (xmlStrEqual (attrib->name, (const xmlChar*)"string"))
                                                                {
                                                                        /* we found <string> */
-                                                                       const char *str=(const char*)attrib->children->content;
-                                                                       version_string=(char*)MALLOC(sizeof(char)*(strlen((const char*)str)+1));
-                                                                       strcpy(version_string,str);
+                                                                       const char *str = (const char*)attrib->children->content;
+                                                                       version_string= strdup(str);
                                                                }
 
                                                        attrib = attrib->next;
index 8b7fb05..0efd862 100644 (file)
@@ -9,6 +9,7 @@
  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  *
  */
+#include <string.h>
 #ifdef HAVE_LIMITS_H
 #include <limits.h>
 #define MAXLONG LONG_MAX
@@ -21,6 +22,9 @@
 #include "stack-def.h"
 #include "stack-c.h"
 #include "MALLOC.h"
+#ifdef _MSC_VER
+#include "strdup_windows.h"
+#endif
 /*--------------------------------------------------------------------------*/
 #ifndef MAXLONG
 #define MAXLONG LONG_MAX
@@ -114,8 +118,7 @@ char *getLocalNamefromId(int n)
 
        if (strlen(fortranName) > 0)
        {
-               Name = (char*)MALLOC(sizeof(char)*(strlen(fortranName)+1));
-               if (Name) strcpy(Name,fortranName);
+               Name = strdup(fortranName);
        }
        return Name;
 }
@@ -136,8 +139,7 @@ char *getGlobalNamefromId(int n)
 
        if (strlen(fortranName) > 0)
        {
-               Name = (char*)MALLOC(sizeof(char)*(strlen(fortranName)+1));
-               if (Name) strcpy(Name,fortranName);
+               Name = strdup(fortranName);
        }
        return Name;
 }
index 19520ba..a7625c4 100644 (file)
@@ -10,6 +10,7 @@
  *
  */
 #include <stdio.h>
+#include <string.h>
 #include "machine.h"
 #include "../../../fileio/includes/removedir.h"
 #include "../../../fileio/includes/createdirectory.h"
@@ -35,6 +36,7 @@ extern  char  *getenv();
 
 #ifdef _MSC_VER
 #include <process.h>
+#include "strdup_windows.h"
 #endif
 #include "prompt.h"
 #include "tmpdir.h"
@@ -119,10 +121,6 @@ void C2F(tmpdirc)(void)
 /*--------------------------------------------------------------------------*/
 char *getTMPDIR(void)
 {
-       char *TMPDIR=NULL;
-       TMPDIR=(char*)MALLOC(sizeof(char)*(strlen(tmp_dir)+1));
-       strcpy(TMPDIR,tmp_dir);
-
-       return TMPDIR;
+       return strdup(tmp_dir);
 }
 /*--------------------------------------------------------------------------*/