summaryrefslogtreecommitdiff
path: root/libc/unistd/execle.c
blob: 14c7a0bfc2b7b3a94e2503ca7635545a41e64419 (plain)
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
46
47
48
49
50
51
52
53

#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>

extern int execvep(const char *path, char *const argv[], char *const envp[]);

int execle(const char *file, const char *arg, ...)
{
	const char *shortargv[16];
	const char **argv;
	const char *c;
	int i;
	va_list args;
	const char *const *envp;

	i = 1;

	va_start(args, arg);

	do {
		c = va_arg(args, const char *);

		i++;
	} while (c);

	va_end(args);

	if (i <= 16)
		argv = shortargv;
	else {
		argv = (const char **) alloca(sizeof(char *) * i);
	}

	argv[0] = arg;
	i = 1;

	va_start(args, arg);

	do {
		argv[i] = va_arg(args, const char *);
	} while (argv[i++]);

	envp = va_arg (args, const char *const *);
	va_end(args);

	i = execvep(file, (char *const *) argv, (char *const *) envp);

	if (argv != shortargv)
		free(argv);

	return i;
}