aboutsummaryrefslogblamecommitdiffstats
path: root/libexec_put_argumentsn.c
blob: 9d09ec0d208798f6ad9ab04ee48324578a801819 (plain) (tree)












































                                                                                               
/* 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