summaryrefslogtreecommitdiffstats
path: root/libc/unistd/execlp.c
blob: 923a2e22aade7202f632a9f2dd1933a97ab3bb22 (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

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

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

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

	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++]);

	va_end(args);

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

	return i;
}