2019-06-07 09:49:31 +00:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <LibCore/CArgsParser.h>
|
|
|
|
#include <LibCore/CFile.h>
|
|
|
|
#include <errno.h>
|
2019-05-16 13:02:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <string.h>
|
2019-05-16 13:02:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2019-06-06 18:28:58 +00:00
|
|
|
#define DEFAULT_LINE_COUNT 10
|
|
|
|
|
2019-05-16 13:02:17 +00:00
|
|
|
int tail_from_pos(CFile& file, off_t startline, bool want_follow)
|
|
|
|
{
|
|
|
|
if (!file.seek(startline + 1))
|
2019-06-06 08:53:42 +00:00
|
|
|
return 1;
|
2019-05-16 13:02:17 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const auto& b = file.read(4096);
|
|
|
|
if (b.is_empty()) {
|
|
|
|
if (!want_follow) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
while (!file.can_read()) {
|
|
|
|
// FIXME: would be nice to have access to can_read_from_fd with an infinite timeout
|
|
|
|
usleep(100);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-06 08:53:42 +00:00
|
|
|
if (write(STDOUT_FILENO, b.pointer(), b.size()) < 0)
|
|
|
|
return 1;
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t find_seek_pos(CFile& file, int wanted_lines)
|
|
|
|
{
|
|
|
|
// Rather than reading the whole file, start at the end and work backwards,
|
|
|
|
// stopping when we've found the number of lines we want.
|
|
|
|
off_t pos = 0;
|
|
|
|
if (!file.seek(0, CIODevice::SeekMode::FromEndPosition, &pos)) {
|
|
|
|
fprintf(stderr, "Failed to find end of file: %s\n", file.error_string());
|
2019-06-06 08:53:42 +00:00
|
|
|
return 1;
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t end = pos;
|
|
|
|
int lines = 0;
|
|
|
|
|
|
|
|
// FIXME: Reading char-by-char is only OK if CIODevice's read buffer
|
|
|
|
// is smart enough to not read char-by-char. Fix it there, or fix it here :)
|
2019-06-07 09:49:31 +00:00
|
|
|
for (; pos >= 0; pos--) {
|
2019-05-16 13:02:17 +00:00
|
|
|
file.seek(pos);
|
|
|
|
const auto& ch = file.read(1);
|
|
|
|
if (ch.is_empty()) {
|
|
|
|
// Presumably the file got truncated?
|
|
|
|
// Keep trying to read backwards...
|
|
|
|
} else {
|
|
|
|
if (*ch.pointer() == '\n' && (end - pos) > 1) {
|
|
|
|
lines++;
|
|
|
|
if (lines == wanted_lines)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:49:31 +00:00
|
|
|
int main(int argc, char* argv[])
|
2019-05-16 13:02:17 +00:00
|
|
|
{
|
2019-05-17 13:35:30 +00:00
|
|
|
CArgsParser args_parser("tail");
|
2019-05-17 10:59:37 +00:00
|
|
|
|
|
|
|
args_parser.add_arg("f", "follow -- appended data is output as it is written to the file");
|
|
|
|
args_parser.add_arg("n", "lines", "fetch the specified number of lines");
|
2019-05-17 13:17:43 +00:00
|
|
|
args_parser.add_required_single_value("file");
|
2019-05-17 10:59:37 +00:00
|
|
|
|
2019-06-22 13:47:08 +00:00
|
|
|
CArgsParserResult args = args_parser.parse(argc, argv);
|
2019-05-17 10:59:37 +00:00
|
|
|
|
|
|
|
Vector<String> values = args.get_single_values();
|
|
|
|
if (values.size() != 1) {
|
|
|
|
args_parser.print_usage();
|
2019-06-06 08:53:42 +00:00
|
|
|
return 1;
|
2019-05-17 10:59:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 13:02:17 +00:00
|
|
|
int line_count = 0;
|
2019-05-17 10:59:37 +00:00
|
|
|
if (args.is_present("n")) {
|
|
|
|
line_count = strtol(args.get("n").characters(), NULL, 10);
|
|
|
|
if (errno == EINVAL) {
|
|
|
|
args_parser.print_usage();
|
2019-06-06 08:53:42 +00:00
|
|
|
return 1;
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
2019-06-06 18:28:58 +00:00
|
|
|
} else {
|
2019-06-07 09:49:31 +00:00
|
|
|
line_count = DEFAULT_LINE_COUNT;
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 10:59:37 +00:00
|
|
|
CFile f(values[0]);
|
2019-05-16 13:02:17 +00:00
|
|
|
if (!f.open(CIODevice::ReadOnly)) {
|
2019-05-17 10:59:37 +00:00
|
|
|
fprintf(stderr, "Error opening file %s: %s\n", f.filename().characters(), strerror(errno));
|
2019-06-06 08:53:42 +00:00
|
|
|
exit(1);
|
2019-05-16 13:02:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 10:59:37 +00:00
|
|
|
bool flag_follow = args.is_present("f");
|
2019-05-16 13:02:17 +00:00
|
|
|
auto pos = find_seek_pos(f, line_count);
|
|
|
|
return tail_from_pos(f, pos, flag_follow);
|
|
|
|
}
|