mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-08 12:18:51 +00:00
Add helper functions to convert va_list of char* to char**.
Signed-off-by: Christian Seiler <christian@iwakd.de> Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
parent
9c4693b853
commit
61a1d519f4
@ -26,6 +26,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
@ -447,3 +448,48 @@ int sha1sum_file(char *fnam, unsigned char *digest)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup)
|
||||||
|
{
|
||||||
|
va_list ap2;
|
||||||
|
size_t count = 1 + skip;
|
||||||
|
char **result;
|
||||||
|
|
||||||
|
/* first determine size of argument list, we don't want to reallocate
|
||||||
|
* constantly...
|
||||||
|
*/
|
||||||
|
va_copy(ap2, ap);
|
||||||
|
while (1) {
|
||||||
|
char* arg = va_arg(ap2, char*);
|
||||||
|
if (!arg)
|
||||||
|
break;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
va_end(ap2);
|
||||||
|
|
||||||
|
result = calloc(count, sizeof(char*));
|
||||||
|
if (!result)
|
||||||
|
return NULL;
|
||||||
|
count = skip;
|
||||||
|
while (1) {
|
||||||
|
char* arg = va_arg(ap, char*);
|
||||||
|
if (!arg)
|
||||||
|
break;
|
||||||
|
arg = do_strdup ? strdup(arg) : arg;
|
||||||
|
if (!arg)
|
||||||
|
goto oom;
|
||||||
|
result[count++] = arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* calloc has already set last element to NULL*/
|
||||||
|
return result;
|
||||||
|
|
||||||
|
oom:
|
||||||
|
free(result);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip)
|
||||||
|
{
|
||||||
|
return (const char**)lxc_va_arg_list_to_argv(ap, skip, 0);
|
||||||
|
}
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#define _utils_h
|
#define _utils_h
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <stdarg.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
@ -182,4 +183,8 @@ extern ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const voi
|
|||||||
extern int sha1sum_file(char *fnam, unsigned char *md_value);
|
extern int sha1sum_file(char *fnam, unsigned char *md_value);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* convert variadic argument lists to arrays (for execl type argument lists) */
|
||||||
|
extern char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
|
||||||
|
extern const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user