home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / mthreads / part01 / ndir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-20  |  2.3 KB  |  116 lines

  1. /* $Id: ndir.c,v 3.0 1991/09/09 20:23:31 davison Trn $
  2.  */
  3. /* This software is Copyright 1991 by Stan Barber. 
  4.  *
  5.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  6.  * use this software as long as: there is no monetary profit gained
  7.  * specifically from the use or reproduction of this software, it is not
  8.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  9.  * included prominently in any copy made. 
  10.  *
  11.  * The authors make no claims as to the fitness or correctness of this software
  12.  * for any use whatsoever, and it is provided as is. Any use of this software
  13.  * is at the user's own risk. 
  14.  */
  15.  
  16. #include "EXTERN.h"
  17. #include "common.h"
  18. #include "INTERN.h"
  19. #include "ndir.h"
  20.  
  21. #ifdef EMULATE_NDIR
  22. /*
  23.  * support for Berkeley directory reading routine on a V7 file system
  24.  */
  25.  
  26. /*
  27.  * open a directory.
  28.  */
  29. DIR *
  30. opendir(name)
  31. char *name;
  32. {
  33.     register DIR *dirp;
  34.     register int fd;
  35.     char *malloc();
  36.  
  37.     if ((fd = open(name, 0)) == -1)
  38.         return NULL;
  39.     if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
  40.         close (fd);
  41.         return NULL;
  42.     }
  43.     dirp->dd_fd = fd;
  44.     dirp->dd_loc = 0;
  45.     return dirp;
  46. }
  47.  
  48. /*
  49.  * read an old style directory entry and present it as a new one
  50.  */
  51. #ifndef pyr
  52. #define    ODIRSIZ    14
  53.  
  54. struct    olddirect {
  55.     ino_t    od_ino;
  56.     char    od_name[ODIRSIZ];
  57. };
  58. #else    /* an Pyramid in the ATT universe */
  59. #define    ODIRSIZ    248
  60.  
  61. struct    olddirect {
  62.     long    od_ino;
  63.     short    od_fill1, od_fill2;
  64.     char    od_name[ODIRSIZ];
  65. };
  66. #endif
  67.  
  68. /*
  69.  * get next entry in a directory.
  70.  */
  71. struct direct *
  72. readdir(dirp)
  73. register DIR *dirp;
  74. {
  75.     register struct olddirect *dp;
  76.     static struct direct dir;
  77.  
  78.     for (;;) {
  79.         if (dirp->dd_loc == 0) {
  80.             dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
  81.                 DIRBLKSIZ);
  82.             if (dirp->dd_size <= 0)
  83.                 return NULL;
  84.         }
  85.         if (dirp->dd_loc >= dirp->dd_size) {
  86.             dirp->dd_loc = 0;
  87.             continue;
  88.         }
  89.         dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
  90.         dirp->dd_loc += sizeof(struct olddirect);
  91.         if (dp->od_ino == 0)
  92.             continue;
  93.         dir.d_ino = dp->od_ino;
  94.         strncpy(dir.d_name, dp->od_name, ODIRSIZ);
  95.         dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
  96.         dir.d_namlen = strlen(dir.d_name);
  97.         dir.d_reclen = DIRSIZ(&dir);
  98.         return (&dir);
  99.     }
  100. }
  101.  
  102. /*
  103.  * close a directory.
  104.  */
  105. void
  106. closedir(dirp)
  107. register DIR *dirp;
  108. {
  109.     close(dirp->dd_fd);
  110.     dirp->dd_fd = -1;
  111.     dirp->dd_loc = 0;
  112.     free(dirp);
  113. }
  114.  
  115. #endif /* EMULATE_NDIR */
  116.