shithub: scc

Download patch

ref: ba2b29ffa4013243f10fad9e508238f0d70d1168
parent: 5df46147ef1c3abe8adb2afb464861736065e7a2
author: Roberto E. Vargas Caballero <[email protected]>
date: Thu Nov 23 18:15:22 EST 2017

[nm] Add nm() function

This function is going to be the core of the program. At this moment
it does almost nothing, but at least it opens the file.

--- a/nm/main.c
+++ b/nm/main.c
@@ -1,17 +1,38 @@
 static char sccsid[] = "@(#) ./nm/main.c";
 
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "../inc/arg.h"
 #include "../inc/scc.h"
+#include "../inc/myro.h"
 
 char *argv0;
+int radix = 16;
 
 void
+nm(char *fname)
+{
+	FILE *fp;
+	struct myrohdr hdr;
+
+	if ((fp = fopen(fname, "rb")) == NULL)
+		goto file_error;
+	if (readhdr(fp, &hdr) == EOF)
+		goto file_error;
+	if (strncmp(hdr.magic, MYROMAGIC, MYROMAGIC_SIZ))
+		die("nm: %s: File format not recognized", fname);
+
+file_error:
+	die("nm: %s: %s", fname, strerror(errno));
+}
+
+void
 usage(void)
 {
-	fputs("nm [-APv][-efox][ -g| -u][-t format] file...\n", stderr);
+	fputs("nm [-APv][ -g| -u][-t format] file...\n", stderr);
 	exit(1);
 }
 
@@ -20,18 +41,21 @@
 {
 	ARGBEGIN {
 	case 'A':
-	case 'e':
-	case 'f':
 	case 'g':
-	case 'o':
 	case 'u':
 	case 'v':
-	case 'x':
 	/* case 't': */
 		;
 	default:
 		usage();
 	} ARGEND
+
+	if (argc == 0) {
+		nm("a.out");
+	} else {
+		while (argc-- > 0)
+			nm(*++argv);
+	}
 
 	return 0;
 }