Fixes to allow versionless packages on cd
[minix3.git] / lib / libcompat_minix / nlist.c
blob4901032f1fdbc955904d26e1eab20965c930d0db
1 /*
2 * "nlist.c", Peter Valkenburg, january 1989.
3 */
5 #include <lib.h>
6 #include <string.h>
7 #include <a.out.h>
8 #include <sys/types.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <stdio.h>
13 #define fail(fp) (fclose(fp), -1) /* ret. exp. when nlist fails */
15 _PROTOTYPE( int nlist, (char *file, struct nlist nl[]));
18 * Nlist fills fields n_sclass and n_value of array nl with values found in
19 * non-stripped executable file. Entries that are not found have their
20 * n_value/n_sclass fields set to 0. Nl ends with a 0 or nul string n_name.
21 * The return value is -1 on failure, else the number of entries not found.
23 int nlist(file, nl)
24 char *file;
25 struct nlist nl[];
27 int nents, nsrch, nfound, i;
28 struct nlist nlent;
29 FILE *fp;
30 struct exec hd;
32 /* open executable with namelist */
33 if ((fp = fopen(file, "r")) == NULL)
34 return -1;
36 /* get header and seek to start of namelist */
37 if (fread((char *) &hd, sizeof(struct exec), 1, fp) != 1 ||
38 BADMAG(hd) || fseek(fp, A_SYMPOS(hd), SEEK_SET) != 0)
39 return fail(fp);
41 /* determine number of entries searched for & reset fields */
42 nsrch = 0;
43 while (nl[nsrch].n_name != NULL && *(nl[nsrch].n_name) != '\0') {
44 nl[nsrch].n_sclass = 0;
45 nl[nsrch].n_value = 0;
46 nl[nsrch].n_type = 0; /* for compatability */
47 nsrch++;
50 /* loop through namelist & fill in user array */
51 nfound = 0;
52 for (nents = (hd.a_syms & 0xFFFF) / sizeof(struct nlist);
53 nents > 0; nents--) {
54 if (nsrch == nfound)
55 break; /* no need to look further */
56 if (fread((char *) &nlent, sizeof(struct nlist), 1, fp) != 1)
57 return fail(fp);
58 for (i = 0; i < nsrch; i++)
59 if (nl[i].n_sclass == 0 &&
60 strncmp(nl[i].n_name, nlent.n_name,
61 sizeof(nlent.n_name)) == 0) {
62 nl[i] = nlent;
63 nfound++;
64 break;
68 (void) fclose(fp);
70 return nsrch - nfound;