+
+/*
+ * emulate a read of n bytes on an old style directory
+ */
+oread(fd, buf, count)
+int fd, count;
+char *buf;
+{
+ struct fdflags *fp;
+ struct direct *dp;
+ DIR *dirp;
+ struct odirect *odp;
+ register int nleft = count;
+ int dir_off;
+ int i;
+
+ fp = &fdflags[fd];
+ dirp = fp->fd_dirp;
+ odp = &fp->fd_od;
+ if (dirp == NULL)
+ return(-1);
+ dir_off = fp->fd_offset % ODSIZE;
+ if (dir_off) {
+ i = ODSIZE - dir_off;
+ if (nleft < i)
+ i = nleft;
+ bcopy((caddr_t)odp + dir_off, buf, i);
+ fp->fd_offset += i;
+ if (i == nleft)
+ return(i);
+ buf += i;
+ nleft -= i;
+ }
+ while (nleft >= ODSIZE) {
+ if ((dp = readdir(dirp)) == NULL)
+ return(count - nleft);
+ odp->od_ino = dp->d_ino;
+ strncpy(odp->od_name, dp->d_name, 14);
+ bcopy((caddr_t)odp, buf, ODSIZE);
+ fp->fd_offset += ODSIZE;
+ buf += ODSIZE;
+ nleft -= ODSIZE;
+ }
+ if (nleft > 0) {
+ if ((dp = readdir(dirp)) == NULL)
+ return(count - nleft);
+ odp->od_ino = dp->d_ino;
+ strncpy(odp->od_name, dp->d_name, 14);
+ bcopy((caddr_t)odp, buf, nleft);
+ fp->fd_offset += nleft;
+ /* nleft = 0; */
+ }
+ return(count);
+}
+
+/*
+ * emulate the lseek system call
+ */
+off_t
+olseek(fd, n, whence)
+int fd, whence;
+off_t n;
+{
+ struct fdflags *fp;
+ char buf[512];
+ off_t newpos;
+ int i, j;
+
+ if ((unsigned)fd >= NFILES)
+ return(-1);
+ fp = &fdflags[fd];
+ /*
+ * the system can handle everything
+ * except directory files
+ */
+ if (fp->fd_dirp == NULL)
+ return(lseek(fd, n, whence));
+ switch (whence) {
+ case 0:
+ newpos = n;
+ break;
+ case 1:
+ newpos = fp->fd_offset + n;
+ break;
+ case 2: /* not yet implemented */
+ default:
+ return(-1);
+ }
+ if (newpos < 0)
+ return(-1);
+ if (newpos < fp->fd_offset) {
+ rewinddir(fdflags[fd].fd_dirp);
+ fp->fd_offset = 0;
+ }
+ i = newpos - fp->fd_offset;
+ while (i > 0) {
+ j = i < 512 ? i : 512;
+ if (oread(fd, buf, j) != j)
+ break;
+ i -= j;
+ }
+ return(fp->fd_offset);
+}