scc

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

strip.c (1988B)


      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 static int status;
     11 static char tmpname[FILENAME_MAX];
     12 static char *filename;
     13 char *argv0;
     14 
     15 static void
     16 error(char *fmt, ...)
     17 {
     18 	va_list va;
     19 
     20 	va_start(va, fmt);
     21 	fprintf(stderr, "strip: %s: ", filename);
     22 	vfprintf(stderr, fmt, va);
     23 	putc('\n', stderr);
     24 	va_end(va);
     25 
     26 	status = EXIT_FAILURE;
     27 }
     28 
     29 static void
     30 doit(char *fname)
     31 {
     32 	int type;;
     33 	size_t r;
     34 	FILE *src, *dst;
     35 	Map *map;
     36 	Obj *obj;
     37 
     38 	errno = 0;
     39 	filename = fname;
     40 	r = snprintf(tmpname, sizeof(tmpname), "%s.tmp", fname);
     41 	if (r >= sizeof(tmpname)) {
     42 		error("%s: name too long", fname);
     43 		return;
     44 	}
     45 	if ((src = fopen(fname, "rb")) == NULL) {
     46 		error("opening src file");
     47 		goto err0;
     48 	}
     49 	if ((type = objtype(src, NULL)) < 0) {
     50 		error("getting object type");
     51 		goto err1;
     52 	}
     53 	if ((obj = newobj(type)) == NULL) {
     54 		error("creating object");
     55 		goto err1;
     56 	}
     57 	if (readobj(obj, src) < 0) {
     58 		error("reading object");
     59 		goto err2;
     60 	}
     61 	if ((map = loadmap(obj, src)) == NULL) {
     62 		error("loading map");
     63 		goto err3;
     64 	}
     65 	if (strip(obj) < 0) {
     66 		error("stripping");
     67 		goto err3;
     68 	}
     69 	if ((dst = fopen(tmpname, "wb")) == NULL) {
     70 		error("opening dst");
     71 		goto err3;
     72 	}
     73 	if (writeobj(obj, map, dst) < 0) {
     74 		error("writing object");
     75 		goto err5;
     76 	}
     77 	if (fclose(dst) == EOF) {
     78 		error("closing dst");
     79 		goto err4;
     80 	}
     81 	if (remove(fname) == EOF) {
     82 		error("removing fname");
     83 		goto err4;
     84 	}
     85 	if (rename(tmpname, fname) == EOF) {
     86 		error("rename fname");
     87 		goto err4;
     88 	}
     89 
     90 	goto err3;
     91 
     92 err5:
     93 	fclose(dst);
     94 err4:
     95 	remove(tmpname);
     96 err3:
     97 	free(map);
     98 err2:
     99 	delobj(obj);
    100 err1:
    101 	fclose(src);
    102 err0:
    103 	error("cannot strip it");
    104 }
    105 
    106 static void
    107 usage(void)
    108 {
    109 	fputs("usage: strip [file...]\n", stderr);
    110 	exit(EXIT_FAILURE);
    111 }
    112 
    113 int
    114 main(int argc, char *argv[])
    115 {
    116 	ARGBEGIN {
    117 	default:
    118 		usage();
    119 	} ARGEND
    120 
    121 	if (argc == 0) {
    122 		doit("a.out");
    123 	} else {
    124 		for (; *argv; ++argv)
    125 			doit(*argv);
    126 	}
    127 
    128 	return status;
    129 }