home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / ndir.c < prev    next >
C/C++ Source or Header  |  1992-09-22  |  3KB  |  123 lines

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