home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / apps / posix / source / SH / STD / POSIX / DIRENT.C < prev    next >
C/C++ Source or Header  |  1999-11-17  |  1KB  |  64 lines

  1. /*
  2.  * simple implementation of directory(3) routines for V7 and Minix.
  3.  * completly untested. not designed to be efficient.
  4.  * missing telldir and seekdir.
  5.  */
  6.  
  7. #include <sys/types.h>
  8. #include <dirent.h>
  9.  
  10. char    *malloc();
  11.  
  12. #define    DIRSIZ    14
  13. struct    direct_v7
  14. {
  15.     unsigned short    d_ino;
  16.     char    d_name[DIRSIZ];
  17. };
  18.  
  19. DIR *opendir(filename)
  20.     char *filename;
  21. {
  22.     DIR *dirp;
  23.  
  24.     dirp = (DIR *) malloc(sizeof(DIR));
  25.     if (dirp == NULL)
  26.         return NULL;
  27.     dirp->fd = open(filename, 0);
  28.     if (dirp->fd < 0) {
  29.         free((char *) dirp);
  30.         return NULL;
  31.     }
  32.     return dirp;
  33. }
  34.  
  35. struct dirent *readdir(dirp)
  36.     register DIR *dirp;
  37. {
  38.     static    struct direct_v7 ent;
  39.  
  40.     while (read(dirp->fd, (char *)&ent, (int)sizeof(ent)) == sizeof(ent))
  41.         if (ent.d_ino != 0)
  42.             goto found;
  43.     return (struct dirent *) NULL;
  44.  found:
  45.     dirp->ent.d_ino = ent.d_ino;
  46.     strncpy(dirp->ent.d_name, ent.d_name, DIRSIZ);
  47.     return &dirp->ent;
  48. }
  49.  
  50. void rewinddir(dirp)
  51.     DIR *dirp;
  52. {
  53.     lseek(dirp->fd, 0L, 0);
  54. }
  55.  
  56. closedir(dirp)
  57.     DIR *dirp;
  58. {
  59.     close(dirp->fd);
  60.     dirp->fd = -1;
  61.     free((char *) dirp);
  62.     return 0;
  63. }
  64.