Appendix A: Source Code

This appendix contains all the individual programs which appear in the report, along with the ones which are referred to but which to not appear in the body of the report.

Failure I

    char*f="main(){printf(f);printf(f);}";main(){printf(f);printf(f);}

Failure II

    char*f="main(){printf("char*f="");printf(f);printf("";\n");printf(f);}";
    main(){printf("char*f="");printf(f);printf("";\n");printf(f);}

Failure III

    char*f="main(){printf(\"char*f=\"\");printf(f);printf(\"\";\\n\");printf(f);}";
    main(){printf("char*f=\"");printf(f);printf("\";\n");printf(f);}

Self 0

    char*f="main(){putchar('c');putchar('h');putchar('a');putchar('r');putchar('*');&
        putchar('f');putchar('=');putchar(34);printf(f);putchar(34);putchar(';');&
        putchar(10);printf(f);putchar(10);}";
    main(){putchar('c');putchar('h');putchar('a');putchar('r');putchar('*');putchar('f');&
        putchar('=');putchar(34);printf(f);putchar(34);putchar(';');putchar(10);&
        printf(f);putchar(10);}

Self I

    char*f="char*f=;main(){f[7]=0;printf(f);putchar(34);f[7]=';';printf(f);&
        putchar(34);f[8]=0;printf(&f[7]);f[8]='m';putchar(10);printf(&f[8]);putchar(10);}";
    main(){f[7]=0;printf(f);putchar(34);f[7]=';';printf(f);putchar(34);&
        f[8]=0;printf(&f[7]);f[8]='m';putchar(10);printf(&f[8]);putchar(10);}

Self II

    char*f="char*f=%c%s%c;%cmain(){printf(f,34,f,34,10,10);}%c";
    main(){printf(f,34,f,34,10,10);}

Self III

    char a='"';char b='\n';char c='\\';
    char*f="char a='%c';char b='%cn';char c='%c%c';%cchar*f=%c%s%c;%cmain(){&
        printf(f,a,c,c,c,b,a,f,a,b,b);}%c";
    main(){printf(f,a,c,c,c,b,a,f,a,b,b);}

Self IV

    char*lines[]={
    "char*lines[]={",
    "%c%s%c%c%c",
    "0};",
    "main(){",
    "int idx;",
    "puts(lines[0]);",
    "for(idx=0;lines[idx]!=0;idx++){",
    "printf(lines[1],34,lines[idx],34,',',10);",
    "}",
    "puts(lines[2]);",
    "for(idx=3;lines[idx]!=0;idx++){",
    "puts(lines[idx]);",
    "}",
    "}",
    0};
    main(){
    int idx;
    puts(lines[0]);
    for(idx=0;lines[idx]!=0;idx++){
    printf(lines[1],34,lines[idx],34,',',10);
    }
    puts(lines[2]);
    for(idx=3;lines[idx]!=0;idx++){
    puts(lines[idx]);
    }
    }
(Self IV assumes in advance that it will be iterating across an array of strings rather than a single string. It iterates across the array the first time to output the mention, namely the definition of the array. The second iteration provides outputs the use).

Cheat I

    extern void print_a_prog();
    main(){print_a_prog();}

Cheat II

    #include 
    main(){
        int c;
        FILE *f;
        f = fopen( __FILE__, "r" );
        c = fgetc( f );
        while( c != EOF ) {
            putchar( c );
            c = fgetc( f );
        }
        fclose( f );
    }

Cheat III

    main(){
        Find myself in the computer's memory.
        Output all the bits that make me up.
    }

[back] [up] [right]