scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit c7f20efa5745699d2ca0b8812a909dd700c2cb1c
parent 9f9ec428a8a5857cf0d1a5d00ed5d3533d535b11
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 24 Nov 2017 08:51:38 +0000

[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.

Diffstat:
Ainc/ar.h | 3+++
Mnm/main.c | 69++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 63 insertions(+), 9 deletions(-)

diff --git a/inc/ar.h b/inc/ar.h @@ -0,0 +1,3 @@ + +#define ARMAGIC "!<arch>\n" +#define ARMAGIC_SIZ 8 diff --git a/nm/main.c b/nm/main.c @@ -8,25 +8,76 @@ static char sccsid[] = "@(#) ./nm/main.c"; #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) +{ + 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) { - 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 @@ main(int argc, char *argv[]) } ARGEND if (argc == 0) { - nm("a.out"); + doit("a.out"); } else { while (argc-- > 0) - nm(*++argv); + doit(*++argv); } return 0;