tee.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "libbb.h"
  38. #include "common_bufsiz.h"
  39. int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int tee_main(int argc, char **argv)
  41. {
  42. const char *mode = "w\0a";
  43. FILE **files;
  44. FILE **fp;
  45. char **names;
  46. char **np;
  47. char retval;
  48. //TODO: make unconditional
  49. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  50. ssize_t c;
  51. # define buf bb_common_bufsiz1
  52. setup_common_bufsiz();
  53. #else
  54. int c;
  55. #endif
  56. retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
  57. argc -= optind;
  58. argv += optind;
  59. mode += (retval & 2); /* Since 'a' is the 2nd option... */
  60. if (retval & 1) {
  61. signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. (why?) */
  62. }
  63. retval = EXIT_SUCCESS;
  64. /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
  65. * that doesn't consume all its input. Good idea... */
  66. signal(SIGPIPE, SIG_IGN);
  67. /* Allocate an array of FILE *'s, with one extra for a sentinel. */
  68. fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
  69. np = names = argv - 1;
  70. files[0] = stdout;
  71. goto GOT_NEW_FILE;
  72. do {
  73. *fp = stdout;
  74. if (NOT_LONE_DASH(*argv)) {
  75. *fp = fopen_or_warn(*argv, mode);
  76. if (*fp == NULL) {
  77. retval = EXIT_FAILURE;
  78. argv++;
  79. continue;
  80. }
  81. }
  82. *np = *argv++;
  83. GOT_NEW_FILE:
  84. setbuf(*fp, NULL); /* tee must not buffer output. */
  85. fp++;
  86. np++;
  87. } while (*argv);
  88. /* names[0] will be filled later */
  89. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  90. while ((c = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE)) > 0) {
  91. fp = files;
  92. do
  93. fwrite(buf, 1, c, *fp);
  94. while (*++fp);
  95. }
  96. if (c < 0) { /* Make sure read errors are signaled. */
  97. retval = EXIT_FAILURE;
  98. }
  99. #else
  100. setvbuf(stdout, NULL, _IONBF, 0);
  101. while ((c = getchar()) != EOF) {
  102. fp = files;
  103. do
  104. putc(c, *fp);
  105. while (*++fp);
  106. }
  107. #endif
  108. /* Now we need to check for i/o errors on stdin and the various
  109. * output files. Since we know that the first entry in the output
  110. * file table is stdout, we can save one "if ferror" test by
  111. * setting the first entry to stdin and checking stdout error
  112. * status with fflush_stdout_and_exit()... although fflush()ing
  113. * is unnecessary here. */
  114. np = names;
  115. fp = files;
  116. names[0] = (char *) bb_msg_standard_input;
  117. files[0] = stdin;
  118. do { /* Now check for input and output errors. */
  119. /* Checking ferror should be sufficient, but we may want to fclose.
  120. * If we do, remember not to close stdin! */
  121. die_if_ferror(*fp++, *np++);
  122. } while (*fp);
  123. fflush_stdout_and_exit(retval);
  124. }