2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-08 16:30:42 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-14 12:25:11 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-06-01 14:39:49 +03:00
|
|
|
#include <errno.h>
|
2021-05-04 18:41:38 +03:00
|
|
|
#include <fcntl.h>
|
2019-04-26 01:47:30 +03:00
|
|
|
#include <stdio.h>
|
2020-03-08 14:05:14 +03:00
|
|
|
#include <string.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <unistd.h>
|
2019-06-01 14:39:49 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
int head(ByteString const& filename, bool print_filename, ssize_t line_count, ssize_t byte_count);
|
2019-04-26 01:47:30 +03:00
|
|
|
|
2022-01-14 12:25:11 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2019-04-26 01:47:30 +03:00
|
|
|
{
|
2022-04-04 02:16:06 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-02-22 23:14:21 +03:00
|
|
|
|
2021-05-04 18:41:38 +03:00
|
|
|
int line_count = -1;
|
|
|
|
int byte_count = -1;
|
2020-01-27 20:25:36 +03:00
|
|
|
bool never_print_filenames = false;
|
|
|
|
bool always_print_filenames = false;
|
2023-12-16 17:19:34 +03:00
|
|
|
Vector<ByteString> files;
|
2020-01-27 20:25:36 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 18:22:58 +03:00
|
|
|
args_parser.set_general_help("Print the beginning ('head') of a file.");
|
2020-01-27 20:25:36 +03:00
|
|
|
args_parser.add_option(line_count, "Number of lines to print (default 10)", "lines", 'n', "number");
|
2021-05-04 18:41:38 +03:00
|
|
|
args_parser.add_option(byte_count, "Number of bytes to print", "bytes", 'c', "number");
|
2021-04-29 22:46:15 +03:00
|
|
|
args_parser.add_option(never_print_filenames, "Never print filenames", "quiet", 'q');
|
|
|
|
args_parser.add_option(always_print_filenames, "Always print filenames", "verbose", 'v');
|
2020-02-02 14:34:39 +03:00
|
|
|
args_parser.add_positional_argument(files, "File to process", "file", Core::ArgsParser::Required::No);
|
2022-01-14 12:25:11 +03:00
|
|
|
args_parser.parse(args);
|
2019-06-08 16:30:42 +03:00
|
|
|
|
2021-05-04 18:41:38 +03:00
|
|
|
if (line_count == -1 && byte_count == -1) {
|
2019-06-08 16:30:42 +03:00
|
|
|
line_count = 10;
|
2019-06-01 14:39:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool print_filenames = files.size() > 1;
|
2020-01-27 20:25:36 +03:00
|
|
|
if (always_print_filenames)
|
2019-06-01 14:39:49 +03:00
|
|
|
print_filenames = true;
|
2020-01-27 20:25:36 +03:00
|
|
|
else if (never_print_filenames)
|
2019-06-01 14:39:49 +03:00
|
|
|
print_filenames = false;
|
|
|
|
|
|
|
|
if (files.is_empty()) {
|
2021-05-04 18:41:38 +03:00
|
|
|
return head("", print_filenames, line_count, byte_count);
|
2019-06-01 14:39:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
|
2019-06-07 12:49:31 +03:00
|
|
|
for (auto& file : files) {
|
2021-05-04 18:41:38 +03:00
|
|
|
if (head(file, print_filenames, line_count, byte_count) != 0) {
|
2019-06-01 14:39:49 +03:00
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
int head(ByteString const& filename, bool print_filename, ssize_t line_count, ssize_t byte_count)
|
2019-06-01 14:39:49 +03:00
|
|
|
{
|
|
|
|
bool is_stdin = false;
|
2021-05-04 18:41:38 +03:00
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
ScopeGuard fd_close_guard = [&fd] {
|
|
|
|
if (fd > 0)
|
|
|
|
close(fd);
|
|
|
|
};
|
2019-06-01 14:39:49 +03:00
|
|
|
|
|
|
|
if (filename == "" || filename == "-") {
|
2021-05-04 18:41:38 +03:00
|
|
|
fd = 0;
|
2019-06-01 14:39:49 +03:00
|
|
|
is_stdin = true;
|
|
|
|
} else {
|
2021-05-04 18:41:38 +03:00
|
|
|
fd = open(filename.characters(), O_RDONLY);
|
|
|
|
if (fd < 0) {
|
2021-05-31 17:43:25 +03:00
|
|
|
warnln("Failed to open {}: {}", filename, strerror(errno));
|
2019-06-01 14:39:49 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_filename) {
|
|
|
|
if (is_stdin) {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("==> standard input <==");
|
2019-06-01 14:39:49 +03:00
|
|
|
} else {
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("==> {} <==", filename);
|
2019-04-26 01:47:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:41:38 +03:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
size_t buffer_size = line_count != -1 ? BUFSIZ : min((size_t)BUFSIZ, (size_t)byte_count);
|
|
|
|
char buffer[buffer_size];
|
2019-06-08 16:30:42 +03:00
|
|
|
|
2021-05-04 18:41:38 +03:00
|
|
|
while (line_count > 0 || byte_count > 0) {
|
|
|
|
size_t ntoread = line_count != -1 ? buffer_size : min(buffer_size, (size_t)byte_count);
|
|
|
|
ssize_t nread = read(fd, buffer, ntoread);
|
|
|
|
if (nread < 0) {
|
|
|
|
perror("read");
|
|
|
|
return 1;
|
|
|
|
} else if (nread == 0) {
|
|
|
|
break;
|
2019-06-08 16:30:42 +03:00
|
|
|
}
|
2021-05-04 18:41:38 +03:00
|
|
|
|
|
|
|
size_t ntowrite;
|
|
|
|
if (byte_count != -1) {
|
|
|
|
// Write out everything we've read, since we have explicitly ensured
|
|
|
|
// that we wouldn't read more than we want to write.
|
|
|
|
ntowrite = nread;
|
|
|
|
byte_count -= nread;
|
|
|
|
} else {
|
|
|
|
// Count line breaks.
|
|
|
|
ntowrite = 0;
|
|
|
|
while (line_count) {
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* newline = strchr(buffer + ntowrite, '\n');
|
2021-05-04 18:41:38 +03:00
|
|
|
if (newline) {
|
|
|
|
// Found another line break, include this line.
|
|
|
|
ntowrite = newline - buffer + 1;
|
|
|
|
line_count--;
|
|
|
|
} else {
|
|
|
|
// No more line breaks, write the whole thing.
|
|
|
|
ntowrite = nread;
|
|
|
|
break;
|
2019-06-08 16:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
2021-05-04 18:41:38 +03:00
|
|
|
}
|
2019-06-08 16:30:42 +03:00
|
|
|
|
2021-05-04 18:41:38 +03:00
|
|
|
size_t ncomplete = 0;
|
|
|
|
while (ncomplete < ntowrite) {
|
|
|
|
ssize_t nwritten = write(1, buffer + ncomplete, ntowrite - ncomplete);
|
|
|
|
if (nwritten < 0) {
|
|
|
|
perror("write");
|
|
|
|
return 1;
|
2019-06-08 16:30:42 +03:00
|
|
|
}
|
2021-05-04 18:41:38 +03:00
|
|
|
ncomplete += nwritten;
|
2019-06-08 16:30:42 +03:00
|
|
|
}
|
2019-04-26 01:47:30 +03:00
|
|
|
}
|
|
|
|
|
2019-06-01 14:39:49 +03:00
|
|
|
if (print_filename) {
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
|
2019-04-26 01:47:30 +03:00
|
|
|
return 0;
|
|
|
|
}
|