Created
May 22, 2017 12:07
-
-
Save myfreeer/01fb0d7476c16d234032e456abc7d1c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* xargs - Make and execute commands Author: Ian Nicholls: 1 Mar 90 */ | |
/* | |
* xargs - Accept words from stdin until, combined with the arguments | |
* given on the command line, just fit into the command line limit. | |
* Then, execute the result. | |
* e.g. ls | xargs compress | |
* find . -name '*.s' -print | xargs ar qv libc.a | |
* | |
* flags: not any | |
* | |
* Exits with: 0 No errors. | |
* 1 If any system(3) call returns a nonzero status. | |
* 2 Usage error | |
* 3 Line length too short to contain some single argument. | |
* | |
* | |
* Bugs: If the command contains unquoted wildflags, then the system(3) call | |
* call may expand this to larger than the maximum line size. | |
* The command is not executed if nothing was read from stdin. | |
* xargs may give up too easily when the command returns nonzero. | |
*/ | |
#include <errno.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <stdio.h> | |
#ifndef MAX_ARGLINE | |
# define MAX_ARGLINE 1023 | |
#endif | |
#ifndef min | |
# define min(a,b) ((a) < (b) ? (a) : (b)) | |
#endif | |
char outlin[MAX_ARGLINE]; | |
char inlin[MAX_ARGLINE]; | |
char startlin[MAX_ARGLINE]; | |
char *ending = NULL; | |
char traceflag = 0; | |
int main( int ac, char *av[]) | |
{ | |
int outlen, inlen, startlen, endlen=0, i; | |
char errflg = 0; | |
int maxlin = MAX_ARGLINE; | |
unsigned char optind = 0; | |
startlin[0] = 0; | |
if (ac<2) { | |
strcat(startlin, "echo "); | |
} | |
else for (unsigned char optind = 1 ; optind < ac; optind++) { | |
strcat(startlin, av[optind]); | |
strcat(startlin, " "); | |
} | |
startlen = strlen(startlin); | |
if (ending) endlen = strlen(ending); | |
maxlin = maxlin - 1 - endlen; /* Pre-compute */ | |
strcpy(outlin, startlin); | |
outlen = startlen; | |
while (gets(inlin) != NULL) { | |
inlen = strlen(inlin); | |
if (maxlin <= (outlen + inlen)) { | |
if (outlen == startlen) { | |
fprintf(stderr, "%s: Line length too short to process '%s'\n", | |
av[0], inlin); | |
exit(3); | |
} | |
if (ending) strcat(outlin, ending); | |
if (traceflag) fputs(outlin,stderr); | |
errno = 0; | |
if (0 != system(outlin)) { | |
if (errno != 0) perror("xargs"); | |
exit(1); | |
} | |
strcpy(outlin, startlin); | |
outlen = startlen; | |
} | |
strcat(outlin, inlin); | |
strcat(outlin, " "); | |
outlen = outlen + inlen + 1; | |
} | |
if (outlen != startlen) { | |
if (ending) strcat(outlin, ending); | |
if (traceflag) fputs(outlin,stderr); | |
errno = 0; | |
if (0 != system(outlin)) { | |
if (errno != 0) perror("xargs"); | |
exit(1); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment