scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 57732f583e5263c9ea7f1b2c29a591f671bcdb3d
parent 56291d393d17f863d2ace0ce96616772d04bf8ca
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 24 Aug 2019 22:13:34 +0100

[libmach] Remove formember()

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

Diffstat:
Minclude/scc/scc/mach.h | 3---
Msrc/cmd/nm.c | 33++++++++++++++++++---------------
Msrc/cmd/ranlib.c | 61+++++++++++++++++++++++++++++++++++++------------------------
Msrc/cmd/size.c | 32++++++++++++++++++--------------
Msrc/libmach/Makefile | 1-
Dsrc/libmach/formember.c | 26--------------------------
6 files changed, 73 insertions(+), 83 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -73,9 +73,6 @@ struct obj { 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); diff --git 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: error("object file corrupted"); } -static int -newmember(FILE *fp, char *name, void *data) +static void +newlib(FILE *fp) { int t; - - multi = 1; - membname = name; - if ((t = objtype(fp, NULL)) != -1) - newobject(fp, t); - - return 1; + long r; + char memb[SARNAM+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 @@ nm(char *fname) 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)); diff --git a/src/cmd/ranlib.c b/src/cmd/ranlib.c @@ -124,16 +124,30 @@ newsymbol(Objsym *sym) 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 @@ error: 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 diff --git 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 @@ err: (*obj->ops->del)(obj); } -static int -newmember(FILE *fp, char *name, void *data) +static void +newlib(FILE *fp) { int t; - - membname = name; - if ((t = objtype(fp, NULL)) != -1) - newobject(fp, t); - - return 1; + long r; + char memb[SARNAM+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 @@ size(char *fname) 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)); diff --git a/src/libmach/Makefile b/src/libmach/Makefile @@ -16,7 +16,6 @@ OBJS = mach.o \ archive.o \ armember.o \ objlookup.o \ - formember.o \ objtype.o \ objwrite.o \ pack.o \ diff --git a/src/libmach/formember.c b/src/libmach/formember.c @@ -1,26 +0,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); - } -}