xref: /csrg-svn/lib/libc/mips/gen/nlist.c (revision 15261)
1 /* @(#)nlist.c	4.4 (Berkeley) 10/17/83 */
2 #include <sys/types.h>
3 #include <a.out.h>
4 #include <stdio.h>
5 
6 /*
7  * nlist - retreive attributes from name list (string table version)
8  */
9 nlist(name, list)
10 	char *name;
11 	struct nlist *list;
12 {
13 	register struct nlist *p, *q;
14 	register n, m, i, nreq;
15 	FILE *f;
16 	off_t sa;		/* symbol address */
17 	off_t ss;		/* start of strings */
18 	struct exec buf;
19 	struct nlist space[BUFSIZ/sizeof (struct nlist)];
20 	int maxlen;
21 
22 	maxlen = 0;
23 	for (q = list, nreq = 0; q->n_un.n_name && q->n_un.n_name[0]; q++, nreq++) {
24 		q->n_type = 0;
25 		q->n_value = 0;
26 		q->n_desc = 0;
27 		q->n_other = 0;
28 		i = strlen(q->n_un.n_name);
29 		if (i > maxlen)
30 			maxlen = i;
31 	}
32 	f = fopen(name, "r");
33 	if (f == NULL)
34 		return (NULL);
35 	fread((char *)&buf, sizeof buf, 1, f);
36 	if (N_BADMAG(buf)) {
37 		fclose(f);
38 		return (-1);
39 	}
40 	sa = N_SYMOFF(buf);
41 	ss = sa + buf.a_syms;
42 	n = buf.a_syms;
43 	while (n) {
44 		m = sizeof (space);
45 		if (n < m)
46 			m = n;
47 		fseek(f, sa, 0);
48 		i = fread((char *)space, m, 1, f);
49 		sa += m;
50 		n -= m;
51 		for (q = space; (m -= sizeof(struct nlist)) >= 0; q++) {
52 			char nambuf[BUFSIZ];
53 
54 			if (q->n_un.n_strx == 0 || q->n_type & N_STAB)
55 				continue;
56 			fseek(f, ss+q->n_un.n_strx, 0);
57 			fread(nambuf, maxlen+1, 1, f);
58 			for (p = list; p->n_un.n_name && p->n_un.n_name[0]; p++) {
59 				i = 0;
60 				while (p->n_un.n_name[i]) {
61 					if (p->n_un.n_name[i] != nambuf[i])
62 						goto cont;
63 					i++;
64 				}
65 				if (nambuf[i])
66 					goto cont;
67 				p->n_value = q->n_value;
68 				p->n_type = q->n_type;
69 				p->n_desc = q->n_desc;
70 				p->n_other = q->n_other;
71 				if (--nreq == 0)
72 					goto alldone;
73 				break;
74 		cont:		;
75 			}
76 		}
77 	}
78 alldone:
79 	fclose(f);
80 	return (0);
81 }
82