Subject: xargs ported (#249)
Index:	usr.bin/xargs 2.11BSD

Description:
	Long argument lists cause problems especially on small machines
	with a very restrictive (5k) limit on the size of the argument
	list.

Repeat-By:
	In a directory containing several hundred files type:

		ls

	Note that 'ls' gives up with an out of memory error.

	Using xargs the files can be listed with:

	find . -type f -print | xargs ls

Fix:
	Thanks go to Robin Birch (robin@falstaf.demon.co.uk) for porting xargs
	from the 4.4-Lite kit.

	The kit below is a shar archive containing a shar file of the 
	'xargs' program and a patch file to update the usr.bin/Makefile,
	/usr/include/sys/wait.h and /VERSION files.

	Cut where indicated and save to a file (/tmp/foo) and then:

	sh /tmp/foo
	sh /tmp/xargs.shar
	patch -p0 < /tmp/249.patch
	cd /usr/src/usr.bin/xargs
	make
	make install
	make clean
=============================cut here======================
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	/tmp/249.patch
#	/tmp/xargs.shar
# This archive created: Fri Jun 23 20:48:00 1995
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f '/tmp/249.patch'
then
	echo shar: "will not over-write existing file '/tmp/249.patch'"
else
sed 's/^Y//' << \SHAR_EOF > '/tmp/249.patch'
Y*** /usr/include/sys/wait.h.old	Wed Mar 10 20:08:42 1993
Y--- /usr/include/sys/wait.h	Fri Jun 23 20:42:22 1995
Y***************
Y*** 3,9 ****
Y   * All rights reserved.  The Berkeley software License Agreement
Y   * specifies the terms and conditions for redistribution.
Y   *
Y!  *	@(#)wait.h	7.2 (2.11BSD GTE) 3/10/93
Y   */
Y  
Y  /*
Y--- 3,9 ----
Y   * All rights reserved.  The Berkeley software License Agreement
Y   * specifies the terms and conditions for redistribution.
Y   *
Y!  *	@(#)wait.h	7.2.1 (2.11BSD GTE) 1995/06/23
Y   */
Y  
Y  /*
Y***************
Y*** 64,69 ****
Y--- 64,70 ----
Y  #define WIFSTOPPED(x)	((x).w_stopval == WSTOPPED)
Y  #define WIFSIGNALED(x)	((x).w_stopval != WSTOPPED && (x).w_termsig != 0)
Y  #define WIFEXITED(x)	((x).w_stopval != WSTOPPED && (x).w_termsig == 0)
Y+ #define	WEXITSTATUS(x)	((x).w_retcode)
Y  
Y  #define	W_STOPCODE(sig)	((sig << 8) | WSTOPPED)
Y  #define	W_EXITCODE(ret,sig)	((ret << 8) | (sig))
Y*** /usr/src/usr.bin/Makefile.old	Tue May  2 23:50:19 1995
Y--- /usr/src/usr.bin/Makefile	Wed Jun 21 20:38:08 1995
Y***************
Y*** 1,4 ****
Y! #	Makefile	1.1 (2.11BSD GTE) 1995/05/02
Y  #
Y  DESTDIR=
Y  CFLAGS=	-O
Y--- 1,4 ----
Y! #	Makefile	1.2 (2.11BSD GTE) 1995/06/21
Y  #
Y  DESTDIR=
Y  CFLAGS=	-O
Y***************
Y*** 8,14 ****
Y  #
Y  SUBDIR=	at calendar dc diction diff3 eqn f77 find graph learn lex lint \
Y  	m4 neqn plot printf prof ranlib ratfor refer roff spell struct tbl \
Y! 	tip uucp uname yacc
Y  
Y  # Shell scripts that need only be installed and are never removed.
Y  #
Y--- 8,14 ----
Y  #
Y  SUBDIR=	at calendar dc diction diff3 eqn f77 find graph learn lex lint \
Y  	m4 neqn plot printf prof ranlib ratfor refer roff spell struct tbl \
Y! 	tip uucp uname yacc xargs
Y  
Y  # Shell scripts that need only be installed and are never removed.
Y  #
Y*** /VERSION.old	Wed Jun 21 21:15:22 1995
Y--- /VERSION	Wed Jun 21 21:15:04 1995
Y***************
Y*** 1,4 ****
Y! Current Patch Level: 248
Y  
Y  2.11 BSD
Y  ============
Y--- 1,4 ----
Y! Current Patch Level: 249
Y  
Y  2.11 BSD
Y  ============
SHAR_EOF
fi
if test -f '/tmp/xargs.shar'
then
	echo shar: "will not over-write existing file '/tmp/xargs.shar'"
else
sed 's/^Y//' << \SHAR_EOF > '/tmp/xargs.shar'
Y#! /bin/sh
Y# This is a shell archive, meaning:
Y# 1. Remove everything above the #! /bin/sh line.
Y# 2. Save the resulting text in a file.
Y# 3. Execute the file with /bin/sh (not csh) to create:
Y#	/usr/src/usr.bin/xargs
Y# This archive created: Fri Jun 23 20:46:00 1995
Yexport PATH; PATH=/bin:/usr/bin:$PATH
Yif test ! -d '/usr/src/usr.bin/xargs'
Ythen
Y	mkdir '/usr/src/usr.bin/xargs'
Yfi
Ycd '/usr/src/usr.bin/xargs'
Yif test -f 'Makefile'
Ythen
Y	echo shar: "will not over-write existing file 'Makefile'"
Yelse
Ysed 's/^X//' << \SHAR_EOF > 'Makefile'
YX#
YX# Public Domain.  1995/03/13 - Steven Schultz
YX#
YX#	@(#)Makefile	1.0 (2.11BSD GTE) 1995/03/13
YX#
YXCFLAGS=	 -O
YXSEPFLAG= -i
YXSRCS=	xargs.c
YXOBJS=	xargs.o
YX
YXall: xargs xargs.0
YX
YXxargs: ${OBJS}
YX	${CC} ${CFLAGS} ${SEPFLAG} -o $@ ${OBJS}
YX
YXxargs.0: xargs.1
YX	/usr/man/manroff xargs.1 > xargs.0
YX
YXclean:
YX	rm -f ${OBJS} tags xargs xargs.0
YX
YXdepend: ${SRCS}
YX	mkdep ${CFLAGS} ${SRCS}
YX
YXinstall: all
YX	install -s -o bin -g bin -m 755 xargs ${DESTDIR}/usr/bin
YX	install -c -o bin -g bin -m 444 xargs.0 ${DESTDIR}/usr/man/cat1
YX
YXlint: ${SRCS}
YX	lint -hax ${SRCS}
YX
YXtags: ${SRCS}
YX	ctags ${SRCS}
YSHAR_EOF
Yfi
Yif test -f 'pathnames.h'
Ythen
Y	echo shar: "will not over-write existing file 'pathnames.h'"
Yelse
Ysed 's/^X//' << \SHAR_EOF > 'pathnames.h'
YX/*-
YX * Copyright (c) 1990, 1993
YX *	The Regents of the University of California.  All rights reserved.
YX *
YX * Redistribution and use in source and binary forms, with or without
YX * modification, are permitted provided that the following conditions
YX * are met:
YX * 1. Redistributions of source code must retain the above copyright
YX *    notice, this list of conditions and the following disclaimer.
YX * 2. Redistributions in binary form must reproduce the above copyright
YX *    notice, this list of conditions and the following disclaimer in the
YX *    documentation and/or other materials provided with the distribution.
YX * 3. All advertising materials mentioning features or use of this software
YX *    must display the following acknowledgement:
YX *	This product includes software developed by the University of
YX *	California, Berkeley and its contributors.
YX * 4. Neither the name of the University nor the names of its contributors
YX *    may be used to endorse or promote products derived from this software
YX *    without specific prior written permission.
YX *
YX * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
YX * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
YX * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
YX * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
YX * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
YX * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
YX * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
YX * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
YX * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
YX * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
YX * SUCH DAMAGE.
YX *
YX *	@(#)pathnames.h	8.1 (Berkeley) 6/6/93
YX */
YX
YX#define	_PATH_ECHO	"/bin/echo"
YSHAR_EOF
Yfi
Yif test -f 'xargs.1'
Ythen
Y	echo shar: "will not over-write existing file 'xargs.1'"
Yelse
Ysed 's/^X//' << \SHAR_EOF > 'xargs.1'
YX.\" Copyright (c) 1990, 1991, 1993
YX.\"	The Regents of the University of California.  All rights reserved.
YX.\"
YX.\" This code is derived from software contributed to Berkeley by
YX.\" John B. Roll Jr. and the Institute of Electrical and Electronics
YX.\" Engineers, Inc.
YX.\"
YX.\" Redistribution and use in source and binary forms, with or without
YX.\" modification, are permitted provided that the following conditions
YX.\" are met:
YX.\" 1. Redistributions of source code must retain the above copyright
YX.\"    notice, this list of conditions and the following disclaimer.
YX.\" 2. Redistributions in binary form must reproduce the above copyright
YX.\"    notice, this list of conditions and the following disclaimer in the
YX.\"    documentation and/or other materials provided with the distribution.
YX.\" 3. All advertising materials mentioning features or use of this software
YX.\"    must display the following acknowledgement:
YX.\"	This product includes software developed by the University of
YX.\"	California, Berkeley and its contributors.
YX.\" 4. Neither the name of the University nor the names of its contributors
YX.\"    may be used to endorse or promote products derived from this software
YX.\"    without specific prior written permission.
YX.\"
YX.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
YX.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
YX.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
YX.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
YX.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
YX.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
YX.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
YX.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
YX.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
YX.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
YX.\" SUCH DAMAGE.
YX.\"
YX.\"	@(#)xargs.1	8.1 (Berkeley) 6/6/93
YX.\"
YX.TH XARGS 1 "June 6, 1993"
YX.SH NAME
YXxargs \- construct argument list(s) and execute utility
YX.SH SYNOPSIS
YX.B xargs
YX[
YX.B \-t
YX][[
YX.B \-x
YX]
YX.B \-n 
YX.BI number
YX][
YX.B \-s 
YX.BI size
YX][
YX.BI utility 
YX[
YX.BI arguments ...
YX]]
YX.PP
YX.SH DESCRIPTION
YXThe
YX.B xargs
YXutility reads space, tab, newline and end-of-file delimited arguments
YXfrom the standard input and executes the specified
YX.BI utility
YXwith them as
YXarguments.
YX.PP
YXThe utility and any arguments specified on the command line are given
YXto the
YX.BI utility
YXupon each invocation, followed by some number of the arguments read
YXfrom standard input.
YXThe
YX.BI utility
YXis repeatedly executed until standard input is exhausted.
YX.PP
YXSpaces, tabs and newlines may be embedded in arguments using single
YX(``\ '\ '')
YX.EK
YXor double (``"'') quotes or backslashes (``\e'').
YXSingle quotes escape all non-single quote characters, excluding newlines,
YXup to the matching single quote.
YXDouble quotes escape all non-double quote characters, excluding newlines,
YXup to the matching double quote.
YXAny single character, including newlines, may be escaped by a backslash.
YX.PP
YXThe options are as follows:
YX.TP 10n
YX.BR \-n " number"
YXSet the maximum number of arguments taken from standard input for each
YXinvocation of the utility.
YXAn invocation of
YX.BI utility
YXwill use less than
YX.BI number
YXstandard input arguments if the number of bytes accumulated (see the
YX.B s
YXoption) exceeds the specified
YX.BI size
YXor there are fewer than
YX.BI number
YXarguments remaining for the last invocation of
YX.BI utility .
YXThe current default value for
YX.BI number
YXis 5000.
YX.TP
YX.BR \-s " size"
YXSet the maximum number of bytes for the command line length provided to
YX.BI utility .
YXThe sum of the length of the utility name and the arguments passed to
YX.BI utility
YX(including
YX.I /dev/null
YXterminators) will be less than or equal to this number.
YXThe current default value for
YX.BI size
YXis
YXARG_MAX
YX- 2048.
YX.TP
YX.BR \-t
YXEcho the command to be executed to standard error immediately before it
YXis executed.
YX.TP
YX.BR \-x
YXForce
YX.B xargs
YXto terminate immediately if a command line containing
YX.BI number
YXarguments will not fit in the specified (or default) command line length.
YX.PP
YXIf no
YX.BI utility
YXis specified,
YXecho(1)
YXis used.
YX.PP
YXUndefined behavior may occur if
YX.BI utility
YXreads from the standard input.
YX.PP
YXThe
YX.B xargs
YXutility exits immediately (without processing any further input) if a
YXcommand line cannot be assembled,
YX.BI utility
YXcannot be invoked, an invocation of the utility is terminated by a signal
YXor an invocation of the utility exits with a value of 255.
YX.PP
YXThe
YX.B xargs
YXutility exits with a value of 0 if no error occurs.
YXIf
YX.BI utility
YXcannot be invoked,
YX.B xargs
YXexits with a value of 127.
YXIf any other error occurs,
YX.B xargs
YXexits with a value of 1.
YX.SH SEE ALSO
YXecho(1), find(1)
YX.PP
YX.SH STANDARDS
YXThe
YX.B xargs
YXutility is expected to be
YXIEEE Std 1003.2("POSIX") compliant.
YSHAR_EOF
Yfi
Yif test -f 'xargs.c'
Ythen
Y	echo shar: "will not over-write existing file 'xargs.c'"
Yelse
Ysed 's/^X//' << \SHAR_EOF > 'xargs.c'
YX/*-
YX * Copyright (c) 1990, 1993
YX *	The Regents of the University of California.  All rights reserved.
YX *
YX * This code is derived from software contributed to Berkeley by
YX * John B. Roll Jr.
YX *
YX * Redistribution and use in source and binary forms, with or without
YX * modification, are permitted provided that the following conditions
YX * are met:
YX * 1. Redistributions of source code must retain the above copyright
YX *    notice, this list of conditions and the following disclaimer.
YX * 2. Redistributions in binary form must reproduce the above copyright
YX *    notice, this list of conditions and the following disclaimer in the
YX *    documentation and/or other materials provided with the distribution.
YX * 3. All advertising materials mentioning features or use of this software
YX *    must display the following acknowledgement:
YX *	This product includes software developed by the University of
YX *	California, Berkeley and its contributors.
YX * 4. Neither the name of the University nor the names of its contributors
YX *    may be used to endorse or promote products derived from this software
YX *    without specific prior written permission.
YX *
YX * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
YX * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
YX * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
YX * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
YX * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
YX * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
YX * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
YX * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
YX * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
YX * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
YX * SUCH DAMAGE.
YX */
YX
YX#if defined(DOSCCS) && !defined(lint)
YXstatic char copyright[] =
YX"@(#) Copyright (c) 1990, 1993\n\
YX	The Regents of the University of California.  All rights reserved.\n";
YX#endif
YX
YX#if defined(DOSCCS) && !defined(lint)
YXstatic char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
YX#endif
YX
YX#include <sys/param.h>
YX#include <sys/types.h>
YX#include <sys/wait.h>
YX#include <errno.h>
YX#include <stdio.h>
YX#include "pathnames.h"
YX
YXextern int errno;
YXextern char *optarg;
YXextern int optind;
YX
YX/*
YX * This is defined for pdp11s.  ARG_MAX is usually defined in
YX * an include file.  I have left it here until such times as
YX * the system limits stuff in machparam etc is changed to the
YX * same names as 4.4. Note that
YX * this is much smaller than the normal value cos it's only a
YX * small computer (sigh).
YX */
YX
YX#define ARG_MAX NCARGS
YX
YXint tflag, rval;
YX
YXvoid err();
YXvoid run();
YXvoid usage();
YX
YXmain(argc, argv)
YX	int argc;
YX	char **argv;
YX{
YX	register int ch;
YX	register char *p, *bbp, *ebp, **bxp, **exp, **xp;
YX	int cnt, indouble, insingle, nargs, nflag, nline, xflag;
YX	char **av, *argp;
YX
YX	/*
YX	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
YX	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
YX	 * that the smallest argument is 2 bytes in length, this means that
YX	 * the number of arguments is limited to:
YX	 *
YX	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
YX	 *
YX	 * We arbitrarily limit the number of arguments to 5000.  This is
YX	 * allowed by POSIX.2 as long as the resulting minimum exec line is
YX	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
YX	 * probably not worthwhile.
YX	 */
YX	nargs = 5000;
YX	nline = ARG_MAX - 4 * 1024;
YX	nflag = xflag = 0;
YX	while ((ch = getopt(argc, argv, "n:s:tx")) != EOF)
YX		switch(ch) {
YX		case 'n':
YX			nflag = 1;
YX			if ((nargs = atoi(optarg)) <= 0)
YX				err("illegal argument count");
YX			break;
YX		case 's':
YX			nline = atoi(optarg);
YX			break;
YX		case 't':
YX			tflag = 1;
YX			break;
YX		case 'x':
YX			xflag = 1;
YX			break;
YX		case '?':
YX		default:
YX			usage();
YX	}
YX	argc -= optind;
YX	argv += optind;
YX
YX	if (xflag && !nflag)
YX		usage();
YX
YX	/*
YX	 * Allocate pointers for the utility name, the utility arguments,
YX	 * the maximum arguments to be read from stdin and the trailing
YX	 * NULL.
YX	 */
YX	if (!(av = bxp =
YX	    (char **)malloc((u_int)(1 + argc + nargs + 1) * sizeof(char **))))
YX		err("%s", strerror(errno));
YX
YX	/*
YX	 * Use the user's name for the utility as argv[0], just like the
YX	 * shell.  Echo is the default.  Set up pointers for the user's
YX	 * arguments.
YX	 */
YX	if (!*argv)
YX		cnt = strlen(*bxp++ = _PATH_ECHO);
YX	else {
YX		cnt = 0;
YX		do {
YX			cnt += strlen(*bxp++ = *argv) + 1;
YX		} while (*++argv);
YX	}
YX
YX	/*
YX	 * Set up begin/end/traversing pointers into the array.  The -n
YX	 * count doesn't include the trailing NULL pointer, so the malloc
YX	 * added in an extra slot.
YX	 */
YX	exp = (xp = bxp) + nargs;
YX
YX	/*
YX	 * Allocate buffer space for the arguments read from stdin and the
YX	 * trailing NULL.  Buffer space is defined as the default or specified
YX	 * space, minus the length of the utility name and arguments.  Set up
YX	 * begin/end/traversing pointers into the array.  The -s count does
YX	 * include the trailing NULL, so the malloc didn't add in an extra
YX	 * slot.
YX	 */
YX	nline -= cnt;
YX	if (nline <= 0)
YX		err("insufficient space for command");
YX
YX	if (!(bbp = (char *)malloc((u_int)nline + 1)))
YX		err("%s", strerror(errno));
YX	ebp = (argp = p = bbp) + nline - 1;
YX
YX	for (insingle = indouble = 0;;)
YX		switch(ch = getchar()) {
YX		case EOF:
YX			/* No arguments since last exec. */
YX			if (p == bbp)
YX				exit(rval);
YX
YX			/* Nothing since end of last argument. */
YX			if (argp == p) {
YX				*xp = NULL;
YX				run(av);
YX				exit(rval);
YX			}
YX			goto arg1;
YX		case ' ':
YX		case '\t':
YX			/* Quotes escape tabs and spaces. */
YX			if (insingle || indouble)
YX				goto addch;
YX			goto arg2;
YX		case '\n':
YX			/* Empty lines are skipped. */
YX			if (argp == p)
YX				continue;
YX
YX			/* Quotes do not escape newlines. */
YXarg1:			if (insingle || indouble)
YX				 err("unterminated quote");
YX
YXarg2:			*p = '\0';
YX			*xp++ = argp;
YX
YX			/*
YX			 * If max'd out on args or buffer, or reached EOF,
YX			 * run the command.  If xflag and max'd out on buffer
YX			 * but not on args, object.
YX			 */
YX			if (xp == exp || p == ebp || ch == EOF) {
YX				if (xflag && xp != exp && p == ebp)
YX					err("insufficient space for arguments");
YX				*xp = NULL;
YX				run(av);
YX				if (ch == EOF)
YX					exit(rval);
YX				p = bbp;
YX				xp = bxp;
YX			} else
YX				++p;
YX			argp = p;
YX			break;
YX		case '\'':
YX			if (indouble)
YX				goto addch;
YX			insingle = !insingle;
YX			break;
YX		case '"':
YX			if (insingle)
YX				goto addch;
YX			indouble = !indouble;
YX			break;
YX		case '\\':
YX			/* Backslash escapes anything, is escaped by quotes. */
YX			if (!insingle && !indouble && (ch = getchar()) == EOF)
YX				err("backslash at EOF");
YX			/* FALLTHROUGH */
YX		default:
YXaddch:			if (p < ebp) {
YX				*p++ = ch;
YX				break;
YX			}
YX
YX			/* If only one argument, not enough buffer space. */
YX			if (bxp == xp)
YX				err("insufficient space for argument");
YX			/* Didn't hit argument limit, so if xflag object. */
YX			if (xflag)
YX				err("insufficient space for arguments");
YX
YX			*xp = NULL;
YX			run(av);
YX			xp = bxp;
YX			cnt = ebp - argp;
YX			bcopy(argp, bbp, cnt);
YX			p = (argp = bbp) + cnt;
YX			*p++ = ch;
YX			break;
YX		}
YX	/* NOTREACHED */
YX}
YX
YXvoid
YXrun(argv)
YX	char **argv;
YX{
YX	int noinvoke;
YX	register char **p;
YX	pid_t pid;
YX	union wait status;
YX
YX	if (tflag) {
YX		(void)fprintf(stderr, "%s", *argv);
YX		for (p = argv + 1; *p; ++p)
YX			(void)fprintf(stderr, " %s", *p);
YX		(void)fprintf(stderr, "\n");
YX		(void)fflush(stderr);
YX	}
YX	noinvoke = 0;
YX	switch(pid = vfork()) {
YX	case -1:
YX		err("vfork: %s", strerror(errno));
YX	case 0:
YX		execvp(argv[0], argv);
YX		(void)fprintf(stderr,
YX		    "xargs: %s: %s\n", argv[0], strerror(errno));
YX		noinvoke = 1;
YX		_exit(1);
YX	}
YX	pid = waitpid(pid, &status, 0);
YX	if (pid == -1)
YX		err("waitpid: %s", strerror(errno));
YX	/* If we couldn't invoke the utility, exit 127. */
YX	if (noinvoke)
YX		exit(127);
YX	/* If utility signaled or exited with a value of 255, exit 1-125. */
YX	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
YX		exit(1);
YX	if (WEXITSTATUS(status))
YX		rval = 1;
YX}
YX
YXvoid
YXusage()
YX{
YX	(void)fprintf(stderr,
YX"usage: xargs [-t] [-n number [-x]] [-s size] [utility [argument ...]]\n");
YX	exit(1);
YX}
YX
YX#if __STDC__
YX#include <stdarg.h>
YX#else
YX#include <varargs.h>
YX#endif
YX
YXvoid
YX#if __STDC__
YXerr(const char *fmt, ...)
YX#else
YXerr(fmt, va_alist)
YX	char *fmt;
YX        va_dcl
YX#endif
YX{
YX	va_list ap;
YX#if __STDC__
YX	va_start(ap, fmt);
YX#else
YX	va_start(ap);
YX#endif
YX	(void)fprintf(stderr, "xargs: ");
YX	(void)vfprintf(stderr, fmt, ap);
YX	va_end(ap);
YX	(void)fprintf(stderr, "\n");
YX	exit(1);
YX	/* NOTREACHED */
YX}
YSHAR_EOF
Yfi
Ycd ..
Yexit 0
Y#	End of shell archive
SHAR_EOF
fi
exit 0
#	End of shell archive
