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