home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / Diff / ndir.c < prev    next >
C/C++ Source or Header  |  1989-11-20  |  2KB  |  100 lines

  1. /*
  2.  * ndir - routines to simulate the 4BSD new directory code for AmigaDOS.
  3.  */
  4. #include "dir.h"
  5.  
  6. DIR *
  7. opendir(dirname) char *dirname; {
  8.     register DIR    *my_dir, *AllocMem(int, int) ;
  9.     struct FileLock    *Lock(char *, int), *CurrentDir(struct FileLock *) ;
  10.  
  11.     if ((my_dir = AllocMem(sizeof(DIR), 0)) == NULL) return NULL ;
  12.  
  13.  
  14.     if (((my_dir -> d_lock = Lock(dirname, ACCESS_READ)) == NULL)
  15.     /* If we can't examine it */
  16.     ||  !Examine(my_dir -> d_lock, &(my_dir -> d_info))
  17.     /* Or it's not a directory */
  18.     ||  (my_dir -> d_info . fib_DirEntryType < 0)) {
  19.         FreeMem(my_dir, sizeof(DIR)) ;
  20.         return NULL ;
  21.         }
  22.     return my_dir ;
  23.     }
  24.  
  25. struct direct *
  26. readdir(my_dir) DIR *my_dir; {
  27.     static struct direct    result ;
  28.  
  29.     if (!ExNext(my_dir -> d_lock, &(my_dir -> d_info))) return NULL ;
  30.  
  31.     result . d_reclen = result . d_ino = 1 ;    /* Not NULL! */
  32.     (void) strcpy(result . d_name, my_dir -> d_info . fib_FileName) ;
  33.     result . d_namlen = strlen(result . d_name) ;
  34.     return &result ;
  35.     }
  36.  
  37. void
  38. closedir(my_dir) DIR *my_dir; {
  39.  
  40.     UnLock(my_dir -> d_lock) ;
  41.     FreeMem(my_dir, sizeof(DIR)) ;
  42.     }
  43. /*
  44.  * telldir and seekdir don't work quite right. The problem is that you have
  45.  * to save more than a long's worth of stuff to indicate position, and it's
  46.  * socially unacceptable to alloc stuff that you don't free later under
  47.  * AmigaDOS. So we fake it - you get one level of seek, and dat's all.
  48.  * As of now, these things are untested.
  49.  */
  50. #define DIR_SEEK_RETURN        ((long) 1)    /* Not 0! */
  51. long
  52. telldir(my_dir) DIR *my_dir; {
  53.  
  54.     my_dir -> d_seek = my_dir -> d_info ;
  55.     return (long) DIR_SEEK_RETURN ;
  56.     }
  57.  
  58. void
  59. seekdir(my_dir, where) DIR *my_dir; long where; {
  60.  
  61.     if (where == DIR_SEEK_RETURN)
  62.         my_dir -> d_info = my_dir -> d_seek ;
  63.     else    /* Makes the next readdir fail */
  64.         setmem((char *) my_dir, sizeof(DIR), 0) ;
  65.     }
  66.  
  67. void
  68. rewinddir(my_dir) DIR *my_dir; {
  69.  
  70.     if (!Examine(my_dir -> d_lock, &(my_dir -> d_info)))
  71.         setmem((char *) my_dir, sizeof(DIR), 0) ;
  72.     }
  73. #ifdef    TEST
  74. /*
  75.  * Simple code to list the files in the argument directory,
  76.  *    lifted straight from the man page.
  77.  */
  78. #include <stdio.h>
  79. void
  80. main(argc, argv) int argc; char **argv; {
  81.     register DIR        *dirp ;
  82.     register struct direct    *dp ;
  83.     register char        *name ;
  84.  
  85.     if (argc < 2) name = "" ;
  86.     else name = argv[1] ;
  87.  
  88.     if ((dirp = opendir(name)) == NULL) {
  89.         fprintf(stderr, "Bogus! Can't opendir %s\n", name) ;
  90.         exit(1) ;
  91.         }
  92.  
  93.     for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
  94.         printf("%s ", dp -> d_name) ;
  95.     closedir(dirp);
  96.     putchar('\n') ;
  97.     }
  98. #endif    TEST
  99.  
  100.