Skip to content

drmats/selfer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

selfer

My naive take on quines in C.


what?

Read the Reflections on Trusting Trust.


how?

$ gcc ./src.c && ./a.out > ./src2.c && diff -sq ./src.c ./src2.c && rm ./a.out src2.c

/* (c) 2019, xcmats */
#include <stdio.h>
char *src[] = {
    "/* (c) 2019, xcmats */",
    "#include <stdio.h>",
    "char *src[] = {",
    "}, buf[100];",
    "char *esc (char *b, char *s) {",
    "    int i, j;",
    "    for (i = 0, j = 0; s[i] != 0; i++, j++) {",
    "        if (s[i] == '\\\\' || s[i] == '\\\"') b[j++] = '\\\\';",
    "        b[j] = s[i];",
    "    }",
    "    b[j] = 0;",
    "    return b;",
    "}",
    "int main () {",
    "    int i;",
    "    for (i = 0; i < 3; i++) puts(src[i]);",
    "    for (i = 0; i < 19; i++) printf(\"    \\\"%s\\\",\\n\", esc(buf, src[i]));",
    "    printf(\"    \\\"%s\\\"\\n\", esc(buf, src[19]));",
    "    for (i = 3; i < 20; i++) puts(src[i]);",
    "}"
}, buf[100];
char *esc (char *b, char *s) {
    int i, j;
    for (i = 0, j = 0; s[i] != 0; i++, j++) {
        if (s[i] == '\\' || s[i] == '\"') b[j++] = '\\';
        b[j] = s[i];
    }
    b[j] = 0;
    return b;
}
int main () {
    int i;
    for (i = 0; i < 3; i++) puts(src[i]);
    for (i = 0; i < 19; i++) printf("    \"%s\",\n", esc(buf, src[i]));
    printf("    \"%s\"\n", esc(buf, src[19]));
    for (i = 3; i < 20; i++) puts(src[i]);
}

Releases

No releases published

Packages

No packages published

Languages