From: Yu Watanabe Date: Tue, 13 Dec 2022 08:24:19 +0000 (+0900) Subject: env-file: introduce parse_env_file_fd() X-Git-Tag: v253-rc1~266^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=649512b934f4a762c14380dea4a096263b291feb;p=thirdparty%2Fsystemd.git env-file: introduce parse_env_file_fd() --- diff --git a/src/basic/env-file.c b/src/basic/env-file.c index af82ddfb630..5a4b21ee6aa 100644 --- a/src/basic/env-file.c +++ b/src/basic/env-file.c @@ -376,6 +376,35 @@ int parse_env_file_sentinel( return r; } +int parse_env_file_fd_sentinel( + int fd, + const char *fname, /* only used for logging */ + ...) { + + _cleanup_close_ int fd_ro = -EBADFD; + _cleanup_fclose_ FILE *f = NULL; + va_list ap; + int r; + + assert(fd >= 0); + + fd_ro = fd_reopen(fd, O_CLOEXEC | O_RDONLY); + if (fd_ro < 0) + return fd_ro; + + f = fdopen(fd_ro, "re"); + if (!f) + return -errno; + + TAKE_FD(fd_ro); + + va_start(ap, fname); + r = parse_env_filev(f, fname, ap); + va_end(ap); + + return r; +} + static int load_env_file_push( const char *filename, unsigned line, const char *key, char *value, diff --git a/src/basic/env-file.h b/src/basic/env-file.h index 8da451c74a5..2448d943cd4 100644 --- a/src/basic/env-file.h +++ b/src/basic/env-file.h @@ -9,6 +9,8 @@ int parse_env_filev(FILE *f, const char *fname, va_list ap); int parse_env_file_sentinel(FILE *f, const char *fname, ...) _sentinel_; #define parse_env_file(f, fname, ...) parse_env_file_sentinel(f, fname, __VA_ARGS__, NULL) +int parse_env_file_fd_sentinel(int fd, const char *fname, ...) _sentinel_; +#define parse_env_file_fd(fd, fname, ...) parse_env_file_fd_sentinel(fd, fname, __VA_ARGS__, NULL) int load_env_file(FILE *f, const char *fname, char ***ret); int load_env_file_pairs(FILE *f, const char *fname, char ***ret);