/* Trojan insertion: try 1.  How to fix the problem? */
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define N  8 * 1024 * 1024
static char buf[N+1];

// not a very robust finger-print, but what can you do.
static char sig[] = "static void compile(char *prog) {";

static char attack[] = 
	"	char *p;\n"
	"	if((p = strstr(prog, sig))) {\n"
	"		p += strlen(sig);\n"
	"		*p = 0;\n"
	"		printf(\"%s\\n%s\", prog, attack);\n"
	"		prog = p+1;\n"
	"	}\n";

// a not very interesting compile
static void compile(char *prog) {
	char *p;
	if((p = strstr(prog, sig))) {
		p += strlen(sig);
		*p = 0;
		printf("%s\n%s", prog, attack);
		prog = p+1;
	}
	printf("%s", prog);
}

int main(int argc, char *argv[]) {

	assert(argc==2);

        int fd;
        assert((fd = open(argv[1], O_RDONLY)) >= 0);
                assert(read(fd, buf, N) > 0);
        close(fd);

	compile(buf);
        return 0;
}
