/**** Duplicator Maker by Brian Shucker Nov 21, 2000 Usage: dupMaker This program takes C source code for a program P as input and generates C source code for program P' as output. The program P', when executed, will print a copy of itself to a specified file and then execute as program P. The program P' will strip off the first command-line argument, which it expects to be the name of the file to output its duplicate to. It will pass the remaining command-line arguments through to program P in a transparent manner (that is, it will renumber the arguments so that P is unaware that the first argument existed). Important restrictions: 1. program P must use "int myMain(int argc, char *argv[])" as its main function 2. program P may not have a global symbol "__s" or "main()" Anything else goes! ****/ #include #define TRUE 1 #define FALSE 0 #define HEADER_SIZE 1000 char header[HEADER_SIZE]; int main(int argc, char *argv[]) { int i; char ch; FILE *in, *out; /*check that source and output file names are given*/ if(argc != 3) { fprintf(stderr, "Usage: dupMaker \n"); exit(0); } /*open source file for read*/ if((in = fopen(argv[1], "r")) == NULL) { fprintf(stderr, "Cannot open source file %s\n", argv[1]); exit(0); } /*open output file for write*/ if((out = fopen(argv[2], "w")) == NULL) { fprintf(stderr, "Cannot create output file %s\n", argv[2]); exit(0); } /*put header code into buffer*/ for(i=0; i int myMain(int argc, char *argv[]); int main(int argc, char *argv[]){ int i; FILE *out; if(argc < 2) { fprintf(stderr, \"First argument must be file name for duplicate\\n\"); exit(0); } if((out = fopen(argv[1], \"w\")) == NULL) { fprintf(stderr, \"Cannot create duplicate file %s\\n\", argv[1]); exit(0); } printf(\"Creating duplicate source in file %s\\n\\n\", argv[1]); fprintf(out, \"char __s[] = \\\"\"); for(i=0; __s[i]; i++) { if(__s[i] == '\\\\') fprintf(out, \"\\\\\\\\\"); else if (__s[i] == '\\\"') fprintf(out, \"\\\\\\\"\"); else fprintf(out, \"%c\", __s[i]); } fprintf(out, \"%s\", __s); argv[1] = argv[0]; myMain(argc - 1, &argv[1]); } /**end header code**/ "); /*print out char encoding for header code*/ fprintf(out, "char __s[] = \""); fprintf(out, "\\\";\\n\\n"); for(i=0; i