|
|
1.1 root 1: /*
2: * read a directory, with the directory library routines
3: *
4: * fill the buffer with directory entries of the form
5: * nnnn tab filename NUL
6: * nnnn is the i-number, in decimal ascii
7: *
8: * return the number of bytes used in the buffer, or 0 (EOF) or -1 (error)
9: * leave *offp set to something we'll want to use next time.
10: */
11:
12: #include <rf.h>
13: #include "zarf.h"
14: #include <sys/types.h>
15: #include <dirent.h>
16:
17: #define NULL 0
18:
19: /*
20: * our private context
21: */
22: #define MAXDIRS 128
23: typedef struct {
24: Rfile *f;
25: DIR *dfile;
26: } Dirfile;
27: static Dirfile dirfiles[MAXDIRS];
28: static Dirfile *ftod();
29:
30: dodirread(f, buf, size, off, offp)
31: Rfile *f;
32: char *buf;
33: register int size;
34: long off;
35: long *offp;
36: {
37: extern int errno;
38: static char tbuf[MAXNAMLEN+1+20]; /* room for NUL and some digits */
39: register struct dirent *dp;
40: register Dirfile *dirf;
41: register int n;
42: register char *p;
43:
44: dirf = ftod(f);
45: if (dirf == NULL) {
46: fserrno = RFEINVAL;
47: return (-1);
48: }
49: if (dirf->f == NULL) {
50: if ((dirf->dfile = opendir(fsp(f)->name)) == NULL) {
51: fserrno = errno; /* hope it's correct */
52: return (-1);
53: }
54: dirf->f = f;
55: }
56: seekdir(dirf->dfile, off);
57: p = buf;
58: for (;;) {
59: if ((dp = readdir(dirf->dfile)) == NULL)
60: break;
61: sprintf(tbuf, "%d\t%s", dp->d_ino, dp->d_name);
62: n = strlen(tbuf) + 1;
63: if (size < n)
64: break;
65: strcpy(p, tbuf);
66: p += n;
67: size -= n;
68: off = telldir(dirf->dfile);
69: }
70: if (dp != NULL && p == buf) { /* no room for anything */
71: fserrno = RFENOSPC;
72: return (-1);
73: }
74: *offp = off;
75: return (p - buf);
76: }
77:
78: static Dirfile *
79: ftod(f)
80: register Rfile *f;
81: {
82: register Dirfile *dirf;
83: register Dirfile *empty;
84:
85: empty = NULL;
86: for (dirf = dirfiles; dirf < &dirfiles[MAXDIRS]; dirf++) {
87: if (dirf->f == f)
88: return (dirf);
89: if (empty == NULL && dirf->f == NULL)
90: empty = dirf;
91: }
92: return (empty);
93: }
94:
95: dodircleanup(f)
96: Rfile *f;
97: {
98: register Dirfile *dirf;
99:
100: if ((dirf = ftod(f)) == NULL || dirf->f == NULL)
101: return;
102: closedir(dirf->dfile);
103: dirf->f = NULL;
104: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.