tee.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 source tree.
  8. */
  9. //config:config TEE
  10. //config: bool "tee (4.2 kb)"
  11. //config: default y
  12. //config: help
  13. //config: tee is used to read from standard input and write
  14. //config: to standard output and files.
  15. //config:
  16. //config:config FEATURE_TEE_USE_BLOCK_IO
  17. //config: bool "Enable block I/O (larger/faster) instead of byte I/O"
  18. //config: default y
  19. //config: depends on TEE
  20. //config: help
  21. //config: Enable this option for a faster tee, at expense of size.
  22. //applet:IF_TEE(APPLET(tee, BB_DIR_USR_BIN, BB_SUID_DROP))
  23. //kbuild:lib-$(CONFIG_TEE) += tee.o
  24. /* BB_AUDIT SUSv3 compliant */
  25. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
  26. //usage:#define tee_trivial_usage
  27. //usage: "[-ai] [FILE]..."
  28. //usage:#define tee_full_usage "\n\n"
  29. //usage: "Copy stdin to each FILE, and also to stdout\n"
  30. //usage: "\n -a Append to the given FILEs, don't overwrite"
  31. //usage: "\n -i Ignore interrupt signals (SIGINT)"
  32. //usage:
  33. //usage:#define tee_example_usage
  34. //usage: "$ echo \"Hello\" | tee /tmp/foo\n"
  35. //usage: "$ cat /tmp/foo\n"
  36. //usage: "Hello\n"
  37. // Bare "tee" with no below options does not install SIGPIPE handler - just dies on it.
  38. // TODO:
  39. // --output-error[=MODE]
  40. // 'warn' diagnose errors writing to any output
  41. // 'warn-nopipe' diagnose errors writing to any output not a pipe
  42. // 'exit' exit on error writing to any output
  43. // 'exit-nopipe' exit on error writing to any output not a pipe
  44. // ^^^ all of these should set SIGPIPE to SIG_IGN.
  45. // Because "exit" mode should print error message and exit1(1) - not die on SIGPIPE.
  46. // "exit-nopipe" does not exit on EPIPE and does not set exitcode to 1 too.
  47. // -p diagnose errors writing to non pipes
  48. // ^^^^ this should set SIGPIPE to SIG_IGN. EPIPE is ignored (same as "warn-nopipe")
  49. #include "libbb.h"
  50. #include "common_bufsiz.h"
  51. int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  52. int tee_main(int argc, char **argv)
  53. {
  54. const char *mode = "w\0a";
  55. FILE **files;
  56. FILE **fp;
  57. char **names;
  58. char **np;
  59. char retval;
  60. //TODO: make unconditional
  61. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  62. ssize_t c;
  63. # define buf bb_common_bufsiz1
  64. setup_common_bufsiz();
  65. #else
  66. int c;
  67. #endif
  68. retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
  69. argc -= optind;
  70. argv += optind;
  71. mode += (retval & 2); /* Since 'a' is the 2nd option... */
  72. if (retval & 1) {
  73. signal(SIGINT, SIG_IGN);
  74. }
  75. retval = EXIT_SUCCESS;
  76. /* if (opt_p || opt_output_error)
  77. signal(SIGPIPE, SIG_IGN);
  78. */
  79. /* Allocate an array of FILE *'s, with one extra for a sentinel. */
  80. fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
  81. np = names = argv - 1;
  82. files[0] = stdout;
  83. goto GOT_NEW_FILE;
  84. do {
  85. *fp = stdout;
  86. if (NOT_LONE_DASH(*argv)) {
  87. *fp = fopen_or_warn(*argv, mode);
  88. if (*fp == NULL) {
  89. retval = EXIT_FAILURE;
  90. argv++;
  91. continue;
  92. }
  93. }
  94. *np = *argv++;
  95. GOT_NEW_FILE:
  96. setbuf(*fp, NULL); /* tee must not buffer output. */
  97. fp++;
  98. np++;
  99. } while (*argv);
  100. /* names[0] will be filled later */
  101. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  102. while ((c = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE)) > 0) {
  103. fp = files;
  104. do
  105. fwrite(buf, 1, c, *fp);
  106. /* if (opt_p && fwrite() != c && !EPIPE) bb_error_msg("..."); */
  107. while (*++fp);
  108. }
  109. if (c < 0) { /* Make sure read errors are signaled. */
  110. retval = EXIT_FAILURE;
  111. }
  112. #else
  113. setvbuf(stdout, NULL, _IONBF, 0);
  114. while ((c = getchar()) != EOF) {
  115. fp = files;
  116. do
  117. putc(c, *fp);
  118. /* if (opt_p && putc() == EOF && !EPIPE) bb_error_msg("..."); */
  119. while (*++fp);
  120. }
  121. #endif
  122. /* Now we need to check for i/o errors on stdin and the various
  123. * output files. Since we know that the first entry in the output
  124. * file table is stdout, we can save one "if ferror" test by
  125. * setting the first entry to stdin and checking stdout error
  126. * status with fflush_stdout_and_exit()... although fflush()ing
  127. * is unnecessary here. */
  128. np = names;
  129. fp = files;
  130. names[0] = (char *) bb_msg_standard_input;
  131. files[0] = stdin;
  132. do { /* Now check for input and output errors. */
  133. /* Checking ferror should be sufficient, but we may want to fclose.
  134. * If we do, remember not to close stdin! */
  135. die_if_ferror(*fp++, *np++);
  136. } while (*fp);
  137. fflush_stdout_and_exit(retval);
  138. }