3
0

tee.c 2.5 KB

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