Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!apple!sun-barr!cs.utexas.edu!csd4.csd.uwm.edu!gem.mps.ohio-state.edu!rpi!crdgw1!crdgw1.ge.com!barnett From: barnett@crdgw1.crd.ge.com (Bruce Barnett) Newsgroups: comp.unix.wizards Subject: Re: Look! An xargs!! (Re: recursive grep) Message-ID: <2205@crdgw1.crd.ge.com> Date: 8 Sep 89 20:57:46 GMT References: <666@lakart.UUCP> <1641@cbnewsl.ATT.COM> <7774@cbmvax.UUCP> <16816@pasteur.Berkeley.EDU> <1126@virtech.UUCP> <2404@wyse.wyse.com> <10967@smoke.BRL.MIL> <2415@wyse.wyse.com> <10978@smoke.BRL.MIL> Sender: news@crdgw1.crd.ge.com Reply-To: barnett@crdgw1.crd.ge.com (Bruce Barnett) Organization: GE Corp. R & D, Schenectady, NY Lines: 84 In-reply-to: gwyn@smoke.BRL.MIL (Doug Gywn) In article <10978@smoke.BRL.MIL>, gwyn@smoke (Doug Gywn) writes: >Why not simply write a genuine xargs implementation, say in C where you >can do it right without a lot of hassle. Hear! hear! Who really cares if you can *ALMOST* do it in 35 lines when you can do it *RIGHT* in 70? Forgive this posting of the sources in this newsgroup, but maybe it will reduce the noise level. Here is the version of xargs.c from comp.sources.whatever /* 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 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); } -- Bruce G. Barnett uunet!crdgw1!barnett