aboutsummaryrefslogtreecommitdiffstats
path: root/libexec_put_argumentsn.c
blob: 9d09ec0d208798f6ad9ab04ee48324578a801819 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


int
libexec_put_argumentsn(struct libexec_command *cmd, const char *const *args, size_t nargs)
{
	size_t i;
	void *new;

	if (!cmd) {
		errno = EINVAL;
		return -1;
	}

	new = realloc(cmd->arguments, (cmd->narguments + nargs + 1) * sizeof(*cmd->arguments));
	if (!new)
		return -1;
	cmd->arguments = new;

	for (i = 0; i < nargs; i++) {
		cmd->arguments[cmd->narguments + i] = strdup(args[i]);
		if (!cmd->arguments[cmd->narguments + i]) {
			while (i)
				free(cmd->arguments[cmd->narguments + --i]);
			if (!cmd->narguments) {
				free(cmd->arguments);
				cmd->arguments = NULL;
			}
			return -1;
		}
	}

	cmd->arguments[cmd->narguments + nargs] = NULL;
	cmd->narguments += nargs;

	return 0;

}


#else
LIBEXEC_CONST__ int main(void) {return 0;} /* TODO test */
#endif