|
|
1.1 root 1: #include <sys/types.h>
2: #include <a.out.h>
3: #include <stdio.h>
4:
5: /* this is a special version of nlist that uses smaller buffer space */
6:
7: /*
8: * nlist - retrieve attributes from name list (string table version)
9: * tangled in search of speed and buffering
10: */
11:
12: #define NBSIZE 256
13:
14: nlist(name, list)
15: char *name;
16: struct nlist *list;
17: {
18: register struct nlist *p, *q;
19: register n, m;
20: int max, nreq;
21: int f, g;
22: off_t ss; /* start of strings */
23: off_t sc; /* current string offset */
24: register int soff; /* current place in strings buffer */
25: int smax; /* number of bytes in strungs buffer */
26: struct exec buf;
27: struct nlist space[BUFSIZ/sizeof(struct nlist)];
28: char names[NBSIZE+2];
29:
30: names[NBSIZE] = 'X'; /* border protection */
31: names[NBSIZE+1] = 0;
32: max = 0;
33: for (q = list, nreq = 0; q->n_un.n_name && q->n_un.n_name[0]; q++, nreq++) {
34: q->n_type = 0;
35: q->n_value = 0;
36: q->n_desc = 0;
37: q->n_other = 0;
38: if ((n = strlen(q->n_un.n_name)) > max)
39: max = n;
40: }
41: if ((f = open(name, 0)) < 0)
42: return (-1);
43: if (read(f, (char *)&buf, sizeof(buf)) != sizeof(buf)
44: || N_BADMAG(buf)) {
45: close(f);
46: return (-1);
47: }
48: if ((g = open(name, 0)) < 0) {
49: close(f);
50: return (-1);
51: }
52: lseek(f, (off_t)N_SYMOFF(buf), 0);
53: ss = N_SYMOFF(buf) + buf.a_syms;
54: sc = 0;
55: smax = 0;
56: for (n = buf.a_syms; n > 0 && nreq > 0;) {
57: m = read(f, (char *)space, sizeof(space));
58: if (m <= 0)
59: goto out;
60: n -= m / sizeof(struct nlist);
61: for (q = space; nreq > 0 && m > 0; q++, m-= sizeof(space[0])) {
62: if (q->n_un.n_strx == 0 || q->n_type & N_STAB)
63: continue;
64: if (sc <= q->n_un.n_strx
65: && q->n_un.n_strx + max <= sc + smax)
66: soff = q->n_un.n_strx - sc;
67: else {
68: lseek(g, ss+q->n_un.n_strx, 0);
69: smax = read(g, names, NBSIZE);
70: if (smax <= 0)
71: goto out;
72: sc = q->n_un.n_strx;
73: soff = 0;
74: }
75: for (p = list; p->n_un.n_name && p->n_un.n_name[0]; p++) {
76: if (strcmp(p->n_un.n_name, &names[soff]) == 0) {
77: p->n_value = q->n_value;
78: p->n_type = q->n_type;
79: p->n_desc = q->n_desc;
80: p->n_other = q->n_other;
81: --nreq;
82: break;
83: }
84: }
85: }
86: }
87: out:
88: close(f);
89: close(g);
90: return (0);
91: }
92:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.