commit 33bff38345656007e74f1cb08b9cf9cdca3f22eb
parent 9ed5a8907d9f6315a532b49df8c21d073ca1ed62
Author: evenfri <evenfri256@gmail.com>
Date: Mon, 10 Aug 2026 02:58:32 +0800
fix: execvp
Diffstat:
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/execvp.c b/execvp.c
@@ -5,19 +5,22 @@
#include "libc.h"
+static char *empty_env[] = { NULL };
char **environ;
int execvp(const char *file, char *const argv[]) {
+ char **env = (environ != NULL) ? environ : empty_env;
+
if (strchr(file, '/') != 0) {
- return execve(file, argv, environ);
+ return execve(file, argv, env);
}
const char *path_env = NULL;
- if (environ != NULL) {
- for (char **env = environ; *env != NULL; ++env) {
- if (env[0][0] == 'P' && env[0][1] == 'A' &&
- env[0][2] == 'T' && env[0][3] == 'H' && env[0][4] == '=') {
- path_env = *env + 5;
+ if (env != NULL) {
+ for (char **ep = env; *ep != NULL; ++ep) {
+ if (ep[0][0] == 'P' && ep[0][1] == 'A' &&
+ ep[0][2] == 'T' && ep[0][3] == 'H' && ep[0][4] == '=') {
+ path_env = *ep + 5;
break;
}
}
@@ -42,14 +45,14 @@ int execvp(const char *file, char *const argv[]) {
if (dir_len == 0) {
if (file_len + 1 < sizeof(path_buf)) {
memcpy(path_buf, file, file_len + 1);
- execve(path_buf, argv, environ);
+ execve(path_buf, argv, env);
}
} else {
if (dir_len + 1 + file_len + 1 < sizeof(path_buf)) {
memcpy(path_buf, start, dir_len);
path_buf[dir_len] = '/';
memcpy(path_buf + dir_len + 1, file, file_len + 1);
- execve(path_buf, argv, environ);
+ execve(path_buf, argv, env);
}
}