3
0

tee.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * tee implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
  11. #include "libbb.h"
  12. #include <signal.h>
  13. int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  14. int tee_main(int argc, char **argv)
  15. {
  16. const char *mode = "w\0a";
  17. FILE **files;
  18. FILE **fp;
  19. char **names;
  20. char **np;
  21. char retval;
  22. //TODO: make unconditional
  23. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  24. ssize_t c;
  25. # define buf bb_common_bufsiz1
  26. #else
  27. int c;
  28. #endif
  29. retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
  30. argc -= optind;
  31. argv += optind;
  32. mode += (retval & 2); /* Since 'a' is the 2nd option... */
  33. if (retval & 1) {
  34. signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. (why?) */
  35. }
  36. retval = EXIT_SUCCESS;
  37. /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
  38. * that doesn't consume all its input. Good idea... */
  39. signal(SIGPIPE, SIG_IGN);
  40. /* Allocate an array of FILE *'s, with one extra for a sentinal. */
  41. fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
  42. np = names = argv - 1;
  43. files[0] = stdout;
  44. goto GOT_NEW_FILE;
  45. do {
  46. *fp = stdout;
  47. if (NOT_LONE_DASH(*argv)) {
  48. *fp = fopen_or_warn(*argv, mode);
  49. if (*fp == NULL) {
  50. retval = EXIT_FAILURE;
  51. argv++;
  52. continue;
  53. }
  54. }
  55. *np = *argv++;
  56. GOT_NEW_FILE:
  57. setbuf(*fp, NULL); /* tee must not buffer output. */
  58. fp++;
  59. np++;
  60. } while (*argv);
  61. /* names[0] will be filled later */
  62. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  63. while ((c = safe_read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
  64. fp = files;
  65. do
  66. fwrite(buf, 1, c, *fp++);
  67. while (*fp);
  68. }
  69. if (c < 0) { /* Make sure read errors are signaled. */
  70. retval = EXIT_FAILURE;
  71. }
  72. #else
  73. setvbuf(stdout, NULL, _IONBF, 0);
  74. while ((c = getchar()) != EOF) {
  75. fp = files;
  76. do
  77. putc(c, *fp++);
  78. while (*fp);
  79. }
  80. #endif
  81. /* Now we need to check for i/o errors on stdin and the various
  82. * output files. Since we know that the first entry in the output
  83. * file table is stdout, we can save one "if ferror" test by
  84. * setting the first entry to stdin and checking stdout error
  85. * status with fflush_stdout_and_exit()... although fflush()ing
  86. * is unnecessary here. */
  87. np = names;
  88. fp = files;
  89. names[0] = (char *) bb_msg_standard_input;
  90. files[0] = stdin;
  91. do { /* Now check for input and output errors. */
  92. /* Checking ferror should be sufficient, but we may want to fclose.
  93. * If we do, remember not to close stdin! */
  94. die_if_ferror(*fp++, *np++);
  95. } while (*fp);
  96. fflush_stdout_and_exit(retval);
  97. }