summaryrefslogtreecommitdiff
path: root/libc/unistd/execvep.c
blob: 6cd8cbaf9bdc0bfd958a14f42fb4f43227629128 (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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int execvep(const char *path, char *__const argv[], char *__const envp[])
{
    if (!strchr(path, '/')) {
	char *p = getenv("PATH");

	if (!p)
	    p = "/bin:/usr/bin";

	for (; p && *p;) {
	    char partial[FILENAME_MAX];
	    char *p2;

	    p2 = strchr(p, ':');
	    if (p2) {
		size_t len = p2 - p;
		strncpy(partial, p, len);
		partial[len] = 0;
	    } else {
		strcpy(partial, p);
	    }

	    if (strlen(partial))
		strcat(partial, "/");
	    strcat(partial, path);

	    execve(partial, argv, envp);

	    if (errno != ENOENT)
		return -1;

	    if (p2) {
		p = p2 + 1;
	    } else {
		p = 0;
	    }
	}
	return -1;
    } else
	return execve(path, argv, envp);
}