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

  1. #ifdef atarist
  2. /* POSIX compatible directory access routines for TOS */
  3. /* written by Eric R. Smith and placed in the public domain */
  4.  
  5. #include <stddef.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <types.h>
  10. #include <stat.h>
  11. #include <errno.h>
  12. #include <dirent.h>
  13. #include <osbind.h>
  14.  
  15. extern int fullpath;
  16.  
  17. DIR *opendir(_dirname)
  18.      const char *_dirname;
  19. {
  20.   char dirname[FILENAME_MAX];
  21.   char *t;
  22.   DIR  *dd;
  23.   struct dirent *d, *x;
  24.   long r;
  25.   short i = 0;
  26.   struct _dta mydta, *olddta;
  27.  
  28.   if (!(dd = malloc((size_t)sizeof(DIR)))) {
  29.     errno = ENOMEM;
  30.     return NULL;
  31.   }
  32.   olddta = (struct _dta *)Fgetdta();
  33. #if 1
  34.   Fsetdta(&mydta);
  35. #else
  36.   if ((r = Fsetdta(&mydta)) < 0) {
  37.     errno = r;
  38.     return NULL;
  39.   }
  40. #endif
  41.   unx2dos(_dirname,dirname);
  42.   for (t = dirname; *t; t++) ;
  43.  
  44.   /* make sure it's terminated by a slash */
  45.   if (t > dirname && *(t-1) != '\\') {
  46.     *t++ = '\\'; *t = 0;
  47.   }
  48.   strcpy(t, "*.*");
  49.   if ((r = Fsfirst(dirname, (fullpath ? FA_DIR : 0))) < 0) {
  50.     if (r != ENOENT) {
  51.       errno = r;
  52.       free(dd);
  53.       dd = NULL;
  54.       goto _done;
  55.     }
  56.     d = NULL;
  57.     goto _done;
  58.   }
  59.   d = x = malloc((size_t)(DIRENTSIZ(strlen(mydta.dta_name))));
  60.   for (;;) {
  61.     if (!x) {
  62.       errno = ENOMEM;
  63.       free(dd); dd = NULL;
  64.       break;
  65.     }
  66.     dos2unx(mydta.dta_name, x->d_name);
  67.     x->d_ino = rand();      /* to make sure no two are equal */
  68.     x->d_off = i++;
  69.  
  70.     /* I don't know what d_reclen means on Unix, but for TOS we might as well
  71.        stuff the string length in here (so sys/dir.h can be more like BSD) */
  72.     x->d_reclen = strlen(x->d_name);
  73.     if (Fsnext() == 0) {
  74.       x->d_next = malloc((size_t)(DIRENTSIZ(strlen(mydta.dta_name))));
  75.       x = x->d_next;
  76.     }
  77.     else {
  78.       x->d_next = 0;
  79.       break;
  80.     }
  81.   }
  82. _done:
  83.   Fsetdta(olddta);
  84.   if (dd) {
  85.     dd->D_list = dd->D_curpos = d;
  86.   }
  87.   return dd;
  88. }
  89.  
  90. struct dirent *readdir(dirp)
  91.      DIR *dirp;
  92. {
  93.   struct dirent *x;
  94.  
  95.   if (!dirp) return NULL;
  96.  
  97.   x = dirp->D_curpos;
  98.   if (x) dirp->D_curpos = x->d_next;
  99.   return x;
  100. }
  101.  
  102. int closedir(dirp)
  103.      DIR *dirp;
  104. {
  105.   struct dirent *x, *oldx;
  106.  
  107.   if (!dirp) return -1;
  108.  
  109.   for (x = dirp->D_list; x; ) {
  110.     oldx = x;
  111.     x = x->d_next;
  112.     free(oldx);
  113.   }
  114.   free(dirp);
  115.   return 0;
  116. }
  117. #endif /* atarist */
  118.