|
|
1.1 root 1: #include "fs.h"
2: #include "pwd.h"
3:
4: char *
5: stralloc(s)
6: char *s;
7: { char *p;
8: p = (char *) malloc(strlen(s) + 1);
9: if(p)
10: strcpy(p, s);
11: return(p);
12: }
13:
14: char *
15: prname(n) /* find a name for inode n */
16: { int i = 0;
17: xbuf[0] = 0;
18: switch(imap[n].type) {
19: case Unalloc:
20: return("(Unalloc)");
21: case Weird:
22: return("(Weird)");
23: case Dir:
24: if(imap[n].type == Dir)
25: i = imap[n].dotdot;
26: /* and then a random stab, probably find some bogus .. */
27: default:
28: if(!i)
29: i = imap[n].parent;
30: if(i == 0) {
31: strcat(xbuf, "??");
32: return(stralloc(xbuf));
33: }
34: prdirnm(i);
35: nami(i, n);
36: return(stralloc(xbuf));
37: }
38: }
39:
40: char *
41: prino(n)
42: { char *s;
43: struct passwd *x;
44: struct dinode *dp;
45: if(n < 1 || n >= ninode)
46: sprintf(xbuf, "ino %d", n);
47: else {
48: dp = (struct dinode *) (buf + 2*bsize + (n-1)*sizeof(*dp));
49: x = getpwuid(dp->di_uid);
50: sprintf(xbuf, "ino %d is %s[%s:%s]len %d lnks %d", n, prname(n),
51: x?x->pw_name: "(who?)", itype(imap[n].type), dp->di_size,
52: dp->di_nlink);
53: }
54: return(stralloc(xbuf));
55: }
56:
57: prdirnm(n)
58: { int i;
59: if(n == ROOTINO) {
60: strcat(xbuf, "/");
61: return;
62: }
63: i = imap[n].dotdot;
64: if(i == n || i == 0) {
65: strcat(xbuf, "?/");
66: return;
67: }
68: prdirnm(i);
69: nami(i, n);
70: }
71: nami(dir, ino)
72: { int blksrch(), i;
73: i = dirsrch(dir, ino, blksrch);
74: if(i < 0) {
75: strcat(xbuf, "?");
76: return(-1);
77: }
78: }
79:
80: dirsrch(dir, arg, f)
81: int (*f)();
82: { int i, addr[NADDR], j, *p;
83: struct dinode *dp;
84: dp = (struct dinode *) (buf + 2*bsize + (dir-1)*sizeof(*dp));
85: l3tol(addr, dp->di_addr, NADDR);
86: for(i = 0; i < NADDR-3; i++) {
87: if(addr[i] == 0)
88: continue;
89: j = f(dir, arg, addr[i]);
90: if(j >= 0)
91: return(j);
92: }
93: if(!addr[10]) { /* NADDR-3? */
94: return(-1);
95: }
96: if(bread(addr[10], buf+bsize, 1)) {
97: pmesg("dir %d couldn't read ind block %d\n", dir, addr[10]);
98: return(-1);
99: }
100: p = (int *) (buf+bsize);
101: for(i = 0; i < bsize/sizeof(int); i++) {
102: if(p[i] == 0)
103: continue;
104: j = f(dir, arg, p[i]);
105: if(j >= 0)
106: return(j);
107: }
108: return(-1);
109: }
110:
111: blksrch(dir, ino, n)
112: { struct direct *dp;
113: int i;
114: if(bread(n, buf, 1)) {
115: pmesg("dir %d couldn't read dir block %d\n", dir, n);
116: return(-1);
117: }
118: dp = (struct direct *)buf;
119: for(i = 0; i < bsize/sizeof(*dp); i++, dp++)
120: if(dp->d_ino == ino) {
121: strncat(xbuf, dp->d_name, DIRSIZ);
122: if(imap[ino].type == Dir)
123: strcat(xbuf, "/");
124: return(1);
125: }
126: return(-1);
127: }
128:
129: nams(dir, s)
130: char *s;
131: { int nmsrch();
132: return(dirsrch(dir, s, nmsrch));
133: }
134:
135: nmsrch(dir, s, n)
136: char *s;
137: { struct direct *dp;
138: int i;
139: if(bread(n, buf, 1)) {
140: pmesg("dir %d couldn't read dir block %d\n", dir, n);
141: return(-1);
142: }
143: dp = (struct direct *)buf;
144: for(i = 0; i < bsize/sizeof(*dp); i++, dp++) {
145: if(!dp->d_ino || strncmp(s, dp->d_name, DIRSIZ))
146: continue;
147: return(dp->d_ino);
148: }
149: return(-1);
150: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.