home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / compat / opendir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-18  |  2.0 KB  |  109 lines

  1. /* 
  2.  * opendir.c --
  3.  *
  4.  *    This file provides dirent-style directory-reading procedures
  5.  *    for V7 Unix systems that don't have such procedures.  The
  6.  *    origin of this code is unclear, but it seems to have come
  7.  *    originally from Larry Wall.
  8.  *
  9.  */
  10.  
  11. static char sccsid[] = "@(#) opendir.c 1.2 94/11/04 13:55:02";
  12.  
  13. #include "tclInt.h"
  14. #include "tclPort.h"
  15.  
  16. #undef DIRSIZ
  17. #define DIRSIZ(dp) \
  18.     ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
  19.  
  20. /*
  21.  * open a directory.
  22.  */
  23. DIR *
  24. opendir(name)
  25. char *name;
  26. {
  27.     register DIR *dirp;
  28.     register int fd;
  29.     char *myname;
  30.  
  31.     myname = ((*name == '\0') ? "." : name);
  32.     if ((fd = open(myname, 0, 0)) == -1)
  33.         return NULL;
  34.     if ((dirp = (DIR *)ckalloc(sizeof(DIR))) == NULL) {
  35.         close (fd);
  36.         return NULL;
  37.     }
  38.     dirp->dd_fd = fd;
  39.     dirp->dd_loc = 0;
  40.     return dirp;
  41. }
  42.  
  43. /*
  44.  * read an old style directory entry and present it as a new one
  45.  */
  46. #ifndef pyr
  47. #define    ODIRSIZ    14
  48.  
  49. struct    olddirect {
  50.     ino_t    od_ino;
  51.     char    od_name[ODIRSIZ];
  52. };
  53. #else    /* a Pyramid in the ATT universe */
  54. #define    ODIRSIZ    248
  55.  
  56. struct    olddirect {
  57.     long    od_ino;
  58.     short    od_fill1, od_fill2;
  59.     char    od_name[ODIRSIZ];
  60. };
  61. #endif
  62.  
  63. /*
  64.  * get next entry in a directory.
  65.  */
  66. struct dirent *
  67. readdir(dirp)
  68. register DIR *dirp;
  69. {
  70.     register struct olddirect *dp;
  71.     static struct dirent dir;
  72.  
  73.     for (;;) {
  74.         if (dirp->dd_loc == 0) {
  75.             dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
  76.                 DIRBLKSIZ);
  77.             if (dirp->dd_size <= 0)
  78.                 return NULL;
  79.         }
  80.         if (dirp->dd_loc >= dirp->dd_size) {
  81.             dirp->dd_loc = 0;
  82.             continue;
  83.         }
  84.         dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
  85.         dirp->dd_loc += sizeof(struct olddirect);
  86.         if (dp->od_ino == 0)
  87.             continue;
  88.         dir.d_ino = dp->od_ino;
  89.         strncpy(dir.d_name, dp->od_name, ODIRSIZ);
  90.         dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
  91.         dir.d_namlen = strlen(dir.d_name);
  92.         dir.d_reclen = DIRSIZ(&dir);
  93.         return (&dir);
  94.     }
  95. }
  96.  
  97. /*
  98.  * close a directory.
  99.  */
  100. void
  101. closedir(dirp)
  102. register DIR *dirp;
  103. {
  104.     close(dirp->dd_fd);
  105.     dirp->dd_fd = -1;
  106.     dirp->dd_loc = 0;
  107.     ckfree((char *) dirp);
  108. }
  109.