summaryrefslogtreecommitdiff
path: root/src/shared/fileio.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2013-04-03 19:56:39 +0200
committerLennart Poettering <lennart@poettering.net>2013-04-03 20:12:57 +0200
commit768100efd57ffbbefe9beaa33d1dd5ecc7f69173 (patch)
tree13dc0dd86086cf1985dca2bdbc8c63b060b7d5d7 /src/shared/fileio.c
parentf73141d7657b3f60b8669bc8386413d8a8a372c6 (diff)
fileio: write proper env var write-out code
This will properly escape all weird chars when writing env var files. With this in place we can now read and write environment files where the values contain arbitrary weird chars. This enables hostnamed and suchlike to finally properly save pretty host names with backlashes or quotes in them.
Diffstat (limited to 'src/shared/fileio.c')
-rw-r--r--src/shared/fileio.c37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
index 96e23c5bbb..2ccb9e7edc 100644
--- a/src/shared/fileio.c
+++ b/src/shared/fileio.c
@@ -522,6 +522,37 @@ int load_env_file(const char *fname, const char *newline, char ***rl) {
return 0;
}
+static void write_env_var(FILE *f, const char *v) {
+ const char *p;
+
+ p = strchr(v, '=');
+ if (!p) {
+ /* Fallback */
+ fputs(v, f);
+ fputc('\n', f);
+ return;
+ }
+
+ p++;
+ fwrite(v, 1, p-v, f);
+
+ if (string_has_cc(p) || chars_intersect(p, WHITESPACE "\'\"\\")) {
+ fputc('\"', f);
+
+ for (; *p; p++) {
+ if (strchr("\'\"\\", *p))
+ fputc('\\', f);
+
+ fputc(*p, f);
+ }
+
+ fputc('\"', f);
+ } else
+ fputs(p, f);
+
+ fputc('\n', f);
+}
+
int write_env_file(const char *fname, char **l) {
char **i;
char _cleanup_free_ *p = NULL;
@@ -535,10 +566,8 @@ int write_env_file(const char *fname, char **l) {
fchmod_umask(fileno(f), 0644);
errno = 0;
- STRV_FOREACH(i, l) {
- fputs(*i, f);
- fputc('\n', f);
- }
+ STRV_FOREACH(i, l)
+ write_env_var(f, *i);
fflush(f);