home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume6 / glib / part01 / amigadir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-14  |  2.5 KB  |  107 lines

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