shithub: scc

Download patch

ref: b9d7c95aa5bf9e06fc7d6a58cd5d0c600f128bf3
parent: 1156d6a42e3a86badbda9c894bee1be97aed7923
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Aug 24 18:13:34 EDT 2019

[libmach] Remove formember()

This function is not needed and using armember() gives a
procedural interface of the functional interface.

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -73,9 +73,6 @@
 
 extern int archive(FILE *fp);
 extern long armember(FILE *fp, char *member);
-extern int formember(FILE *fp,
-                     int (*fn)(FILE *, char *, void *),
-                     void *data);
 
 extern int objtype(FILE *fp, char **name);
 extern Obj *objnew(int type);
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <scc/ar.h>
 #include <scc/arg.h>
 #include <scc/mach.h>
 
@@ -169,17 +170,21 @@
 		error("object file corrupted");
 }
 
-static int
-newmember(FILE *fp, char *name, void *data)
+static void
+newlib(FILE *fp)
 {
 	int t;
+	long r;
+	char memb[SARNAM+1];
 
-	multi = 1;
-	membname = name;
-	if ((t = objtype(fp, NULL)) != -1)
-		newobject(fp, t);
-
-	return 1;
+	while ((r = armember(fp, memb)) > 0) {
+		membname = memb;
+		if ((t = objtype(fp, NULL)) != -1)
+			newobject(fp, t);
+		membname = NULL;
+	}
+	if (r < 0)
+		error("library corrupted");
 }
 
 static void
@@ -196,14 +201,12 @@
 		return;
 	}
 
-	if ((t = objtype(fp, NULL)) != -1) {
+	if ((t = objtype(fp, NULL)) != -1)
 		newobject(fp, t);
-	} else if (archive(fp)) {
-		if (formember(fp, newmember, NULL) < 0)
-			error("library corrupted");
-	} else {
+	else if (archive(fp))
+		newlib(fp);
+	else
 		error("bad format");
-	}
 
 	if (ferror(fp))
 		error(strerror(errno));
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -124,16 +124,30 @@
 	return 1;
 }
 
+static void
+freehash(void)
+{
+	Symdef **npp, *next, *np;
+
+	for (npp = htab; npp < &htab[NR_SYMDEF]; npp++)
+		*npp = NULL;
+
+	for (np = head; np; np = next) {
+		next = np->next;
+		free(np->name);
+		free(np);
+	}
+
+	head = NULL;
+}
+
 static int
-newmember(FILE *fp, char *nam, void *data)
+newmember(FILE *fp, char *nam)
 {
 	int t, ret = 0;
 	Obj *obj;
 	Objsym *sym;
 
-	if (artype == -1 && (!strcmp(nam, "/") || !strcmp(nam, "__.SYMDEF")))
-		return 1;
-
 	membname = nam;
 	offset = ftell(fp);
 
@@ -173,37 +187,36 @@
 	return ret;
 }
 
-static void
-freehash(void)
-{
-	Symdef **npp, *next, *np;
-
-	for (npp = htab; npp < &htab[NR_SYMDEF]; npp++)
-		*npp = NULL;
-
-	for (np = head; np; np = next) {
-		next = np->next;
-		free(np->name);
-		free(np);
-	}
-
-	head = NULL;
-}
-
 static int
 readsyms(FILE *fp)
 {
+	long r, off;
+	char memb[SARNAM+1];
+
 	if (!archive(fp)) {
 		error("file format not recognized");
 		return 0;
 	}
 
-	if (formember(fp, newmember, NULL) < 0) {
-		error("library file corrupted");
-		return 0;
+	if ((off = armember(fp, memb)) < 0)
+		goto corrupted;
+
+	if (strcmp(memb, "/") != 0 && strcmp(memb, "__.SYMDEF") != 0) {
+		if (fseek(fp, -off, SEEK_CUR) == EOF) {
+			error(errstr());
+			return 0;
+		}
 	}
 
+	while ((r = armember(fp, memb)) > 0)
+		newmember(fp, memb);
+	if (r < 0)
+		goto corrupted;
 	return 1;
+
+corrupted:
+	error("corrupted ar file");
+	return 0;
 }
 
 static int
--- a/src/cmd/size.c
+++ b/src/cmd/size.c
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include <scc/ar.h>
 #include <scc/arg.h>
 #include <scc/mach.h>
 
@@ -96,16 +97,21 @@
 	(*obj->ops->del)(obj);
 }
 
-static int
-newmember(FILE *fp, char *name, void *data)
+static void
+newlib(FILE *fp)
 {
 	int t;
+	long r;
+	char memb[SARNAM+1];
 
-	membname = name;
-	if ((t = objtype(fp, NULL)) != -1)
-		newobject(fp, t);
-
-	return 1;
+	while ((r = armember(fp, memb)) > 0) {
+		membname = memb;
+		if ((t = objtype(fp, NULL)) != -1)
+			newobject(fp, t);
+		membname = NULL;
+	}
+	if (r < 0)
+		error("library corrupted");
 }
 
 static void
@@ -120,14 +126,12 @@
 		return;
 	}
 
-	if ((t = objtype(fp, NULL)) != -1) {
+	if ((t = objtype(fp, NULL)) != -1)
 		newobject(fp, t);
-	} else if (archive(fp)) {
-		if (formember(fp, newmember, NULL) < 0)
-			error("library corrupted");
-	} else {
+	else if (archive(fp))
+		newlib(fp);
+	else
 		error("bad format");
-	}
 
 	if (ferror(fp))
 		error(strerror(errno));
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -16,7 +16,6 @@
        archive.o \
        armember.o \
        objlookup.o \
-       formember.o \
        objtype.o \
        objwrite.o \
        pack.o \
--- a/src/libmach/formember.c
+++ /dev/null
@@ -1,26 +1,0 @@
-#include <stdio.h>
-
-#include <scc/ar.h>
-#include <scc/mach.h>
-
-int
-formember(FILE *fp, int (*fn)(FILE *, char *, void *), void *data)
-{
-	int r;
-	long off;
-	fpos_t pos;
-	char name[SARNAM+1];
-
-	for (;;) {
-		fgetpos(fp, &pos);
-
-		if ((off = armember(fp, name)) <= 0)
-			return off;
-		r = (*fn)(fp, name, data);
-		if (r <= 0)
-			return r;
-
-		fsetpos(fp, &pos);
-		fseek(fp, off, SEEK_CUR);
-	}
-}