Loading...
gen/FreeBSD/exec.c Libc-1725.40.4 Libc-1583.60.2
--- Libc/Libc-1725.40.4/gen/FreeBSD/exec.c
+++ Libc/Libc-1583.60.2/gen/FreeBSD/exec.c
@@ -52,9 +52,6 @@
 #include <crt_externs.h>
 #define environ (*_NSGetEnviron())
 
-static const char execvPe_err_preamble[] = "execvP: ";
-static const char execvPe_err_trailer[] = ": path too long\n";
-
 int
 _execvpe(const char *name, char * const argv[], char * const envp[]);
 
@@ -158,8 +155,8 @@
 	const char **memp;
 	size_t cnt, lp, ln;
 	int eacces, save_errno;
-	char buf[MAXPATHLEN];
-	const char *bp, *np, *op, *p;
+	char *cur, buf[MAXPATHLEN];
+	const char *p, *bp;
 	struct stat sb;
 
 	eacces = 0;
@@ -167,11 +164,7 @@
 	/* If it's an absolute or relative path name, it's easy. */
 	if (strchr(name, '/')) {
 		bp = name;
-#ifdef __APPLE__
-		/* Used to differentiate PATH search vs. not afterwards. */
-		path = NULL;
-#endif
-		op = NULL;
+		cur = NULL;
 		goto retry;
 	}
 	bp = buf;
@@ -182,30 +175,23 @@
 		return (-1);
 	}
 
-	op = path;
-	ln = strlen(name);
-	while (op != NULL) {
-		np = strchrnul(op, ':');
-
+	cur = alloca(strlen(path) + 1);
+	if (cur == NULL) {
+		errno = ENOMEM;
+		return (-1);
+	}
+	strcpy(cur, path);
+	while ((p = strsep(&cur, ":")) != NULL) {
 		/*
 		 * It's a SHELL path -- double, leading and trailing colons
 		 * mean the current directory.
 		 */
-		if (np == op) {
-			/* Empty component. */
+		if (*p == '\0') {
 			p = ".";
 			lp = 1;
-		} else {
-			/* Non-empty component. */
-			p = op;
-			lp = np - op;
-		}
-
-		/* Advance to the next component or terminate after this. */
-		if (*np == '\0')
-			op = NULL;
-		else
-			op = np + 1;
+		} else
+			lp = strlen(p);
+		ln = strlen(name);
 
 		/*
 		 * If the path is too long complain.  This is a possible
@@ -213,11 +199,10 @@
 		 * the user may execute the wrong program.
 		 */
 		if (lp + ln + 2 > sizeof(buf)) {
-			(void)_write(STDERR_FILENO, execvPe_err_preamble,
-			    sizeof(execvPe_err_preamble) - 1);
+			(void)_write(STDERR_FILENO, "execvP: ", 8);
 			(void)_write(STDERR_FILENO, p, lp);
-			(void)_write(STDERR_FILENO, execvPe_err_trailer,
-			    sizeof(execvPe_err_trailer) - 1);
+			(void)_write(STDERR_FILENO, ": path too long\n",
+			    16);
 			continue;
 		}
 		bcopy(p, buf, lp);
@@ -289,20 +274,9 @@
 			goto done;
 		}
 	}
-
 	if (eacces)
 		errno = EACCES;
-#ifdef __APPLE__
-	/*
-	 * Preserve errno from execve(2) if it wasn't a PATH search, or
-	 * if it was a PATH search and we bailed out early.  Note that every
-	 * branch in the loop jumps to the `done` label to preserve errno, so
-	 * this is more of a defensive check.
-	 */
-	else if (path != NULL && op == NULL)
-#else
-	else
-#endif
+	else if (cur)
 		errno = ENOENT;
 	/* else use existing errno from _execve */
 done: