mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 16:37:19 +09:00
env-file: add helper for printing a properly escaped env var file assignment
This commit is contained in:
@@ -570,6 +570,41 @@ int merge_env_file(
|
||||
return parse_env_file_internal(f, fname, merge_env_file_push, env);
|
||||
}
|
||||
|
||||
static void env_file_fputs_escaped(FILE *f, const char *p) {
|
||||
assert(f);
|
||||
assert(p);
|
||||
|
||||
flockfile(f);
|
||||
|
||||
if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
|
||||
fputc_unlocked('"', f);
|
||||
|
||||
for (; *p; p++) {
|
||||
if (strchr(SHELL_NEED_ESCAPE, *p))
|
||||
fputc_unlocked('\\', f);
|
||||
|
||||
fputc_unlocked(*p, f);
|
||||
}
|
||||
|
||||
fputc_unlocked('"', f);
|
||||
} else
|
||||
fputs_unlocked(p, f);
|
||||
|
||||
funlockfile(f);
|
||||
}
|
||||
|
||||
void env_file_fputs_assignment(FILE *f, const char *k, const char *v) {
|
||||
assert(f);
|
||||
assert(k);
|
||||
|
||||
if (!v)
|
||||
return;
|
||||
|
||||
fputs(k, f);
|
||||
env_file_fputs_escaped(f, v);
|
||||
fputc('\n', f);
|
||||
}
|
||||
|
||||
static void write_env_var(FILE *f, const char *v) {
|
||||
const char *p;
|
||||
|
||||
@@ -587,19 +622,7 @@ static void write_env_var(FILE *f, const char *v) {
|
||||
p++;
|
||||
fwrite_unlocked(v, 1, p-v, f);
|
||||
|
||||
if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
|
||||
fputc_unlocked('"', f);
|
||||
|
||||
for (; *p; p++) {
|
||||
if (strchr(SHELL_NEED_ESCAPE, *p))
|
||||
fputc_unlocked('\\', f);
|
||||
|
||||
fputc_unlocked(*p, f);
|
||||
}
|
||||
|
||||
fputc_unlocked('"', f);
|
||||
} else
|
||||
fputs_unlocked(p, f);
|
||||
env_file_fputs_escaped(f, p);
|
||||
|
||||
fputc_unlocked('\n', f);
|
||||
}
|
||||
|
||||
@@ -22,3 +22,5 @@ int merge_env_file(char ***env, FILE *f, const char *fname);
|
||||
int write_env_file(int dir_fd, const char *fname, char **headers, char **l);
|
||||
|
||||
int write_vconsole_conf(int dir_fd, const char *fname, char **l);
|
||||
|
||||
void env_file_fputs_assignment(FILE *f, const char *k, const char *v);
|
||||
|
||||
Reference in New Issue
Block a user