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