tail.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Assertions.h>
  27. #include <AK/ByteBuffer.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <LibCore/File.h>
  30. #include <errno.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #define DEFAULT_LINE_COUNT 10
  36. static int tail_from_pos(Core::File& file, off_t startline, bool want_follow)
  37. {
  38. if (!file.seek(startline + 1))
  39. return 1;
  40. while (true) {
  41. const auto& b = file.read(4096);
  42. if (b.is_empty()) {
  43. if (!want_follow) {
  44. break;
  45. } else {
  46. while (!file.can_read()) {
  47. // FIXME: would be nice to have access to can_read_from_fd with an infinite timeout
  48. usleep(100);
  49. }
  50. continue;
  51. }
  52. }
  53. if (write(STDOUT_FILENO, b.data(), b.size()) < 0)
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. static off_t find_seek_pos(Core::File& file, int wanted_lines)
  59. {
  60. // Rather than reading the whole file, start at the end and work backwards,
  61. // stopping when we've found the number of lines we want.
  62. off_t pos = 0;
  63. if (!file.seek(0, Core::IODevice::SeekMode::FromEndPosition, &pos)) {
  64. fprintf(stderr, "Failed to find end of file: %s\n", file.error_string());
  65. return 1;
  66. }
  67. off_t end = pos;
  68. int lines = 0;
  69. // FIXME: Reading char-by-char is only OK if IODevice's read buffer
  70. // is smart enough to not read char-by-char. Fix it there, or fix it here :)
  71. for (; pos >= 0; pos--) {
  72. file.seek(pos);
  73. const auto& ch = file.read(1);
  74. if (ch.is_empty()) {
  75. // Presumably the file got truncated?
  76. // Keep trying to read backwards...
  77. } else {
  78. if (*ch.data() == '\n' && (end - pos) > 1) {
  79. lines++;
  80. if (lines == wanted_lines)
  81. break;
  82. }
  83. }
  84. }
  85. return pos;
  86. }
  87. int main(int argc, char* argv[])
  88. {
  89. if (pledge("stdio rpath", nullptr) < 0) {
  90. perror("pledge");
  91. return 1;
  92. }
  93. bool follow = false;
  94. int line_count = DEFAULT_LINE_COUNT;
  95. const char* file = nullptr;
  96. Core::ArgsParser args_parser;
  97. args_parser.add_option(follow, "Output data as it is written to the file", "follow", 'f');
  98. args_parser.add_option(line_count, "Fetch the specified number of lines", "lines", 'n', "number");
  99. args_parser.add_positional_argument(file, "File path", "file");
  100. args_parser.parse(argc, argv);
  101. auto f = Core::File::construct(file);
  102. if (!f->open(Core::IODevice::ReadOnly)) {
  103. fprintf(stderr, "Error opening file %s: %s\n", file, strerror(errno));
  104. exit(1);
  105. }
  106. if (pledge("stdio", nullptr) < 0) {
  107. perror("pledge");
  108. return 1;
  109. }
  110. auto pos = find_seek_pos(*f, line_count);
  111. return tail_from_pos(*f, pos, follow);
  112. }