+ }
+ (void)free(name);
+ return(rval);
+}
+
+/*
+ * show_objfile()
+ * show symbols from the object file pointed to by fp. The current
+ * file pointer for fp is expected to be at the beginning of an a.out
+ * header.
+ */
+show_objfile(objname, fp)
+ char *objname;
+ FILE *fp;
+{
+ register struct nlist *names, *np;
+ register int i, nnames, nrawnames;
+ struct exec head;
+ long stabsize;
+ char *stab;
+
+ /* read a.out header */
+ if (fread((char *)&head, sizeof(head), (size_t)1, fp) != 1) {
+ (void)fprintf(stderr,
+ "nm: %s: cannot read header.\n", objname);
+ return(1);
+ }
+
+ /*
+ * skip back to the header - the N_-macros return values relative
+ * to the beginning of the a.out header
+ */
+ if (fseek(fp, (long)-sizeof(head), SEEK_CUR)) {
+ (void)fprintf(stderr,
+ "nm: %s: %s\n", objname, strerror(errno));
+ return(1);
+ }
+
+ /* stop if this is no valid object file */
+ if (N_BADMAG(head)) {
+ (void)fprintf(stderr,
+ "nm: %s: bad format.\n", objname);
+ return(1);
+ }
+
+ /* stop if the object file contains no symbol table */
+ if (!head.a_syms) {
+ (void)fprintf(stderr,
+ "nm: %s: no name list.\n", objname);
+ return(1);
+ }
+
+ if (fseek(fp, (long)N_SYMOFF(head), SEEK_CUR)) {
+ (void)fprintf(stderr,
+ "nm: %s: %s\n", objname, strerror(errno));
+ return(1);
+ }
+
+ /* get memory for the symbol table */
+ names = emalloc((size_t)head.a_syms);
+ nrawnames = head.a_syms / sizeof(*names);
+ if (fread((char *)names, (size_t)head.a_syms, (size_t)1, fp) != 1) {
+ (void)fprintf(stderr,
+ "nm: %s: cannot read symbol table.\n", objname);
+ (void)free((char *)names);
+ return(1);
+ }
+
+ /*
+ * Following the symbol table comes the string table. The first
+ * 4-byte-integer gives the total size of the string table
+ * _including_ the size specification itself.
+ */
+ if (fread((char *)&stabsize, sizeof(stabsize), (size_t)1, fp) != 1) {
+ (void)fprintf(stderr,
+ "nm: %s: cannot read stab size.\n", objname);
+ (void)free((char *)names);
+ return(1);
+ }
+ stab = emalloc((size_t)stabsize);
+
+ /*
+ * read the string table offset by 4 - all indices into the string
+ * table include the size specification.
+ */
+ stabsize -= 4; /* we already have the size */
+ if (fread(stab + 4, (size_t)stabsize, (size_t)1, fp) != 1) {
+ (void)fprintf(stderr,
+ "nm: %s: stab truncated..\n", objname);
+ (void)free((char *)names);
+ (void)free(stab);
+ return(1);
+ }