3
0

xargs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Mini xargs implementation for busybox
  3. *
  4. * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
  5. * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
  6. * Remixed by Mark Whitley <markw@codepoet.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "busybox.h"
  27. int xargs_main(int argc, char **argv)
  28. {
  29. char *cmd_to_be_executed;
  30. char *file_to_act_on;
  31. int i;
  32. int len = 0;
  33. /*
  34. * No options are supported in this version of xargs; no getopt.
  35. *
  36. * Re: The missing -t flag: Most programs that produce output also print
  37. * the filename, so xargs doesn't really need to do it again. Supporting
  38. * the -t flag =greatly= bloats up the size of this app and the memory it
  39. * uses because you have to buffer all the input file strings in memory. If
  40. * you really want to see the filenames that xargs will act on, just run it
  41. * once with no args and xargs will echo the filename. Simple.
  42. */
  43. /* Store the command to be executed (taken from the command line) */
  44. if (argc == 1) {
  45. /* default behavior is to echo all the filenames */
  46. argv[0] = "/bin/echo";
  47. len++; /* space for trailing ' ' */
  48. len++; /* space for trailing '\0' */
  49. } else {
  50. argv++;
  51. len = argc; /* arg = count for ' ' + trailing '\0' */
  52. argc--;
  53. }
  54. /* concatenate all the arguments passed to xargs together */
  55. for (i = 0; i < argc; i++)
  56. len += strlen(argv[i]);
  57. cmd_to_be_executed = xmalloc (len);
  58. for (i = len = 0; i < argc; i++) {
  59. len += sprintf(cmd_to_be_executed + len, "%s ", argv[i]);
  60. }
  61. /* Now, read in one line at a time from stdin, and store this
  62. * line to be used later as an argument to the command */
  63. while ((file_to_act_on = get_line_from_file(stdin)) !=NULL) {
  64. FILE *cmd_output;
  65. char *output_line;
  66. char *execstr;
  67. /* eat the newline off the filename. */
  68. chomp(file_to_act_on);
  69. /* eat blank lines */
  70. if (file_to_act_on[0] == 0)
  71. continue;
  72. /* assemble the command and execute it */
  73. execstr = xcalloc(strlen(cmd_to_be_executed) +
  74. strlen(file_to_act_on) + 1, sizeof(char));
  75. strcat(execstr, cmd_to_be_executed);
  76. strcat(execstr, file_to_act_on);
  77. cmd_output = popen(execstr, "r");
  78. if (cmd_output == NULL)
  79. perror_msg_and_die("popen");
  80. /* harvest the output */
  81. while ((output_line = get_line_from_file(cmd_output)) != NULL) {
  82. fputs(output_line, stdout);
  83. free(output_line);
  84. }
  85. /* clean up */
  86. pclose(cmd_output);
  87. free(execstr);
  88. free(file_to_act_on);
  89. }
  90. #ifdef BB_FEATURE_CLEAN_UP
  91. free(cmd_to_be_executed);
  92. #endif
  93. return 0;
  94. }
  95. /* vi: set sw=4 ts=4: */