-
Notifications
You must be signed in to change notification settings - Fork 0
/
execvp.c
153 lines (134 loc) · 2.87 KB
/
execvp.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2021-2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <paths.h>
#include <limits.h>
#include <unistd.h>
#include "iamroot.h"
#ifndef __FreeBSD__
#define getenv _getenv
extern int __execve(const char *, char * const [], char * const []);
/*
* Stolen and hacked from musl (src/process/execvp.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
static int __execvp(const char *file, char * const argv[])
{
const char *p, *z, *path = getenv("PATH");
size_t l, k;
int seen_eacces = 0;
errno = ENOENT;
if (!*file) return -1;
if (strchr(file, '/'))
return __execve(file, argv, __environ);
if (!path) path = _PATH_DEFPATH;
k = strnlen(file, NAME_MAX+1);
if (k > NAME_MAX) {
errno = ENAMETOOLONG;
return -1;
}
l = strnlen(path, PATH_MAX-1)+1;
for(p=path; ; p=z) {
char b[l+k+1];
z = __strchrnul(p, ':');
if ((size_t)(z-p) >= l) {
if (!*z++) break;
continue;
}
memcpy(b, p, z-p);
b[z-p] = '/';
memcpy(b+(z-p)+(z>p), file, k+1);
__execve(b, argv, __environ);
switch (errno) {
case EACCES:
seen_eacces = 1;
case ENOENT:
case ENOTDIR:
break;
default:
return -1;
}
if (!*z++) break;
}
if (seen_eacces) errno = EACCES;
return -1;
}
#undef getenv
#else
/*
* Stolen from musl (src/process/execvp.c)
*
* SPDX-FileCopyrightText: The musl Contributors
*
* SPDX-License-Identifier: MIT
*/
static int __execvP(const char *file, const char *path, char * const argv[])
{
const char *p, *z;
size_t l, k;
int seen_eacces = 0;
errno = ENOENT;
if (!*file) return -1;
if (strchr(file, '/'))
return __execve(file, argv, environ);
if (!path) path = _PATH_DEFPATH;
k = strnlen(file, NAME_MAX+1);
if (k > NAME_MAX) {
errno = ENAMETOOLONG;
return -1;
}
l = strnlen(path, PATH_MAX-1)+1;
for(p=path; ; p=z) {
char b[l+k+1];
z = __strchrnul(p, ':');
if ((size_t)(z-p) >= l) {
if (!*z++) break;
continue;
}
memcpy(b, p, z-p);
b[z-p] = '/';
memcpy(b+(z-p)+(z>p), file, k+1);
__execve(b, argv, environ);
switch (errno) {
case EACCES:
seen_eacces = 1;
case ENOENT:
case ENOTDIR:
break;
default:
return -1;
}
if (!*z++) break;
}
if (seen_eacces) errno = EACCES;
return -1;
}
int execvP(const char *file, const char *path, char * const argv[])
{
__debug("%s(file: '%s', path: '%s' argv: { '%s', '%s', ... })\n",
__func__, file, path, argv[0], argv[1]);
/* Forward to local function */
return __execvP(file, path, argv);
}
#endif
int execvp(const char *file, char * const argv[])
{
__debug("%s(file: '%s', argv: { '%s', '%s', ... })\n", __func__, file,
argv[0], argv[1]);
#ifdef __FreeBSD__
/* Forward to another function */
return execvP(file, _getenv("PATH"), argv);
#else
/* Forward to local function */
return __execvp(file, argv);
#endif
}