tee.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Vector.h>
  27. #include <LibCore/ArgsParser.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #include <stdio.h>
  32. #include <unistd.h>
  33. static Vector<int> collect_fds(Vector<const char*> paths, bool append, bool* err)
  34. {
  35. int oflag;
  36. mode_t mode;
  37. if (append) {
  38. oflag = O_APPEND;
  39. mode = 0;
  40. } else {
  41. oflag = O_CREAT | O_WRONLY | O_TRUNC;
  42. mode = S_IROTH | S_IWOTH | S_IRGRP | S_IWGRP | S_IRUSR | S_IWUSR;
  43. }
  44. Vector<int> fds;
  45. for (const char* path : paths) {
  46. int fd = open(path, oflag, mode);
  47. if (fd < 0) {
  48. perror("failed to open file for writing");
  49. *err = true;
  50. } else {
  51. fds.append(fd);
  52. }
  53. }
  54. fds.append(STDOUT_FILENO);
  55. return fds;
  56. }
  57. static void copy_stdin(Vector<int>& fds, bool* err)
  58. {
  59. for (;;) {
  60. char buf[4096];
  61. ssize_t nread = read(STDIN_FILENO, buf, sizeof(buf));
  62. if (nread == 0)
  63. break;
  64. if (nread < 0) {
  65. perror("read() error");
  66. *err = true;
  67. // a failure to read from stdin should lead to an early exit
  68. return;
  69. }
  70. Vector<int> broken_fds;
  71. for (size_t i = 0; i < fds.size(); ++i) {
  72. auto fd = fds.at(i);
  73. int twrite = 0;
  74. while (twrite != nread) {
  75. ssize_t nwrite = write(fd, buf + twrite, nread - twrite);
  76. if (nwrite < 0) {
  77. if (errno == EINTR) {
  78. continue;
  79. } else {
  80. perror("write() failed");
  81. *err = true;
  82. broken_fds.append(fd);
  83. // write failures to a successfully opened fd shall
  84. // prevent further writes, but shall not block writes
  85. // to the other open fds
  86. break;
  87. }
  88. } else {
  89. twrite += nwrite;
  90. }
  91. }
  92. }
  93. // remove any fds which we can no longer write to for subsequent copies
  94. for (auto to_remove : broken_fds)
  95. fds.remove_first_matching([&](int fd) { return to_remove == fd; });
  96. }
  97. }
  98. static void close_fds(Vector<int>& fds)
  99. {
  100. for (int fd : fds) {
  101. int closed = close(fd);
  102. if (closed < 0) {
  103. perror("failed to close output file");
  104. }
  105. }
  106. }
  107. static void int_handler(int)
  108. {
  109. // pass
  110. }
  111. int main(int argc, char** argv)
  112. {
  113. bool append = false;
  114. bool ignore_interrupts = false;
  115. Vector<const char*> paths;
  116. Core::ArgsParser args_parser;
  117. args_parser.add_option(append, "Append, don't overwrite", "append", 'a');
  118. args_parser.add_option(ignore_interrupts, "Ignore SIGINT", "ignore-interrupts", 'i');
  119. args_parser.add_positional_argument(paths, "Files to copy stdin to", "file", Core::ArgsParser::Required::No);
  120. args_parser.parse(argc, argv);
  121. if (ignore_interrupts) {
  122. if (signal(SIGINT, int_handler) == SIG_ERR)
  123. perror("failed to install SIGINT handler");
  124. }
  125. bool err_open = false;
  126. bool err_write = false;
  127. auto fds = collect_fds(paths, append, &err_open);
  128. copy_stdin(fds, &err_write);
  129. close_fds(fds);
  130. return (err_open || err_write) ? 1 : 0;
  131. }