Don't read: foo.c solution.

Christopher Barry cbarry@2xtreme.net
Sat, 24 Oct 1998 16:35:44 -0700


Here it is:
-----------
#include <stdio.h>

char	s[] = {
	'\t',
	'0',
	'\n',
	'}',
	';',
	'\n',
	'\n',
	'i',
	'n',
	't',
	' ',
	'm',
	'a',
	'i',
	'n',
	'(',
	'v',
	'o',
	'i',
	'd',
	')',
	'\n',
	'{',
	'\n',
	'\t',
	'i',
	'n',
	't',
	' ',
	'i',
	';',
	'\n',
	'\n',
	'\t',
	'p',
	'r',
	'i',
	'n',
	't',
	'f',
	'(',
	'"',
	'#',
        'i',
        'n',
        'c',
        'l',
        'u',
        'd',
        'e',
        ' ',
        '<',
        's',
        't',
        'd',
        'i',
        'o',
        '.',
        'h',
        '>',
        '\\',
	'n',
	'\\',
	'n',
	'c',
	'h',
	'a',
	'r',
	'\\',
	't',
	's',
	'[',
	']',
	' ',
	'=',
	' ',
	'{',
	'\\',
	'n',
	'"',
	')',
	';',
	'\n',
	'\t',
	'f',
	'o',
	'r',
	'(',
	'i',
       	'=',
	'0',
	';',
	' ',
	's',
	'[',
	'i',
	']',
	';',
	' ',
	'i',
	'+',
	'+',
	')',
	'\n',
	'\t',
	'\t',
	'p',
	'r',
	'i',
	'n',
	't',
	'f',
	'(',
	'"',
	'\\',
	't',
	'%',
	'd',
	',',
	' ',
	'\\',
	'n',
	'"',
	',',
	' ',
	's',
	'[',
	'i',
	']',
	')',
	';',
	'\n',
	'\t',
	'p',
	'r',
	'i',
	'n',
	't',
	'f',
	'(',
	'"',
	'%',
	's',
	'"',
	',',
	' ',
	's',
	')',
	';',
	'\n',
	'\n',
	'\t',
	'r',
	'e',
	't',
	'u',
	'r',
	'n',
	' ',
	'0',
	';',
	'\n',
	'}',
	'\n',
	0
};

int main(void)
{
	int i;

	printf("#include <stdio.h>\n\nchar\ts[] = {\n");
	for(i=0; s[i]; i++)
		printf("\t%d, \n", s[i]);
	printf("%s", s);

	return 0;
}

----------

Now this isn't the actual foo.c, but after compiling it will generate as
it's output the true foo.c. So if you named the above source bar.c then:
$ cc bar.c -o bar
$ bar > foo.c

You'll get a program that truly generates it's source. The above program
is more architecture neutral than the real foo.c, which is character set
dependent.

It's obvious that the above program's structure can be included into any
source file so that the resulting binary carries it's own source code
with it as baggage, including all comments and everything(though this is
useless), and it would be trivial to write a preprocessor to do this to
(I generated the above generator program by writing a generator for it
rather than do all that typing).

Anyways, I hope this helps to demonstrate a lot of the points I raised
in "Replies to replies", and maybe quietly humbled a few of you.

Best of luck to you all. I'll check back now and then,
Christopher