-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquine.c
34 lines (29 loc) · 1.59 KB
/
quine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* This program will print its own source when run.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* q = "/* This program will print its own source when run.\n */\n#include <stdio.h>\n#include <string.h>\n#include <stdlib.h>\n\nchar* q = \"%s\";\n\nchar* escape (char* input) {\n char translateFrom[] = \"\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\\'\";\n char translateTo[] = \"abfnrtv\\\\\\\"\\\'\";\n\n /* In the worst case, we will have to escape every character, so at\n most the size of the string doubles. +1 for trailing \\0.\n */\n char* output = (char*) calloc ((2*strlen(input)+1), sizeof(char));\n char* outputStart = output;\n\n for (;*input; input++) {\n char* pos;\n if (NULL != (pos = strchr(translateFrom, *input))) {\n *output++ = \'\\\\\';\n *output++ = translateTo[pos-translateFrom];\n }\n else\n *output++ = *input;\n }\n return outputStart;\n}\n\nint main() {\n printf (q, escape(q));\n return 0;\n}\n";
char* escape (char* input) {
char translateFrom[] = "\a\b\f\n\r\t\v\\\"\'";
char translateTo[] = "abfnrtv\\\"\'";
/* In the worst case, we will have to escape every character, so at
most the size of the string doubles. +1 for trailing \0.
*/
char* output = (char*) calloc ((2*strlen(input)+1), sizeof(char));
char* outputStart = output;
for (;*input; input++) {
char* pos;
if (NULL != (pos = strchr(translateFrom, *input))) {
*output++ = '\\';
*output++ = translateTo[pos-translateFrom];
}
else
*output++ = *input;
}
return outputStart;
}
int main() {
printf (q, escape(q));
return 0;
}