From 8d51757e02818146b05c805b81e167f118fca7dc Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Tue, 12 Nov 2024 13:36:10 +0000 Subject: The quine --- cquine.in.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 cquine.in.c (limited to 'cquine.in.c') diff --git a/cquine.in.c b/cquine.in.c new file mode 100644 index 0000000..42860c8 --- /dev/null +++ b/cquine.in.c @@ -0,0 +1,27 @@ +// SPDX-FileCopyrightText: 2024 Tomasz Kramkowski +// SPDX-LicenseIdentifier: GPL-3.0-or-later + +#include +#include + +void escape(char *dest, const char *src) +{ + for (size_t i = 0; src[i] != '\0'; i++) { + switch (src[i]) { + case '\t': *dest++ = '\\'; *dest++ = 't'; break; + case '\n': *dest++ = '\\'; *dest++ = 'n'; break; + case '"': *dest++ = '\\'; *dest++ = '"'; break; + case '\\': *dest++ = '\\'; *dest++ = '\\'; break; + default: *dest++ = src[i]; break; + } + } + *dest = '\0'; +} + +int main(void) +{ + const char *program = "%s"; + char escaped[strlen(program) * 2 + 1]; + escape(escaped, program); + printf(program, escaped); +} -- cgit v1.2.3-54-g00ecf