|
|
1.1 root 1: /* simulate new system call */
2: /* output is up to cnt bytes (up to some max)
3: * each directory entry gets a string which is
4: * the ascii representation of the inode number, a tab, and the
5: * zero-terminated file name. these zero-terminated strings are packed together
6: * This implementation freely returns short reads. End of directory
7: * is a 0 length read, -1 and ENOSPC indicate that cnt couldn't hold
8: * even the next entry
9: */
10: #include "sys/types.h"
11: #include "sys/dir.h"
12: #include "errno.h"
13:
14: char mb[4096], ncnv[23];
15: pdirread(fd, buf, cnt)
16: char *buf;
17: { int n, m, j, loc;
18: char *p, *lp;
19: struct direct *d;
20:
21: loc = lseek(fd, 0, 1);
22: n = read(fd, mb, sizeof(mb));
23: if(n <= 0)
24: return(n);
25: lp = p = buf;
26: d = (struct direct *) mb;
27: loop:
28: if(d->d_ino == 0)
29: goto incr;
30: for(m = d->d_ino, j = 22; m; j--) {
31: ncnv[j] = m % 10 + '0';
32: m /= 10;
33: }
34: for(++j; j < 23 && p < buf + cnt; )
35: *p++ = ncnv[j++];
36: if(j != 23 || p >= buf + cnt)
37: goto early;
38: *p++ = '\t';
39: for(j = 0; d->d_name[j] && j < DIRSIZ && p < buf + cnt; j++)
40: *p++ = d->d_name[j];
41: if(p >= buf + cnt)
42: goto early;
43: *p++ = 0;
44: incr:
45: lp = p;
46: n -= sizeof(*d);
47: d++;
48: if(lp < buf + cnt && n > 0)
49: goto loop;
50: done:
51: lseek(fd, loc + (char *)d - mb, 0);
52: return(lp - buf);
53: early:
54: *lp = 0;
55: if((char *)d != mb)
56: goto done;
57: errno = ENOSPC;
58: return(-1);
59: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.