3
0

tee.c 2.6 KB

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