summaryrefslogtreecommitdiffstats
path: root/cquine.in.c
blob: 42860c8c8a716e9aa494499c9042e4d6bb75358c (plain)
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
// 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);
}