aboutsummaryrefslogtreecommitdiff
path: root/src/process/execvp.c
blob: d799ddae248403fd6a41df818e7b6d957abfc60d (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
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

extern char **__environ;

int execvp(const char *file, char *const argv[])
{
	const char *p, *z, *path = getenv("PATH");
	int l;

	if (strchr(file, '/'))
		return execve(file, argv, __environ);

	/* FIXME: integer overflows */
	if (!path) path = "/usr/local/bin:/bin:/usr/bin";
	l = strlen(file) + strlen(path) + 2;

	for(p=path; p && *p; p=z) {
		char b[l];
		z = strchr(p, ':');
		if (z) {
			memcpy(b, p, z-p);
			b[z++-p] = 0;
		} else strcpy(b, p);
		strcat(b, "/");
		strcat(b, file);
		if (!access(b, X_OK))
			return execve(b, argv, __environ);
	}
	errno = ENOENT;
	return -1;
}