home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / pdksh-4.9-src.tgz / tar.out / contrib / pdksh / std / posix / dirent.C < prev    next >
C/C++ Source or Header  |  1996-09-28  |  1KB  |  65 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. /* $Id: dirent.C,v 1.3 93/05/05 21:17:40 sjg Exp $ */
  7.  
  8. #include <sys/types.h>
  9. #include <dirent.h>
  10.  
  11. char    *malloc();
  12.  
  13. #define    DIRSIZ    14
  14. struct    direct_v7
  15. {
  16.     unsigned short    d_ino;
  17.     char    d_name[DIRSIZ];
  18. };
  19.  
  20. DIR *opendir(filename)
  21.     char *filename;
  22. {
  23.     DIR *dirp;
  24.  
  25.     dirp = (DIR *) malloc(sizeof(DIR));
  26.     if (dirp == NULL)
  27.         return NULL;
  28.     dirp->fd = open(filename, 0);
  29.     if (dirp->fd < 0) {
  30.         free((char *) dirp);
  31.         return NULL;
  32.     }
  33.     return dirp;
  34. }
  35.  
  36. struct dirent *readdir(dirp)
  37.     register DIR *dirp;
  38. {
  39.     static    struct direct_v7 ent;
  40.  
  41.     while (read(dirp->fd, (char *)&ent, (int)sizeof(ent)) == sizeof(ent))
  42.         if (ent.d_ino != 0)
  43.             goto found;
  44.     return (struct dirent *) NULL;
  45.  found:
  46.     dirp->ent.d_ino = ent.d_ino;
  47.     strncpy(dirp->ent.d_name, ent.d_name, DIRSIZ);
  48.     return &dirp->ent;
  49. }
  50.  
  51. void rewinddir(dirp)
  52.     DIR *dirp;
  53. {
  54.     lseek(dirp->fd, 0L, 0);
  55. }
  56.  
  57. closedir(dirp)
  58.     DIR *dirp;
  59. {
  60.     close(dirp->fd);
  61.     dirp->fd = -1;
  62.     free((char *) dirp);
  63.     return 0;
  64. }
  65.