update C2F(getenvc)
Allan Cornet [Fri, 9 Feb 2007 13:22:21 +0000 (13:22 +0000)]
Add C functions on Directories CreateDir,ExistDir,RemoveDir (to check on linux ;) )

scilab/modules/core/src/c/tmpdir.c
scilab/modules/io/Makefile.am
scilab/modules/io/includes/createdir.h [new file with mode: 0644]
scilab/modules/io/src/c/createdir.c [new file with mode: 0644]
scilab/modules/io/src/c/getenvc.c
scilab/modules/io/src/c/io.vcproj

index 06f868d..1a411f3 100644 (file)
@@ -1,9 +1,9 @@
-/* Copyright INRIA/ENPC */
-
-#include "machine.h"
+/*-----------------------------------------------------------------------------------*/
+/* Scilab */
+/*-----------------------------------------------------------------------------------*/
 #include <stdio.h>
-
-
+#include "machine.h"
+#include "../../../io/includes/createdir.h"
 #include "MALLOC.h" /* MALLOC */
 
 #if _MSC_VER
@@ -28,15 +28,13 @@ extern  char  *getenv();
 #endif
 #include "prompt.h"
 #include "tmpdir.h"
-
+/*-----------------------------------------------------------------------------------*/
 static char tmp_dir[256],buf[256];
-
-
-/****************************
- * creates a tmp dir for a scilab session 
- * and fixes the TMPDIR env variable
- ****************************/
-
+/*-----------------------------------------------------------------------------------*/
+/**
+* creates a tmp dir for a scilab session 
+* and fixes the TMPDIR env variable
+*/
 void C2F(settmpdir)(void)
 {
 #if _MSC_VER
@@ -82,114 +80,29 @@ void C2F(settmpdir)(void)
   }
 #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);
+  CreateDir(tmp_dir) ;
 #endif 
   sprintf(buf,"TMPDIR=%s",tmp_dir);
   putenv(buf);
     }
 }
-
-/****************************
- * get a reference to tmp_dir 
- ****************************/
-
+/*-----------------------------------------------------------------------------------*/
+/**
+* get a reference to tmp_dir 
+*/
 char *get_sci_tmp_dir(void)
 {
   /* just in case */
   C2F(settmpdir)();
   return tmp_dir;
 }
-
-
-#if _MSC_VER
-/* Remove directory and subdirectories */
-/* A.C INRIA 2005 */
-int DeleteDirectory(char *refcstrRootDirectory)
-{
-       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;
-}
-#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_dir2);
 }
+/*-----------------------------------------------------------------------------------*/
index fa231b2..144aa11 100644 (file)
@@ -6,7 +6,9 @@
 IO_C_SOURCES = src/c/getpidc.c \
 src/c/setenvc.c \
 src/c/systemc.c \
-src/c/getenvc.c
+src/c/getenvc.c \
+src/c/createdir.c
+
 
 IO_FORTRAN_SOURCES = src/fortran/v2unit.f \
 src/fortran/newsave.f \
diff --git a/scilab/modules/io/includes/createdir.h b/scilab/modules/io/includes/createdir.h
new file mode 100644 (file)
index 0000000..6198691
--- /dev/null
@@ -0,0 +1,15 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef __CREATEDIR_H__\r
+#define __CREATEDIR_H__\r
+/*-----------------------------------------------------------------------------------*/ \r
+#include "machine.h"\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL CreateDir(const char *path);\r
+BOOL ExistDir(char *path);\r
+BOOL RemoveDir(char *path);\r
+/*-----------------------------------------------------------------------------------*/ \r
+#endif /* __CREATEDIR_H__ */\r
+/*-----------------------------------------------------------------------------------*/ \r
diff --git a/scilab/modules/io/src/c/createdir.c b/scilab/modules/io/src/c/createdir.c
new file mode 100644 (file)
index 0000000..79e677c
--- /dev/null
@@ -0,0 +1,154 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef _MSC_VER\r
+       #include <sys/stat.h>\r
+       #include <sys/types.h>\r
+       #include <fcntl.h>\r
+       #include <unistd.h> \r
+#else\r
+       #include <Windows.h>\r
+#endif\r
+#include <stdio.h>\r
+#include "createdir.h"\r
+#include "MALLOC.h"\r
+/*-----------------------------------------------------------------------------------*/ \r
+#define DIRMODE 0777\r
+/*-----------------------------------------------------------------------------------*/ \r
+#if _MSC_VER\r
+int DeleteDirectory(char *refcstrRootDirectory);\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL ExistDir(char * path)\r
+{\r
+       BOOL bOK=FALSE;\r
+       #ifndef _MSC_VER\r
+               BOOL bOK=FALSE;\r
+               struct stat buf;\r
+               if (path == NULL) return FALSE;\r
+\r
+               if ( (stat (path, &buf)) && (S_ISDIR (buf.st_mode)) )\r
+               {\r
+                       bOK=TRUE;\r
+               }\r
+       #else\r
+               WIN32_FIND_DATA ffd;\r
+               HANDLE sh = FindFirstFile(path, &ffd);\r
+\r
+               if(INVALID_HANDLE_VALUE == sh) return FALSE;\r
+               if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) bOK=TRUE;\r
+       #endif\r
+\r
+       return bOK;\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL CreateDir(const char* path) \r
+{\r
+       BOOL bOK=FALSE;\r
+       #ifndef _MSC_VER\r
+               struct stat statBuf;\r
+\r
+               if  (!ExistPath(path)) \r
+               {\r
+                       if (mkdir(path, DIRMODE) == 0) \r
+                       {\r
+                               bOK=TRUE;\r
+                       }\r
+               }\r
+       #else\r
+               if (CreateDirectory(path,NULL)) bOK=TRUE;\r
+       #endif\r
+       return bOK;\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL RemoveDir(char *path)\r
+{\r
+       BOOL bOK=FALSE;\r
+       if (ExistDir(path))\r
+       {\r
+               #ifndef _MSC_VER\r
+                       \r
+                       if (rmdir(path) == 0 ) \r
+                       {\r
+                               bOK=TRUE;\r
+                       }\r
+                       else\r
+                       {\r
+                               char buf[1024];\r
+                               sprintf(buf,"rm -rf %s >/dev/null  2>/dev/null",path);\r
+                               system(buf);\r
+                       }\r
+               #else\r
+                       DeleteDirectory(path); \r
+                       if (!ExistDir(path)) bOK=TRUE;\r
+               #endif\r
+       }\r
+       return bOK;\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
+#if _MSC_VER\r
+int DeleteDirectory(char *refcstrRootDirectory)\r
+{\r
+       BOOL bDeleteSubdirectories=TRUE;\r
+       BOOL bSubdirectory = FALSE;\r
+       HANDLE hFile;\r
+       char *strFilePath=NULL;\r
+       char *strPattern=NULL;\r
+       WIN32_FIND_DATA FileInformation;\r
+       DWORD dwError;\r
+\r
+       strPattern = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5));\r
+       sprintf(strPattern,"%s\\*.*",refcstrRootDirectory);\r
+\r
+       hFile = FindFirstFile(strPattern, &FileInformation);\r
+       if (strPattern) { FREE(strPattern);strPattern=NULL;}\r
+\r
+       if(hFile != INVALID_HANDLE_VALUE)\r
+       {\r
+               do\r
+               {\r
+                       if(FileInformation.cFileName[0] != '.')\r
+                       {\r
+                               if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}\r
+                               strFilePath = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5+strlen(FileInformation.cFileName)));\r
+                               sprintf(strFilePath,"%s\\%s",refcstrRootDirectory,FileInformation.cFileName);\r
+\r
+                               if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)\r
+                               {\r
+                                       if(bDeleteSubdirectories)\r
+                                       {\r
+                                               int iRC = DeleteDirectory(strFilePath);\r
+                                               if(iRC) return iRC;\r
+                                       }\r
+                                       else bSubdirectory = TRUE;\r
+                               }\r
+                               else\r
+                               {\r
+                                       if(SetFileAttributes(strFilePath,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();\r
+                                       if(DeleteFile(strFilePath) == FALSE) return GetLastError();\r
+                               }\r
+                       }\r
+               } while(FindNextFile(hFile, &FileInformation) == TRUE);\r
+\r
+               FindClose(hFile);\r
+\r
+               dwError = GetLastError();\r
+               if(dwError != ERROR_NO_MORE_FILES) return dwError;\r
+               else\r
+               {\r
+                       if(!bSubdirectory)\r
+                       {\r
+                               if(SetFileAttributes(refcstrRootDirectory,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();\r
+                               if(RemoveDirectory(refcstrRootDirectory) == FALSE)      return GetLastError();\r
+                       }\r
+               }\r
+       }\r
+\r
+       if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}\r
+       if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}\r
+\r
+       return 0;\r
+}\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
index 3e5814d..9cbcde6 100644 (file)
@@ -1,28 +1,42 @@
-/* Copyright INRIA/ENPC */
-/* returns setenv defined variable when getenv fails */
-#include <stdio.h>
-#include <string.h>
-#include "core_math.h"
+/*-----------------------------------------------------------------------------------*/
+/* INRIA 2006 */
+/* Scilab */
+/*-----------------------------------------------------------------------------------*/ 
+#if _MSC_VER
+       #include <Windows.h> /* GetEnvironmentVariable */
+#else
+       #include <stdlib.h> /* getenv */
+#endif
+#include <string.h> /* strlen */
 #include "machine.h"
 #include "sciprint.h"
 /*-----------------------------------------------------------------------------------*/
 void C2F(getenvc)(int *ierr,char *var,char *buf,int *buflen,int *iflag)
 {
-  char *getenv(const char *),*local;
-  *ierr=0;
-  if ( (local=getenv(var)) == 0)
-    {
-      if ( *iflag == 1 )
-       sciprint("You must define the environment variable %s\r\n",var);
-      *ierr=1;
-      return;
-    }
-  else 
-    {
-      strncpy(buf,local,*buflen);
-      *buflen = strlen(buf);
-    }
-  return;
+       #if _MSC_VER
+       if (GetEnvironmentVariable(var,buf,(DWORD)buflen)==0)
+       {
+               if ( *iflag == 1 ) sciprint("You must define the environment variable %s\r\n",var);
+               *ierr=1;
+       }
+       else
+       {
+               *buflen = strlen(buf);
+               *ierr=0;
+       }
+       #else
+       char *local;
+       if ( (local=getenv(var)) == 0)
+       {
+               if ( *iflag == 1 ) sciprint("You must define the environment variable %s\r\n",var);
+               *ierr=1;
+       }
+       else 
+       {
+               strncpy(buf,local,*buflen);
+               *buflen = strlen(buf);
+       }
+       #endif
 }
 /*-----------------------------------------------------------------------------------*/
 
index d902115..469a665 100644 (file)
                        Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
                        >
                        <File
+                               RelativePath=".\createdir.c"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\getenvc.c"
                                >
                        </File>
                        Filter="h;hpp;hxx;hm;inl"
                        >
                        <File
+                               RelativePath="..\..\includes\createdir.h"
+                               >
+                       </File>
+                       <File
                                RelativePath="..\..\includes\gw_io.h"
                                >
                        </File>