env-util: make write_env_file() optionally take headers

This also makes write_env_file() and write_env_file_label() optionally
take dir_fd, and drop write_env_file_at().

Preparation for later commits.
This commit is contained in:
Yu Watanabe
2023-10-31 17:15:21 +09:00
parent 78101ca9cc
commit f155cb6d75
10 changed files with 19 additions and 16 deletions
+7 -1
View File
@@ -603,7 +603,7 @@ static void write_env_var(FILE *f, const char *v) {
fputc_unlocked('\n', f);
}
int write_env_file_at(int dir_fd, const char *fname, char **l) {
int write_env_file(int dir_fd, const char *fname, char **headers, char **l) {
_cleanup_fclose_ FILE *f = NULL;
_cleanup_free_ char *p = NULL;
int r;
@@ -617,6 +617,12 @@ int write_env_file_at(int dir_fd, const char *fname, char **l) {
(void) fchmod_umask(fileno(f), 0644);
STRV_FOREACH(i, headers) {
assert(isempty(*i) || startswith(*i, "#"));
fputs_unlocked(*i, f);
fputc_unlocked('\n', f);
}
STRV_FOREACH(i, l)
write_env_var(f, *i);
+1 -4
View File
@@ -19,7 +19,4 @@ int load_env_file_pairs_fd(int fd, const char *fname, char ***ret);
int merge_env_file(char ***env, FILE *f, const char *fname);
int write_env_file_at(int dir_fd, const char *fname, char **l);
static inline int write_env_file(const char *fname, char **l) {
return write_env_file_at(AT_FDCWD, fname, l);
}
int write_env_file(int dir_fd, const char *fname, char **headers, char **l);