2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
5 /* $Id: anm.c,v 1.10 1994/06/24 10:13:42 ceriel Exp $ */
7 ** print symbol tables for
10 ** anm [-gopruns] [name ...]
34 long s_base[S_MAX]; /* for specially encoded bases */
42 if (--argc>0 && argv[1][0]=='-' && argv[1][1]!=0) {
44 while (*++*argv) switch (**argv) {
45 case 'n': /* sort numerically */
49 case 's': /* sort in section order */
53 case 'g': /* globl symbols only */
57 case 'u': /* undefined symbols only */
61 case 'r': /* sort in reverse order */
65 case 'p': /* don't sort -- symbol table order */
69 case 'o': /* prepend a name to each line */
74 fprintf(stderr, "anm: invalid argument -%c\n", *argv[0]);
89 if ((fd = open(filename, 0)) < 0) {
90 fprintf(stderr, "anm: cannot open %s\n", filename);
99 extern int rd_unsigned2();
101 extern char *strncpy();
108 struct ar_hdr archive_header;
109 static char buf[sizeof(archive_header.ar_name)+1];
111 if (narg > 1) printf("\n%s:\n", filename);
113 magic = rd_unsigned2(fd);
121 while (rd_arhdr(fd, &archive_header)) {
122 nextpos = lseek(fd, 0L, 1) + archive_header.ar_size;
123 if (nextpos & 1) nextpos++;
124 strncpy(buf,archive_header.ar_name,sizeof(archive_header.ar_name));
126 if ( strcmp(filename, SYMDEF)) {
127 printf("\n%s:\n", filename);
130 lseek(fd, nextpos, 0);
134 fprintf(stderr, "anm: %s -- bad format\n", filename);
142 struct outname *nbufp = NULL;
158 if (BADMAGIC(hbuf)) {
164 fprintf(stderr, "anm: %s -- no name list\n", filename);
168 if (hbuf.oh_nchar == 0) {
169 fprintf(stderr, "anm: %s -- no names\n", filename);
172 if ((readcount = hbuf.oh_nchar) != hbuf.oh_nchar) {
173 fprintf(stderr, "anm: string area too big in %s\n", filename);
177 /* store special section bases ??? */
178 if (hbuf.oh_flags & HF_8086) {
179 rd_sect(&sbuf, hbuf.oh_nsect);
183 for (i=0; i<hbuf.oh_nsect; i++) {
185 (sbuf.os_base>>12) & 03777760;
189 if ((cbufp = (char *)malloc(readcount)) == NULL) {
190 fprintf(stderr, "anm: out of memory on %s\n", filename);
193 rd_string(cbufp, hbuf.oh_nchar);
199 fi_to_co = (long) (cbufp - OFF_CHAR(hbuf));
207 if (globl_flg && (nbuf.on_type&S_EXT)==0)
212 ((nbuf.on_type&S_TYP)!=S_UND || (nbuf.on_type&S_ETC)!=0))
215 if (nbuf.on_foff == 0) nbuf.on_mptr = 0;
216 else nbuf.on_mptr = (char *) (nbuf.on_foff + fi_to_co);
218 /* adjust value for specially encoded bases */
219 if (hbuf.oh_flags & HF_8086) {
220 if (((nbuf.on_type&S_ETC) == 0) ||
221 ((nbuf.on_type&S_ETC) == S_SCT)) {
222 j = nbuf.on_type&S_TYP;
223 if ((j>=S_MIN) && (j<=S_MAX))
224 nbuf.on_valu += s_base[j];
229 nbufp = (struct outname *)malloc(sizeof(struct outname));
231 nbufp = (struct outname *)realloc(nbufp, (i+1)*sizeof(struct outname));
233 fprintf(stderr, "anm: out of memory on %s\n", filename);
239 if (nbufp && nosort_flg==0)
240 qsort(nbufp, i, sizeof(struct outname), compare);
242 for (n=0; n<i; n++) {
247 printf("%s:", filename);
249 switch(nbufp[n].on_type&S_ETC) {
251 sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
264 if (nbufp[n].on_type&S_EXT)
270 if (nbufp[n].on_type&S_EXT)
275 switch(nbufp[n].on_type&S_TYP) {
283 sprintf(cs1, "%2d", (nbufp[n].on_type&S_TYP) - S_MIN);
291 printf("%8lx %s %s %s\n",nbufp[n].on_valu,cs1,cs2,nbufp[n].on_mptr ? nbufp[n].on_mptr : "(NULL)");
301 struct outname *p1, *p2;
306 if ((p1->on_type&S_TYP) > (p2->on_type&S_TYP))
308 if ((p1->on_type&S_TYP) < (p2->on_type&S_TYP))
309 return(-revsort_flg);
313 if (p1->on_valu > p2->on_valu)
315 if (p1->on_valu < p2->on_valu)
316 return(-revsort_flg);
320 if (! p2->on_mptr) return 0;
323 if (! p2->on_mptr) return revsort_flg;
325 i = strcmp(p1->on_mptr, p2->on_mptr);
330 return(-revsort_flg);
337 fprintf(stderr,"read error on %s\n", filename);