sl@0: /* sl@0: * opendir.c -- sl@0: * sl@0: * This file provides dirent-style directory-reading procedures sl@0: * for V7 Unix systems that don't have such procedures. The sl@0: * origin of this code is unclear, but it seems to have come sl@0: * originally from Larry Wall. sl@0: * sl@0: * sl@0: * RCS: @(#) $Id: opendir.c,v 1.2 1998/09/14 18:39:44 stanton Exp $ sl@0: */ sl@0: sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: sl@0: #undef DIRSIZ sl@0: #define DIRSIZ(dp) \ sl@0: ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3)) sl@0: sl@0: /* sl@0: * open a directory. sl@0: */ sl@0: DIR * sl@0: opendir(name) sl@0: char *name; sl@0: { sl@0: register DIR *dirp; sl@0: register int fd; sl@0: char *myname; sl@0: sl@0: myname = ((*name == '\0') ? "." : name); sl@0: if ((fd = open(myname, 0, 0)) == -1) sl@0: return NULL; sl@0: if ((dirp = (DIR *)ckalloc(sizeof(DIR))) == NULL) { sl@0: close (fd); sl@0: return NULL; sl@0: } sl@0: dirp->dd_fd = fd; sl@0: dirp->dd_loc = 0; sl@0: return dirp; sl@0: } sl@0: sl@0: /* sl@0: * read an old style directory entry and present it as a new one sl@0: */ sl@0: #ifndef pyr sl@0: #define ODIRSIZ 14 sl@0: sl@0: struct olddirect { sl@0: ino_t od_ino; sl@0: char od_name[ODIRSIZ]; sl@0: }; sl@0: #else /* a Pyramid in the ATT universe */ sl@0: #define ODIRSIZ 248 sl@0: sl@0: struct olddirect { sl@0: long od_ino; sl@0: short od_fill1, od_fill2; sl@0: char od_name[ODIRSIZ]; sl@0: }; sl@0: #endif sl@0: sl@0: /* sl@0: * get next entry in a directory. sl@0: */ sl@0: struct dirent * sl@0: readdir(dirp) sl@0: register DIR *dirp; sl@0: { sl@0: register struct olddirect *dp; sl@0: static struct dirent dir; sl@0: sl@0: for (;;) { sl@0: if (dirp->dd_loc == 0) { sl@0: dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, sl@0: DIRBLKSIZ); sl@0: if (dirp->dd_size <= 0) sl@0: return NULL; sl@0: } sl@0: if (dirp->dd_loc >= dirp->dd_size) { sl@0: dirp->dd_loc = 0; sl@0: continue; sl@0: } sl@0: dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc); sl@0: dirp->dd_loc += sizeof(struct olddirect); sl@0: if (dp->od_ino == 0) sl@0: continue; sl@0: dir.d_ino = dp->od_ino; sl@0: strncpy(dir.d_name, dp->od_name, ODIRSIZ); sl@0: dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */ sl@0: dir.d_namlen = strlen(dir.d_name); sl@0: dir.d_reclen = DIRSIZ(&dir); sl@0: return (&dir); sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * close a directory. sl@0: */ sl@0: void sl@0: closedir(dirp) sl@0: register DIR *dirp; sl@0: { sl@0: close(dirp->dd_fd); sl@0: dirp->dd_fd = -1; sl@0: dirp->dd_loc = 0; sl@0: ckfree((char *) dirp); sl@0: }