scc

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

objdump.c (1245B)


      1 #include <errno.h>
      2 #include <stdarg.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include <scc/arg.h>
      8 #include <scc/mach.h>
      9 
     10 char *argv0;
     11 static char *filename;
     12 static int status;
     13 
     14 static void
     15 error(char *fmt, ...)
     16 {
     17 	va_list va;
     18 
     19 	va_start(va, fmt);
     20 	fprintf(stderr, "objdump: %s: ", filename);
     21 	vfprintf(stderr, fmt, va);
     22 	putc('\n', stderr);
     23 	va_end(va);
     24 
     25 	status = EXIT_FAILURE;
     26 }
     27 
     28 /*
     29  * TODO: Dummy implementation used only in the assembler tests
     30  */
     31 static void
     32 dump(char *fname)
     33 {
     34 	int c, n;
     35 	FILE *fp;
     36 
     37 	filename = fname;
     38 	if ((fp = fopen(fname, "rb")) == NULL) {
     39 		error("%s", strerror(errno));
     40 		return;
     41 	}
     42 
     43 	puts("data:");
     44 	for (n = 1; (c = getc(fp)) != EOF; n++)
     45 		printf("%02X%c", c, (n%16 == 0) ? '\n' : ' ');
     46 	if (n%16 != 0)
     47 		putchar('\n');
     48 
     49 	if (ferror(fp))
     50 		error("%s", strerror(errno));
     51 
     52 	fclose(fp);
     53 }
     54 
     55 static void
     56 usage(void)
     57 {
     58 	fputs("usage: objdump file ...\n", stderr);
     59 	exit(1);
     60 }
     61 
     62 int
     63 main(int argc, char *argv[])
     64 {
     65 	ARGBEGIN {
     66 	default:
     67 		usage();
     68 	} ARGEND
     69 
     70 	if (argc == 0)
     71 		dump("a.out");
     72 	else while (*argv) {
     73 		dump(*argv++);
     74 	}
     75 
     76 	if (fclose(stdout) == EOF) {
     77 		fprintf(stderr,
     78 		        "objdump: writing output: %s\n",
     79 		        strerror(errno));
     80 		return EXIT_FAILURE;
     81 	}
     82 
     83 	return status;
     84 }