|
|
1.1 ! root 1: #include "defs.h" ! 2: #if !defined(BSD4_2) && !defined(BSD4_1C) && !defined(HP9K5) ! 3: #ifdef M_XENIX ! 4: #include <sys/types.h> ! 5: #endif /* M_XENIX */ ! 6: #include <sys/param.h> ! 7: #include "ndir.h" ! 8: ! 9: #ifdef SCCSID ! 10: static char *SccsId = "@(#)ndir.c 1.12 10/15/87"; ! 11: #endif /* SCCSID */ ! 12: ! 13: /* ! 14: * support for Berkeley directory reading routine on a V7 file system ! 15: */ ! 16: ! 17: extern char *malloc(); ! 18: ! 19: /* ! 20: * open a directory. ! 21: */ ! 22: DIR * ! 23: opendir(name) ! 24: char *name; ! 25: { ! 26: register DIR *dirp; ! 27: register int fd; ! 28: ! 29: if ((fd = open(name, 0)) == -1) ! 30: return NULL; ! 31: if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) { ! 32: close (fd); ! 33: return NULL; ! 34: } ! 35: dirp->dd_fd = fd; ! 36: dirp->dd_loc = 0; ! 37: return dirp; ! 38: } ! 39: ! 40: /* ! 41: * read an old style directory entry and present it as a new one ! 42: */ ! 43: #ifdef pyr ! 44: /* Pyramid in the AT&T universe */ ! 45: #define ODIRSIZ 248 ! 46: struct olddirect { ! 47: long od_ino; ! 48: short od_fill1, od_fill2; ! 49: char od_name[ODIRSIZ]; ! 50: }; ! 51: #else /* V7 file system */ ! 52: #define ODIRSIZ 14 ! 53: ! 54: struct olddirect { ! 55: short od_ino; ! 56: char od_name[ODIRSIZ]; ! 57: }; ! 58: #endif /* !pyr */ ! 59: ! 60: /* ! 61: * get next entry in a directory. ! 62: */ ! 63: struct direct * ! 64: readdir(dirp) ! 65: register DIR *dirp; ! 66: { ! 67: register struct olddirect *dp; ! 68: static struct direct dir; ! 69: ! 70: for (;;) { ! 71: if (dirp->dd_loc == 0) { ! 72: dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, ! 73: DIRBLKSIZ); ! 74: if (dirp->dd_size <= 0) ! 75: return NULL; ! 76: } ! 77: if (dirp->dd_loc >= dirp->dd_size) { ! 78: dirp->dd_loc = 0; ! 79: continue; ! 80: } ! 81: dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc); ! 82: dirp->dd_loc += sizeof(struct olddirect); ! 83: if (dp->od_ino == 0) ! 84: continue; ! 85: dir.d_ino = dp->od_ino; ! 86: strncpy(dir.d_name, dp->od_name, ODIRSIZ); ! 87: dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */ ! 88: dir.d_namlen = strlen(dir.d_name); ! 89: dir.d_reclen = DIRSIZ(&dir); ! 90: return (&dir); ! 91: } ! 92: } ! 93: ! 94: /* ! 95: * close a directory. ! 96: */ ! 97: void ! 98: closedir(dirp) ! 99: register DIR *dirp; ! 100: { ! 101: close(dirp->dd_fd); ! 102: dirp->dd_fd = -1; ! 103: dirp->dd_loc = 0; ! 104: free((char *)dirp); ! 105: } ! 106: ! 107: /* ! 108: * seek to an entry in a directory. ! 109: * Only values returned by "telldir" should be passed to seekdir. ! 110: */ ! 111: void ! 112: seekdir(dirp, loc) ! 113: register DIR *dirp; ! 114: long loc; ! 115: { ! 116: long curloc, base, offset; ! 117: struct direct *dp; ! 118: long lseek(), telldir(); ! 119: ! 120: curloc = telldir(dirp); ! 121: if (loc == curloc) ! 122: return; ! 123: base = loc & ~(DIRBLKSIZ - 1); ! 124: offset = loc & (DIRBLKSIZ - 1); ! 125: (void) lseek(dirp->dd_fd, base, 0); ! 126: dirp->dd_loc = 0; ! 127: while (dirp->dd_loc < offset) { ! 128: dp = readdir(dirp); ! 129: if (dp == NULL) ! 130: return; ! 131: } ! 132: } ! 133: ! 134: /* ! 135: * return a pointer into a directory ! 136: */ ! 137: long ! 138: telldir(dirp) ! 139: DIR *dirp; ! 140: { ! 141: return lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc; ! 142: } ! 143: #endif /* !BSD4_2 && !BSD4_1C && !HP9K5 */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.