home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / archiver / rblharc / lhdir.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  2KB  |  88 lines

  1. /*----------------------------------------------------------------------*/
  2. /*        Directory access routine for LHarc UNIX            */
  3. /*                                    */
  4. /*        Copyright(C) MCMLXXXIX  Yooichi.Tagawa            */
  5. /*                                    */
  6. /*    Emulate opendir(),readdir(),closedir() function for LHarc    */
  7. /*                                    */
  8. /*  V0.00  Original                1988.05.31  Y.Tagawa    */
  9. /*  V0.03  Release #3 for LHarc UNIX        1988.07.02  Y.Tagawa    */
  10. /*----------------------------------------------------------------------*/
  11.  
  12.  
  13. #include <sys/types.h>
  14.  
  15. /* Where is O_RDONLY ? (^_^) */
  16. #include <sys/file.h>
  17. #ifndef O_RDONLY
  18. #include <fcntl.h>
  19. #endif
  20.  
  21. #define direct old_direct
  22. #include <sys/dir.h>
  23. #undef direct
  24.  
  25. #include "lhdir.h"
  26.  
  27.  
  28. DIR *opendir (name)
  29.      char *name;
  30. {
  31.   register DIR *dirp;
  32.   register int fd;
  33.   if ((fd = open (name, O_RDONLY)) >= 0)
  34.     {
  35.       if ((dirp = (DIR*)malloc (sizeof (DIR))) != (DIR*)0)
  36.     {
  37.       dirp->dd_fd = fd;
  38.       dirp->dd_loc = 0;
  39.       dirp->dd_size = 0;
  40.       return dirp;
  41.     }
  42.  
  43.       close (fd);
  44.     }
  45.  
  46.   return (DIR*)0;
  47. }
  48.  
  49. struct direct *readdir (dirp)
  50.      register DIR *dirp;
  51. {
  52.   static struct direct lhdir;
  53.   register struct old_direct *dp;
  54.  
  55.   do {
  56.     if (dirp->dd_loc >= dirp->dd_size)
  57.       {
  58.     dirp->dd_loc = 0;
  59.     if ((dirp->dd_size = read (dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ)) <= 0)
  60.       return (struct direct *)0;
  61.       }
  62.  
  63.     dp = (struct old_direct *)(dirp->dd_buf + dirp->dd_loc);
  64.  
  65.     if (dirp->dd_loc + sizeof (struct old_direct) > dirp->dd_size)
  66.       return (struct direct *)0;
  67.  
  68.     dirp->dd_loc += sizeof (struct old_direct);
  69.  
  70.   } while (dp->d_ino == 0) ;
  71.  
  72.   /* construct new format */
  73.   lhdir.d_ino = dp->d_ino;
  74.   strncpy (lhdir.d_name, dp->d_name, DIRSIZ);
  75.   lhdir.d_name[DIRSIZ] = '\0';
  76.   lhdir.d_namlen = strlen (lhdir.d_name);
  77.  
  78.   return &lhdir;
  79. }
  80.  
  81. closedir (dirp)
  82.      DIR *dirp;
  83. {
  84.   close (dirp->dd_fd);
  85.   free (dirp);
  86. }
  87.  
  88.