mirror of
https://github.com/troglobit/libite.git
synced 2026-10-25 11:54:46 +00:00
New helper function, popenf(), from the Infix project
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
55272c13f7
commit
9eb847e56c
3 files changed
+66
-1
No files matched your search
+1
-1
@@ -10,7 +10,7 @@ libite_la_SOURCES = chomp.c copyfile.c \
|
|||||||
fparseln.c fsendfile.c \
|
fparseln.c fsendfile.c \
|
||||||
ifconfig.c lfile.c \
|
ifconfig.c lfile.c \
|
||||||
makepath.c progress.c \
|
makepath.c progress.c \
|
||||||
pidfile.c pidfilefn.c \
|
pidfile.c pidfilefn.c popenf.c \
|
||||||
reallocarray.c rsync.c strtrim.c \
|
reallocarray.c rsync.c strtrim.c \
|
||||||
strlcpy.c strlcat.c strtonum.c \
|
strlcpy.c strlcat.c strtonum.c \
|
||||||
strdupa.h strndupa.h strnlen.h \
|
strdupa.h strndupa.h strnlen.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 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));
|
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 (const char *basename);
|
||||||
int pidfile_signal(const char *pidfile, int signal);
|
int pidfile_signal(const char *pidfile, int signal);
|
||||||
pid_t pidfile_read (const char *pidfile);
|
pid_t pidfile_read (const char *pidfile);
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
/* Formatted popen()
|
||||||
|
*
|
||||||
|
* Copyright (c) 2023 Tobias Waldekranz <tobias@waldekranz.com>
|
||||||
|
* Copyright (c) 2023 Joachim Wiberg <troglobit@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user