// 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); }