tee.c 2.6 KB

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