Files
42_EXT_01_dr_quine/C/3_Sully/Sully_exploded.c

56 lines
1.2 KiB
C

#define _GNU_SOURCE /* for asprintf */
#include <stdlib.h> /* for free and system */
#include <string.h> /* for __FILE__ */
#include <stdio.h>
#define xstr(s) #s
#define str(s) xstr(s)
#define FT(s) \
int main() \
{ \
\
char *before = "\
#define _GNU_SOURCE /* for asprintf */\n\
#include <stdlib.h> /* for free and system */\n\
#include <string.h> /* for __FILE__ */\n\
#include <stdio.h>\n\
#define xstr(s) #s\n\
#define str(s) xstr(s)\n\
#define FT(s) "; \
char *quine = s; \
char *after = "\
\n\
int i = %i;\n\
char *file_name = %s%s;\n\
FT(str(FT(s)))\n"; \
\
if (i == 0) \
return 0; \
if ((strchr(file_name, '_')) != ((void*)0)) \
i--;\
\
char *kid_name; \
char *kid_name_c; \
char *after_expanded; \
char *cmd; \
\
asprintf(&kid_name, "Sully_%i", i); \
asprintf(&kid_name_c, "%s.c", kid_name); \
\
FILE *file = fopen(kid_name_c, "w"); \
asprintf(&after_expanded, after, i, "__FI", "LE__"); \
fprintf(file, "%s%s%s", before, quine, after_expanded); \
fclose(file); \
\
asprintf(&cmd, "clang %1$s -o %2$s ; ./%2$s", kid_name_c, kid_name); \
system(cmd); \
\
free(cmd); \
free(kid_name_c); \
free(kid_name); \
free(after_expanded); \
return 0; \
}
int i = 5;
char *file_name = __FILE__;
FT(str(FT(s)))