--- 1.1.1.1	2004/04/09 18:31:31
+++ 1.1.1.2	2004/04/09 18:31:50
@@ -52,21 +52,32 @@
 #include <string.h>
 #include <unistd.h>
 #include <limits.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#if __minix
+#define _PATH_ECHO	"/bin/echo"
+typedef unsigned u_int;
+#else
 #include "pathnames.h"
+#endif
+
+#ifndef ARG_MAX
+#define ARG_MAX		(sizeof(int) == 2 ? 4096 : 128 * 1024)
+#endif
 
-int fflag, tflag;
-void err __P((const char *, ...));
-void run(), usage();
-
-main(argc, argv)
-	int argc;
-	char **argv;
+int exit_status = 0;
+int tflag;
+void err(const char *, ...);
+void run(char **argv);
+void usage(void);
+
+int main(int argc, char **argv)
 {
 	extern int optind;
 	extern char *optarg;
 	register int ch;
 	register char *p, *bbp, *ebp, **bxp, **exp, **xp;
-	int cnt, indouble, insingle, nargs, nflag, nline, xflag;
+	int cnt, indouble, insingle, nargs, nflag, nline, xflag, zflag;
 	char **av, *argp;
 
 	/*
@@ -82,14 +93,17 @@
 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
 	 * probably not worthwhile.
 	 */
 	nargs = 5000;
 	nline = ARG_MAX - 4 * 1024;
 		switch(ch) {
-		case 'f':
-			fflag = 1;
-			break;
 		case 'n':
 			nflag = 1;
 			if ((nargs = atoi(optarg)) <= 0)
@@ -250,14 +328,13 @@
 	/* NOTREACHED */
 }
 
-void
-run(argv)
-	char **argv;
+void run(char **argv)
 {
 	register char **p;
 	pid_t pid;
-	volatile int noinvoke;
+	int noinvoke;
 	int status;
+	int pfd[2];
 
 	if (tflag) {
 		(void)fprintf(stderr, "%s", *argv);
@@ -266,61 +343,79 @@
 		(void)fprintf(stderr, "\n");
 		(void)fflush(stderr);
 	}
-	noinvoke = 0;
-	switch(pid = vfork()) {
+	if (pipe(pfd) < 0) err("pipe: %s", strerror(errno));
+
+	switch(pid = fork()) {
 	case -1:
-		err("vfork: %s", strerror(errno));
+		err("fork: %s", strerror(errno));
 	case 0:
+		close(pfd[0]);
+		fcntl(pfd[1], F_SETFD, fcntl(pfd[1], F_GETFD) | FD_CLOEXEC);
+
 		execvp(argv[0], argv);
+		noinvoke = (errno == ENOENT) ? 127 : 126;
 		(void)fprintf(stderr,
 		    "xargs: %s: %s.\n", argv[0], strerror(errno));
-		noinvoke = 1;
-		_exit(1);
+
+		/* Modern way of returning noinvoke instead of a dirty vfork()
+		 * trick:					(kjb)
+		 */
+		write(pfd[1], &noinvoke, sizeof(noinvoke));
+		_exit(-1);
 	}
+	close(pfd[1]);
+	if (read(pfd[0], &noinvoke, sizeof(noinvoke)) < sizeof(noinvoke))
+		noinvoke = 0;
+	close(pfd[0]);
+
 	pid = waitpid(pid, &status, 0);
 	if (pid == -1)
 		err("waitpid: %s", strerror(errno));
+
 	/*
 	 * If we couldn't invoke the utility or the utility didn't exit
-	 * properly, quit with 127.
-	 * Otherwise, if not specified otherwise, and the utility exits
-	 * non-zero, exit with that value.
+	 * properly, quit with 127 or 126 respectively.
+	 */
+	if (noinvoke)
+		exit(noinvoke);
+
+	/*
+	 * According to POSIX, we have to exit if the utility exits with
+	 * a 255 status, or is interrupted by a signal.   xargs is allowed
+	 * to return any exit status between 1 and 125 in these cases, but
+	 * we'll use 124 and 125, the same values used by GNU xargs.
 	 */
-	if (noinvoke || !WIFEXITED(status) || WIFSIGNALED(status))
-		exit(127);
-	if (!fflag && WEXITSTATUS(status))
-		exit(WEXITSTATUS(status));
+	if (WIFEXITED(status)) {
+		if (WEXITSTATUS (status) == 255) {
+			fprintf (stderr, "xargs: %s exited with status 255\n",
+				 argv[0]);
+			exit(124);
+		} else if (WEXITSTATUS (status) != 0) {
+			exit_status = 123;
+		}
+	} else if (WIFSTOPPED (status)) {
+		fprintf (stderr, "xargs: %s terminated by signal %d\n",
+			 argv[0], WSTOPSIG (status));
+		exit(125);
+	} else if (WIFSIGNALED (status)) {
+		fprintf (stderr, "xargs: %s terminated by signal %d\n",
+			 argv[0], WTERMSIG (status));
+		exit(125);
+	}
 }
 
-#if __STDC__
-#include <stdarg.h>
-#else
-#include <varargs.h>
-#endif
-
-void
-#if __STDC__
-err(const char *fmt, ...)
-#else
-err(fmt, va_alist)
-	char *fmt;
-        va_dcl
-#endif
+void err(const char *fmt, ...)
 {
 	va_list ap;
-#if __STDC__
+
 	va_start(ap, fmt);
-#else
-	va_start(ap);
-#endif
 	(void)fprintf(stderr, "xargs: ");
 	(void)vfprintf(stderr, fmt, ap);
 	va_end(ap);
