tee.c 3.0 KB

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