shithub: scc

Download patch

ref: e646a0300cdd915eeaeeada2f44b4380c27fa654
parent: ba2b29ffa4013243f10fad9e508238f0d70d1168
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Nov 24 03:51:38 EST 2017

[nm] Add more skeleton for ar files

Nm has to work with binary files and with archive files.
For this reason the functions has to accept a file name
and a file member name, because in this way we can cover
both cases.

--- /dev/null
+++ b/inc/ar.h
@@ -1,0 +1,3 @@
+
+#define ARMAGIC       "!<arch>\n"
+#define ARMAGIC_SIZ   8
--- a/nm/main.c
+++ b/nm/main.c
@@ -8,25 +8,76 @@
 #include "../inc/arg.h"
 #include "../inc/scc.h"
 #include "../inc/myro.h"
+#include "../inc/ar.h"
 
 char *argv0;
 int radix = 16;
 
 void
-nm(char *fname)
+fdie(char *fname)
 {
-	FILE *fp;
+	die("nm: %s: %s", fname, strerror(errno));
+}
+
+static int
+myrofile(char *fname, FILE *fp)
+{
+	char magic[MYROMAGIC_SIZ];
+
+	rewind(fp);
+	fread(magic, sizeof(magic), 1, fp);
+	if (ferror(fp))
+		fdie(fname);
+	return strncmp(magic, MYROMAGIC, MYROMAGIC_SIZ) == 0;
+}
+
+static int
+arfile(char *fname, FILE *fp)
+{
+	char magic[ARMAGIC_SIZ];
+
+	rewind(fp);
+	fread(magic, sizeof(magic), 1, fp);
+	if (ferror(fp))
+		fdie(fname);
+	return strncmp(magic, ARMAGIC, ARMAGIC_SIZ) == 0;
+}
+
+static void
+nm(char *fname, char *member, FILE *fp)
+{
 	struct myrohdr hdr;
+	size_t n;
 
-	if ((fp = fopen(fname, "rb")) == NULL)
-		goto file_error;
+	rewind(fp);
 	if (readhdr(fp, &hdr) == EOF)
-		goto file_error;
+		fdie(fname);
 	if (strncmp(hdr.magic, MYROMAGIC, MYROMAGIC_SIZ))
+	n = hdr.symsize / MYROSYM_SIZ;
+	if (n == 0) {
+		fprintf(stderr, "nm: %s: no name list\n", member);
+		return;
+	}
+}
+
+void
+doit(char *fname)
+{
+	FILE *fp;
+	char magic[20];
+
+	if ((fp = fopen(fname, "rb")) == NULL)
+		fdie(fname);
+
+	if (myrofile(fname, fp))
+		nm(fname, fname, fp);
+	else if (arfile(fname, fp))
+		/* run over the members */;
+	else
 		die("nm: %s: File format not recognized", fname);
 
-file_error:
-	die("nm: %s: %s", fname, strerror(errno));
+	if (fclose(fp) == EOF)
+		fdie(fname);
 }
 
 void
@@ -51,10 +102,10 @@
 	} ARGEND
 
 	if (argc == 0) {
-		nm("a.out");
+		doit("a.out");
 	} else {
 		while (argc-- > 0)
-			nm(*++argv);
+			doit(*++argv);
 	}
 
 	return 0;