ref: 128f871fd0d4943b50c2de4b5afcde1b1210ae30
parent: 63e58ec2d143083afa060d26ac15a125ffe88609
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Nov 29 07:52:42 EST 2017
[objdump] Add doit() function This fuction allows a better separation of the code and it is a first step to centralize the error handle code.
--- a/objdump/main.c
+++ b/objdump/main.c
@@ -238,18 +238,15 @@
}
void
-dump(char *fname)
+dump(char *fname, FILE *fp)
{
- FILE *fp;
struct obj_info obj;
struct myrohdr *hdr;
obj.fname = fname;
- if ((fp = fopen(fname, "rb")) == NULL)
- goto wrong_file;
-
obj.fp = fp;
hdr = &obj.hdr;
+
if (rdmyrohdr(obj.fp, hdr) < 0)
goto wrong_file;
if (strncmp(hdr->magic, MYROMAGIC, MYROMAGIC_SIZ)) {
@@ -256,7 +253,7 @@
fprintf(stderr,
"objdump: %s: File format not recognized\n",
fname);
- goto close_file;
+ return;
}
puts(fname);
if (hdr->strsize > SIZE_MAX) {
@@ -263,7 +260,7 @@
fprintf(stderr,
"objdump: %s: overflow in header\n",
fname);
- goto close_file;
+ return;
}
strsiz = hdr->strsize;
@@ -286,19 +283,28 @@
goto wrong_file;
if (printdata(&obj) < 0)
goto wrong_file;
+ return;
- goto close_file;
-
wrong_file:
fprintf(stderr,
"objdump: %s: %s\n",
fname, strerror(errno));
-close_file:
- if (fp)
- fclose(fp);
}
void
+doit(char *fname)
+{
+ FILE *fp;
+
+ if ((fp = fopen(fname, "rb")) == NULL) {
+ fprintf(stderr, "objdump: %s: %s\n", fname, strerror(errno));
+ return;
+ }
+ dump(fname, fp);
+ fclose(fp);
+}
+
+void
usage(void)
{
fputs("usage: objdump file ...\n", stderr);
@@ -314,11 +320,11 @@
} ARGEND
if (argc == 0)
- dump("a.out");
+ doit("a.out");
else while (*argv) {
free(strings);
strings = NULL;
- dump(*argv++);
+ doit(*argv++);
}
if (fclose(stdout) == EOF)