File:  [MW Coherent from dump] / coherent / e / bin / xargs.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Wed May 29 04:56:41 2019 UTC (7 years, 2 months ago) by root
Branches: MarkWilliams, MAIN
CVS tags: relic, HEAD
coherent

/* xargs -- quickie version of System V xargs(1): read a list of
 *	arguments from stdin, apply to the command which is
 *	the argument(s) of xargs
 */

#include <stdio.h>

char *cmdname;		/* should point to argv[0] */

char command[BUFSIZ];	/* command given to xargs */
char line[BUFSIZ];	/* current input line */	
char cmdbuf[BUFSIZ];	/* command + input lines */

main(argc, argv)
	int argc;
	char *argv[];
{
	char *gets();
	char *strcat(), *strcpy();

	cmdname = argv[0];

	/* skip (xargs) command name */

	argv++, argc--;

	/* construct command from arguments */

	strcpy(command, "exec");
	while (argc--) {
		(void) strcat(command, " ");
		(void) strcat(command, *argv++);
	}

	/* initialize for command execution loop */

	(void) strcpy(cmdbuf, command);

	/* here's where all the action is: read in arguments
	 * from stdin, appending to the current command buffer
	 * if next line would overflow command buffer, execute
	 * command buffer and reinitialize
	 */

	while (gets(line) != NULL) {

		/* the "+2" is for the blank and trailing null char */

		if (strlen(cmdbuf)+strlen(line)+2 > BUFSIZ) {
			docmd(cmdbuf);
			(void) strcpy(cmdbuf, command);
		}
		(void) strcat(cmdbuf, " ");
		(void) strcat(cmdbuf, line);
	}

	/* see if there is any left to do */

	if (strlen(cmdbuf) != strlen(command)) {
		docmd(cmdbuf);
	}
}

docmd(cmdbuf)
char *cmdbuf;
{
	return system(cmdbuf);
}



unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.