tee.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* BB_AUDIT SUSv3 compliant */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
  11. //usage:#define tee_trivial_usage
  12. //usage: "[-ai] [FILE]..."
  13. //usage:#define tee_full_usage "\n\n"
  14. //usage: "Copy stdin to each FILE, and also to stdout\n"
  15. //usage: "\n -a Append to the given FILEs, don't overwrite"
  16. //usage: "\n -i Ignore interrupt signals (SIGINT)"
  17. //usage:
  18. //usage:#define tee_example_usage
  19. //usage: "$ echo \"Hello\" | tee /tmp/foo\n"
  20. //usage: "$ cat /tmp/foo\n"
  21. //usage: "Hello\n"
  22. #include "libbb.h"
  23. #include "common_bufsiz.h"
  24. int tee_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  25. int tee_main(int argc, char **argv)
  26. {
  27. const char *mode = "w\0a";
  28. FILE **files;
  29. FILE **fp;
  30. char **names;
  31. char **np;
  32. char retval;
  33. //TODO: make unconditional
  34. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  35. ssize_t c;
  36. # define buf bb_common_bufsiz1
  37. setup_common_bufsiz();
  38. #else
  39. int c;
  40. #endif
  41. retval = getopt32(argv, "ia"); /* 'a' must be 2nd */
  42. argc -= optind;
  43. argv += optind;
  44. mode += (retval & 2); /* Since 'a' is the 2nd option... */
  45. if (retval & 1) {
  46. signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction. (why?) */
  47. }
  48. retval = EXIT_SUCCESS;
  49. /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
  50. * that doesn't consume all its input. Good idea... */
  51. signal(SIGPIPE, SIG_IGN);
  52. /* Allocate an array of FILE *'s, with one extra for a sentinel. */
  53. fp = files = xzalloc(sizeof(FILE *) * (argc + 2));
  54. np = names = argv - 1;
  55. files[0] = stdout;
  56. goto GOT_NEW_FILE;
  57. do {
  58. *fp = stdout;
  59. if (NOT_LONE_DASH(*argv)) {
  60. *fp = fopen_or_warn(*argv, mode);
  61. if (*fp == NULL) {
  62. retval = EXIT_FAILURE;
  63. argv++;
  64. continue;
  65. }
  66. }
  67. *np = *argv++;
  68. GOT_NEW_FILE:
  69. setbuf(*fp, NULL); /* tee must not buffer output. */
  70. fp++;
  71. np++;
  72. } while (*argv);
  73. /* names[0] will be filled later */
  74. #if ENABLE_FEATURE_TEE_USE_BLOCK_IO
  75. while ((c = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE)) > 0) {
  76. fp = files;
  77. do
  78. fwrite(buf, 1, c, *fp);
  79. while (*++fp);
  80. }
  81. if (c < 0) { /* Make sure read errors are signaled. */
  82. retval = EXIT_FAILURE;
  83. }
  84. #else
  85. setvbuf(stdout, NULL, _IONBF, 0);
  86. while ((c = getchar()) != EOF) {
  87. fp = files;
  88. do
  89. putc(c, *fp);
  90. while (*++fp);
  91. }
  92. #endif
  93. /* Now we need to check for i/o errors on stdin and the various
  94. * output files. Since we know that the first entry in the output
  95. * file table is stdout, we can save one "if ferror" test by
  96. * setting the first entry to stdin and checking stdout error
  97. * status with fflush_stdout_and_exit()... although fflush()ing
  98. * is unnecessary here. */
  99. np = names;
  100. fp = files;
  101. names[0] = (char *) bb_msg_standard_input;
  102. files[0] = stdin;
  103. do { /* Now check for input and output errors. */
  104. /* Checking ferror should be sufficient, but we may want to fclose.
  105. * If we do, remember not to close stdin! */
  106. die_if_ferror(*fp++, *np++);
  107. } while (*fp);
  108. fflush_stdout_and_exit(retval);
  109. }