home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume3 / xargs < prev    next >
Text File  |  1986-11-30  |  4KB  |  153 lines

  1. Subject: xargs - execute a command with many arguments
  2. Newsgroups: mod.sources
  3. Approved: jpn@panda.UUCP
  4.  
  5. Mod.sources:  Volume 3, Issue 106
  6. Submitted by: seismo!amdahl!gam (Gordon A. Moffett)
  7.  
  8. Here is a reimplementation of the System V utility xargs.  I haven't
  9. heard any complaints about it, though [1] There is room for improvement
  10. regarding the command buffer size (tho' it is better than the System V
  11. area in that particular regard) [2] It does not have all the features
  12. of the System V version (as the man page points out).
  13.  
  14.                                Gordon A. Moffett
  15.                                {ihnp4,seismo,hplabs}!amdahl!gam
  16.  
  17. --------------------------------------------------------------------
  18.  
  19. # This is a shell archive.  Remove anything before this line,
  20. # then unpack it by saving it in a file and typing "sh file".
  21. #
  22. # Wrapped by gam on Wed Feb  5 13:15:01 PST 1986
  23. # Contents:  xargs.1 xargs.c
  24.  
  25. echo x - xargs.1
  26. sed 's/^@//' > "xargs.1" <<'@//E*O*F xargs.1//'
  27. @.TH XARGS 1
  28. @.SH NAME
  29. xargs \- execute a command with many arguments
  30. @.SH SYNOPSIS
  31. @.B xargs
  32. command ...
  33. @.SH DESCRIPTION
  34. @.I Xargs
  35. is based on the System V command of the same name.  This
  36. version performs the default function of
  37. @.IR xargs ,
  38. which is also the simplest function.
  39. @.PP
  40. Because Unix imposes a limit on how much space the arguments
  41. of a command may use, that if the argument list were too
  42. large the system prohibits the
  43. command from being executed (in System V, this error is
  44. @.BR E2BIG ,
  45. @.B errno
  46. = 7).
  47. @.PP
  48. To avoid this problem,
  49. @.I xargs
  50. will take arguments which are themselves a command and
  51. @.I its
  52. arguments (options), and then read standard input and
  53. apply it to the command with its given arguments.
  54. @.I Xargs
  55. is careful not to excede the system-imposed limit, which
  56. is expected to be greater than the system's stream file
  57. buffer size,
  58. @.B BUFSIZ
  59. in
  60. @.IR /usr/include/stdio.h .
  61. (The true upper limit can be found in the manual page
  62. @.IR intro(2),
  63. where the
  64. @.B E2BIG
  65. error is described).
  66. It continues to execute the command with the read-in arguments
  67. until it reaches end-of-file.
  68. @.SH "SEE ALSO"
  69. intro(2), exec(2)
  70. @.SH BUGS
  71. Not really the System V Release 2 version at all, but just
  72. looks like it.
  73. @.SH NOTES
  74. @//E*O*F xargs.1//
  75. chmod u=rw,g=r,o=r xargs.1
  76.  
  77. echo x - xargs.c
  78. sed 's/^@//' > "xargs.c" <<'@//E*O*F xargs.c//'
  79. /* xargs -- quickie version of System V xargs(1): read a list of
  80.  *    arguments from stdin, apply to the command which is
  81.  *    the argument(s) of xargs
  82.  */
  83.  
  84. #include <stdio.h>
  85.  
  86. char *cmdname;        /* should point to argv[0] */
  87.  
  88. char command[BUFSIZ];    /* command given to xargs */
  89. char line[BUFSIZ];    /* current input line */    
  90. char cmdbuf[BUFSIZ];    /* command + input lines */
  91.  
  92. main(argc, argv)
  93.     int argc;
  94.     char *argv[];
  95. {
  96.     char *gets();
  97.     char *strcat(), *strcpy();
  98.  
  99.     cmdname = argv[0];
  100.  
  101.     /* skip (xargs) command name */
  102.  
  103.     argv++, argc--;
  104.  
  105.     /* construct command from arguments */
  106.  
  107.     strcpy(command, "exec");
  108.     while (argc--) {
  109.         (void) strcat(command, " ");
  110.         (void) strcat(command, *argv++);
  111.     }
  112.  
  113.     /* initialize for command execution loop */
  114.  
  115.     (void) strcpy(cmdbuf, command);
  116.  
  117.     /* here's where all the action is: read in arguments
  118.      * from stdin, appending to the current command buffer
  119.      * if next line would overflow command buffer, execute
  120.      * command buffer and reinitialize
  121.      */
  122.  
  123.     while (gets(line) != NULL) {
  124.  
  125.         /* the "+2" is for the blank and trailing null char */
  126.  
  127.         if (strlen(cmdbuf)+strlen(line)+2 > BUFSIZ) {
  128.             docmd(cmdbuf);
  129.             (void) strcpy(cmdbuf, command);
  130.         }
  131.         (void) strcat(cmdbuf, " ");
  132.         (void) strcat(cmdbuf, line);
  133.     }
  134.  
  135.     /* see if there is any left to do */
  136.  
  137.     if (strlen(cmdbuf) != strlen(command)) {
  138.         docmd(cmdbuf);
  139.     }
  140. }
  141.  
  142. docmd(cmdbuf)
  143. char *cmdbuf;
  144. {
  145.     return system(cmdbuf);
  146. }
  147. @//E*O*F xargs.c//
  148. chmod u=rw,g=r,o=r xargs.c
  149.  
  150. exit 0
  151.  
  152.  
  153.