dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnet/support dir.c,1.3,1.4


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnet/support dir.c,1.3,1.4
Date: Tue, 05 Nov 2002 22:55:09 -0500

Update of /cvsroot/dotgnu-pnet/pnet/support
In directory subversions:/tmp/cvs-serv7101/support

Modified Files:
        dir.c 
Log Message:


Make the directory scanning routines more opaque and handle
platforms that don't have "d_type".


Index: dir.c
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/support/dir.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** dir.c       2 Nov 2002 17:56:31 -0000       1.3
--- dir.c       6 Nov 2002 03:55:06 -0000       1.4
***************
*** 65,68 ****
--- 65,155 ----
  }
  
+ #ifdef HAVE_DIRENT_H
+ 
+ /*
+  * Define the ILDir type.
+  */
+ struct _tagILDir
+ {
+       char *pathname;
+       DIR *dir;
+ };
+ 
+ /*
+  * Define the ILDirEnt type.
+  */
+ struct _tagILDirEnt
+ {
+       int type;
+       struct dirent *dptr;
+       struct dirent de;
+ };
+ 
+ /*
+  * Get the type of a directory entry.
+  */
+ static void GetDirEntryType(ILDir *dir, ILDirEnt *entry)
+ {
+       char *fullName;
+       struct stat st;
+       entry->type = ILFileType_Unknown;
+       fullName = (char *)ILMalloc(strlen(dir->pathname) +
+                                                               
strlen(entry->dptr->d_name) + 2);
+       if(fullName)
+       {
+               strcpy(fullName, dir->pathname);
+               strcat(fullName, "/");
+               strcat(fullName, entry->dptr->d_name);
+               if(lstat(fullName, &st) >= 0)
+               {
+               #ifdef S_ISFIFO
+                       if(S_ISFIFO(st.st_mode))
+                       {
+                               entry->type = ILFileType_FIFO;
+                       }
+               #endif
+               #ifdef S_ISCHR
+                       if(S_ISCHR(st.st_mode))
+                       {
+                               entry->type = ILFileType_CHR;
+                       }
+               #endif
+               #ifdef S_ISDIR
+                       if(S_ISDIR(st.st_mode))
+                       {
+                               entry->type = ILFileType_DIR;
+                       }
+               #endif
+               #ifdef S_ISBLK
+                       if(S_ISBLK(st.st_mode))
+                       {
+                               entry->type = ILFileType_BLK;
+                       }
+               #endif
+               #ifdef S_ISREG
+                       if(S_ISREG(st.st_mode))
+                       {
+                               entry->type = ILFileType_REG;
+                       }
+               #endif
+               #ifdef S_ISLNK
+                       if(S_ISLNK(st.st_mode))
+                       {
+                               entry->type = ILFileType_LNK;
+                       }
+               #endif
+               #ifdef S_ISSOCK
+                       if(S_ISSOCK(st.st_mode))
+                       {
+                               entry->type = ILFileType_SOCK;
+                       }
+               #endif
+               }
+               ILFree(fullName);
+       }
+ }
+ 
+ #endif /* HAVE_DIRENT_H */
+ 
  /*
   * Implementing this way because opendir seems to be somewhat 
non-standardised.
***************
*** 71,78 ****
  ILDir *ILOpenDir(char *path)
  {
! #ifdef HAVE_OPENDIR
!       return (ILDir *)opendir(path);
  #else
!       return NULL;
  #endif
  }
--- 158,183 ----
  ILDir *ILOpenDir(char *path)
  {
! #ifdef HAVE_DIRENT_H
!       ILDir *dir = (ILDir *)ILMalloc(sizeof(ILDir));
!       if(dir)
!       {
!               dir->pathname = ILDupString(path);
!               if(!(dir->pathname))
!               {
!                       ILFree(dir);
!                       return (ILDir *)0;
!               }
!               dir->dir = opendir(path);
!               if(!(dir->dir))
!               {
!                       ILFree(dir->pathname);
!                       ILFree(dir);
!                       return (ILDir *)0;
!               }
!       }
!       return dir;
  #else
!       errno = ENOENT;
!       return (ILDir *)0;
  #endif
  }
***************
*** 82,93 ****
  ILDirEnt *ILReadDir(ILDir *directory)
  {
        ILDirEnt *result = NULL;
  
- #ifndef HAVE_READDIR_R
-       ILDirEnt *allocatedResult = NULL;
- #endif
-     
        /* Threadsafe version of readdir() */
- #ifdef HAVE_READDIR_R
        /*  Fetch a directory entry  */
        if((result = (ILDirEnt *)ILMalloc(sizeof(ILDirEnt))) == NULL)
--- 187,196 ----
  ILDirEnt *ILReadDir(ILDir *directory)
  {
+ #ifdef HAVE_DIRENT_H
+     
+ #ifdef HAVE_READDIR_R
        ILDirEnt *result = NULL;
  
        /* Threadsafe version of readdir() */
        /*  Fetch a directory entry  */
        if((result = (ILDirEnt *)ILMalloc(sizeof(ILDirEnt))) == NULL)
***************
*** 96,100 ****
      }
      
!       if(readdir_r(directory, result, &result) != 0)
        {
                ILFree(result);
--- 199,203 ----
      }
      
!       if(readdir_r(directory->dir, &(result->de), &(result->dptr)) != 0)
        {
                ILFree(result);
***************
*** 102,111 ****
        }
  
        return result;
  #else
  #ifdef HAVE_READDIR
!       /*  Not Threadsafe, so maby if systems need it, we should rewrite it.  
*/
!       
!       if((result = readdir(directory)) == NULL)
        {
                return NULL;
--- 205,216 ----
        }
  
+       GetDirEntryType(directory, result);
        return result;
  #else
  #ifdef HAVE_READDIR
!       /*  Not Threadsafe, so maybe if systems need it, we should rewrite it.  
*/
!       struct dirent *result;
!       ILDirEnt *allocatedResult = NULL;
!       if((result = readdir(directory->dir)) == NULL)
        {
                return NULL;
***************
*** 115,123 ****
         *      own struct  */
        allocatedResult = (ILDirEnt *)ILMalloc(sizeof(ILDirEnt));
!       ILMemCpy(alocatedResult, result, sizeof(ILDirEnt));
        return allocatedResult;
  #else
!       return NULL; // fallback mode
  #endif
  #endif
  }
--- 220,237 ----
         *      own struct  */
        allocatedResult = (ILDirEnt *)ILMalloc(sizeof(ILDirEnt));
!       if(allocatedResult != NULL)
!       {
!               allocatedResult->type = result->d_type;
!               allocatedResult->dptr = &(allocatedResult->de);
!               ILMemCpy(&(allocatedResult->de), result, sizeof(struct dirent));
!               GetDirEntryType(directory, allocatedResult);
!       }
        return allocatedResult;
  #else
!       return NULL;
! #endif
  #endif
+ #else
+       return NULL;
  #endif
  }
***************
*** 125,130 ****
  int ILCloseDir(ILDir *directory)
  {
! #ifdef HAVE_CLOSEDIR
!       return (closedir(directory)!=EBADF);
  #else
        return 0;
--- 239,247 ----
  int ILCloseDir(ILDir *directory)
  {
! #ifdef HAVE_DIRENT_H
!       int result = (closedir(directory->dir) == 0);
!       ILFree(directory->pathname);
!       ILFree(directory);
!       return result;
  #else
        return 0;
***************
*** 132,137 ****
  }
  
  #ifdef        __cplusplus
  };
  #endif
- 
--- 249,271 ----
  }
  
+ const char *ILDirEntName(ILDirEnt *entry)
+ {
+ #ifdef HAVE_DIRENT_H
+       return entry->dptr->d_name;
+ #else
+       return (const char *)0;
+ #endif
+ }
+ 
+ int ILDirEntType(ILDirEnt *entry)
+ {
+ #ifdef HAVE_DIRENT_H
+       return entry->type;
+ #else
+       return ILFileType_Unknown;
+ #endif
+ }
+ 
  #ifdef        __cplusplus
  };
  #endif





reply via email to

[Prev in Thread] Current Thread [Next in Thread]