diff options
Diffstat (limited to 'cquine.in.c')
-rw-r--r-- | cquine.in.c | 27 |
1 files changed, 27 insertions, 0 deletions
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 <tomasz@kramkow.ski> +// SPDX-LicenseIdentifier: GPL-3.0-or-later + +#include <stdio.h> +#include <string.h> + +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); +} |