From 9eb847e56c8a2ade8d0fdefca3c9915c9caaad4b Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 6 Jun 2023 18:23:04 +0200 Subject: [PATCH] New helper function, popenf(), from the Infix project Signed-off-by: Joachim Wiberg --- src/Makefile.am | 2 +- src/lite.h | 2 ++ src/popenf.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/popenf.c diff --git a/src/Makefile.am b/src/Makefile.am index 0f1c335..c9b8eac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,7 @@ libite_la_SOURCES = chomp.c copyfile.c \ fparseln.c fsendfile.c \ ifconfig.c lfile.c \ makepath.c progress.c \ - pidfile.c pidfilefn.c \ + pidfile.c pidfilefn.c popenf.c \ reallocarray.c rsync.c strtrim.c \ strlcpy.c strlcat.c strtonum.c \ strdupa.h strndupa.h strnlen.h \ diff --git a/src/lite.h b/src/lite.h index 487ba12..2c2426a 100644 --- a/src/lite.h +++ b/src/lite.h @@ -104,6 +104,8 @@ int makepath (const char *dir); int dir (const char *dir, const char *type, int (*filter) (const char *file), char ***list, int strip); int rsync (char *src, char *dst, int opt, int (*filter) (const char *file)); +FILE *popenf (const char *type, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); + int pidfile (const char *basename); int pidfile_signal(const char *pidfile, int signal); pid_t pidfile_read (const char *pidfile); diff --git a/src/popenf.c b/src/popenf.c new file mode 100644 index 0000000..67ae191 --- /dev/null +++ b/src/popenf.c @@ -0,0 +1,63 @@ +/* Formatted popen() + * + * Copyright (c) 2023 Tobias Waldekranz + * Copyright (c) 2023 Joachim Wiberg + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include +#include +#include + +/** + * Formatted pipe stream to or from a process + * @param type A popen() type, "r" or "w", can also include "e" for @c O_CLOEXEC. + * @param fmt Formatted string to be composed into a shell command line. + * + * This function is an extension to popen(), lessening the burden of + * first having to compose the filename from parts in a seprate buffer. + * + * @returns Upon successful completion, popenf() returns a FILE pointer. + * Otherwise, @c NULL is returned and @a errno is set to indicate the + * error. + */ +FILE *popenf(const char *type, const char *fmt, ...) +{ + va_list ap; + char *cmd; + int len; + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + cmd = alloca(len + 1); + if (!cmd) { + errno = ENOMEM; + return NULL; + } + + va_start(ap, fmt); + vsnprintf(cmd, len + 1, fmt, ap); + va_end(ap); + + return popen(cmd, type); +}