home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / unaxcess / part2 / dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.6 KB  |  106 lines

  1. /*
  2.  *
  3.  *                N O T I C E
  4.  *
  5.  * This file is NOT a copyrighted part of the UNaXcess distribution.  These
  6.  * are directory-reading routines which are compatible with the Berkeley Unix
  7.  * (4.2BSD, 4.3BSD) strectory routines.  They come from the Usenet news
  8.  * distribution and are in the public domain.
  9.  *
  10.  * To get the best use of them:  install the file "dir.h" in /usr/include
  11.  * -- standard usage calls it "ndir.h", and make a random archive of dir.o and
  12.  * put it in /usr/lib/libndir.a .  It is then available with "-lndir".
  13.  *
  14.  * Bell System {III, V} sites, just make an archive -- it is only one file
  15.  * anyway.  Other sites will have to run ranlib on the archive to keep ld
  16.  * happy.
  17.  */
  18.  
  19. #include <sys/types.h>
  20. #include "dir.h"
  21.  
  22. #ifndef BSD
  23.  
  24. /*
  25.  * close a directory.
  26.  */
  27. closedir(dirp)
  28.         register DIR *dirp;
  29. {
  30.         close(dirp->dd_fd);
  31.         dirp->dd_fd = -1;
  32.         dirp->dd_loc = 0;
  33.         free(dirp);
  34. }
  35.  
  36.  
  37.  
  38. /*
  39.  * open a directory.
  40.  */
  41. DIR *
  42. opendir(name)
  43.         char *name;
  44. {
  45.         register DIR *dirp;
  46.         register int fd;
  47.  
  48.         if ((fd = open(name, 0)) == -1)
  49.                 return NULL;
  50.         if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
  51.                 close (fd);
  52.                 return NULL;
  53.         }
  54.         dirp->dd_fd = fd;
  55.         dirp->dd_loc = 0;
  56.         return dirp;
  57. }
  58.  
  59.  
  60.  
  61. /*
  62.  * read an old style directory entry and present it as a new one
  63.  */
  64. #define ODIRSIZ 14
  65.  
  66. struct  olddirect {
  67.         ino_t   od_ino;
  68.         char    od_name[ODIRSIZ];
  69. };
  70.  
  71. /*
  72.  * get next entry in a directory.
  73.  */
  74. struct direct *
  75. readdir(dirp)
  76.         register DIR *dirp;
  77. {
  78.         register struct olddirect *dp;
  79.         static struct direct dir;
  80.  
  81.         for (;;) {
  82.                 if (dirp->dd_loc == 0) {
  83.                         dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, 
  84.                             DIRBLKSIZ);
  85.                         if (dirp->dd_size <= 0)
  86.                                 return NULL;
  87.                 }
  88.                 if (dirp->dd_loc >= dirp->dd_size) {
  89.                         dirp->dd_loc = 0;
  90.                         continue;
  91.                 }
  92.                 dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
  93.                 dirp->dd_loc += sizeof(struct olddirect);
  94.                 if (dp->od_ino == 0)
  95.                         continue;
  96.                 dir.d_ino = dp->od_ino;
  97.                 strncpy(dir.d_name, dp->od_name, ODIRSIZ);
  98.                 dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
  99.                 dir.d_namlen = strlen(dir.d_name);
  100.                 dir.d_reclen = DIRBLKSIZ;
  101.                 return (&dir);
  102.         }
  103. }
  104.  
  105. #endif BSD
  106.