tControl and quote characters in string configuration variables are now properl… | |
git clone git://src.adamsgaard.dk/vaccinewars | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 5cd6140008fa710b14d0c86e3a5553df065cacdb | |
parent bf1956fb9811da51db35f13b89f643347538b9c8 | |
Author: Ben Webb <[email protected]> | |
Date: Sat, 2 Mar 2002 19:36:18 +0000 | |
Control and quote characters in string configuration variables are now | |
properly escaped to avoid writing out broken config files. | |
Diffstat: | |
M src/dopewars.c | 50 +++++++++++++++++++++++++++++… | |
1 file changed, 47 insertions(+), 3 deletions(-) | |
--- | |
diff --git a/src/dopewars.c b/src/dopewars.c | |
t@@ -1967,6 +1967,47 @@ gboolean CheckMaxIndex(GScanner *scanner, int GlobalInd… | |
} | |
/* | |
+ * Prints the given string to a file, converting control characters | |
+ * and escaping other special characters. | |
+ */ | |
+static void PrintEscaped(FILE *fp, gchar *str) | |
+{ | |
+ int i; | |
+ | |
+ for (i = 0; i < strlen(str); i++) { | |
+ switch(str[i]) { | |
+ case '"': | |
+ case '\'': | |
+ case '\\': | |
+ fputc('\\', fp); | |
+ fputc(str[i], fp); | |
+ break; | |
+ case '\n': | |
+ fputs("\\n", fp); | |
+ break; | |
+ case '\t': | |
+ fputs("\\t", fp); | |
+ break; | |
+ case '\r': | |
+ fputs("\\r", fp); | |
+ break; | |
+ case '\b': | |
+ fputs("\\b", fp); | |
+ break; | |
+ case '\f': | |
+ fputs("\\f", fp); | |
+ break; | |
+ default: | |
+ if (isprint(str[i])) { | |
+ fputc(str[i], fp); | |
+ } else { | |
+ fprintf(fp, "\\%o", str[i]); | |
+ } | |
+ } | |
+ } | |
+} | |
+ | |
+/* | |
* Writes a single configuration file variable (identified by GlobalIndex | |
* and StructIndex) to the specified file, in a format suitable for reading | |
* back in (via. ParseNextConfig and friends). | |
t@@ -1996,8 +2037,9 @@ static void WriteConfigValue(FILE *fp, int GlobalIndex, … | |
fprintf(fp, "%s = %s\n", GlobalName, prstr); | |
g_free(prstr); | |
} else if (Globals[GlobalIndex].StringVal) { | |
- fprintf(fp, "%s = \"%s\"\n", GlobalName, | |
- *GetGlobalString(GlobalIndex, StructIndex)); | |
+ fprintf(fp, "%s = \"", GlobalName); | |
+ PrintEscaped(fp, *GetGlobalString(GlobalIndex, StructIndex)); | |
+ fprintf(fp, "\"\n"); | |
} else if (Globals[GlobalIndex].StringList) { | |
int i; | |
t@@ -2005,7 +2047,9 @@ static void WriteConfigValue(FILE *fp, int GlobalIndex, … | |
for (i = 0; i < *Globals[GlobalIndex].MaxIndex; i++) { | |
if (i > 0) | |
fprintf(fp, ", "); | |
- fprintf(fp, "\"%s\"", (*Globals[GlobalIndex].StringList)[i]); | |
+ fputc('"', fp); | |
+ PrintEscaped(fp, (*Globals[GlobalIndex].StringList)[i]); | |
+ fputc('"', fp); | |
} | |
fprintf(fp, " }\n"); | |
} |