createdir.c

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------------*/
00002 /* INRIA 2007 */
00003 /* Allan CORNET */
00004 /*-----------------------------------------------------------------------------------*/ 
00005 #ifndef _MSC_VER
00006         #include <sys/stat.h>
00007         #include <sys/types.h>
00008         #include <fcntl.h>
00009         #include <unistd.h>
00010         #include <dirent.h>
00011 #else
00012         #include <Windows.h>
00013 #endif
00014 #include <stdlib.h>
00015 #include <stdio.h>
00016 #include "createdir.h"
00017 #include "MALLOC.h"
00018 /*-----------------------------------------------------------------------------------*/ 
00019 #define DIRMODE 0777
00020 /*-----------------------------------------------------------------------------------*/ 
00021 int DeleteDirectory(char *refcstrRootDirectory);
00022 #ifndef _MSC_VER
00023 static void removefile(char *filename);
00024 #endif
00025 /*-----------------------------------------------------------------------------------*/ 
00026 BOOL ExistDir(char * path)
00027 {
00028         BOOL bOK=FALSE;
00029         #ifndef _MSC_VER
00030                 struct stat buf;
00031                 if (path == NULL) return FALSE;
00032                 if (stat(path, &buf) == 0 && S_ISDIR(buf.st_mode)) bOK=TRUE;
00033         #else
00034                 WIN32_FIND_DATA ffd;
00035                 HANDLE sh = FindFirstFile(path, &ffd);
00036 
00037                 if(INVALID_HANDLE_VALUE == sh) return FALSE;
00038                 if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) bOK=TRUE;
00039         #endif
00040 
00041         return bOK;
00042 }
00043 /*-----------------------------------------------------------------------------------*/ 
00044 BOOL CreateDir(const char* path) 
00045 {
00046         BOOL bOK=FALSE;
00047         #ifndef _MSC_VER
00048                 if  (!ExistDir(path)) 
00049                 {
00050                         if (mkdir(path, DIRMODE) == 0) 
00051                         {
00052                                 bOK=TRUE;
00053                         }
00054                 }
00055         #else
00056                 if (CreateDirectory(path,NULL)) bOK=TRUE;
00057         #endif
00058         return bOK;
00059 }
00060 /*-----------------------------------------------------------------------------------*/ 
00061 BOOL RemoveDir(char *path)
00062 {
00063         BOOL bOK=FALSE;
00064         if (ExistDir(path))
00065         {
00066                 DeleteDirectory(path);
00067                 if (!ExistDir(path)) bOK=TRUE; 
00068         }
00069         return bOK;
00070 }
00071 /*-----------------------------------------------------------------------------------*/ 
00072 #ifdef _MSC_VER
00073 int DeleteDirectory(char *refcstrRootDirectory)
00074 {
00075         BOOL bDeleteSubdirectories=TRUE;
00076         BOOL bSubdirectory = FALSE;
00077         HANDLE hFile;
00078         char *strFilePath=NULL;
00079         char *strPattern=NULL;
00080         WIN32_FIND_DATA FileInformation;
00081         DWORD dwError;
00082 
00083         strPattern = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5));
00084         sprintf(strPattern,"%s\\*.*",refcstrRootDirectory);
00085 
00086         hFile = FindFirstFile(strPattern, &FileInformation);
00087         if (strPattern) { FREE(strPattern);strPattern=NULL;}
00088 
00089         if(hFile != INVALID_HANDLE_VALUE)
00090         {
00091                 do
00092                 {
00093                         if(FileInformation.cFileName[0] != '.')
00094                         {
00095                                 if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
00096                                 strFilePath = MALLOC(sizeof(char)*(strlen(refcstrRootDirectory)+5+strlen(FileInformation.cFileName)));
00097                                 sprintf(strFilePath,"%s\\%s",refcstrRootDirectory,FileInformation.cFileName);
00098 
00099                                 if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
00100                                 {
00101                                         if(bDeleteSubdirectories)
00102                                         {
00103                                                 int iRC = DeleteDirectory(strFilePath);
00104                                                 if(iRC) return iRC;
00105                                         }
00106                                         else bSubdirectory = TRUE;
00107                                 }
00108                                 else
00109                                 {
00110                                         if(SetFileAttributes(strFilePath,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
00111                                         if(DeleteFile(strFilePath) == FALSE) return GetLastError();
00112                                 }
00113                         }
00114                 } while(FindNextFile(hFile, &FileInformation) == TRUE);
00115 
00116                 FindClose(hFile);
00117 
00118                 dwError = GetLastError();
00119                 if(dwError != ERROR_NO_MORE_FILES) return dwError;
00120                 else
00121                 {
00122                         if(!bSubdirectory)
00123                         {
00124                                 if(SetFileAttributes(refcstrRootDirectory,FILE_ATTRIBUTE_NORMAL) == FALSE) return GetLastError();
00125                                 if(RemoveDirectory(refcstrRootDirectory) == FALSE)      return GetLastError();
00126                         }
00127                 }
00128         }
00129 
00130         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
00131         if (strFilePath) {FREE(strFilePath);strFilePath=NULL;}
00132 
00133         return 0;
00134 }
00135 #endif
00136 /*-----------------------------------------------------------------------------------*/
00137 #ifndef _MSC_VER
00138 static void removefile(char *filename)
00139 {
00140         FILE *f = fopen(filename, "r") ;
00141         if (! f) return ;
00142         fclose(f) ;
00143         chmod(filename, S_IWRITE) ;
00144         remove(filename) ;
00145 }
00146 #endif 
00147 /*-----------------------------------------------------------------------------------*/
00148 #ifndef _MSC_VER
00149 int DeleteDirectory(char *refcstrRootDirectory)
00150 {
00151         DIR *dir;
00152         struct dirent *ent;
00153 
00154         dir = opendir(refcstrRootDirectory) ;
00155 
00156         if (!dir)
00157         {
00158                 removefile(refcstrRootDirectory) ;
00159         } 
00160         else
00161         {
00162                 while((ent = readdir(dir)) != NULL)
00163                 {
00164                         char *filename = NULL;
00165                         if (ent->d_name[0] == '.') continue ;
00166 
00167                         filename = MALLOC(strlen(refcstrRootDirectory) + 1 + strlen(ent->d_name) + 1 + 1) ;
00168                         sprintf(filename,"%s/%s",refcstrRootDirectory,ent->d_name);
00169                         removefile(filename);
00170                         if (filename) {FREE(filename);filename=NULL;}
00171                 }
00172                 rmdir(refcstrRootDirectory);
00173         }
00174         return 0;
00175 }
00176 #endif
00177 /*-----------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:58 2007 for Scilab [trunk] by  doxygen 1.5.1