2019-06-01 11:39:49 +00:00
|
|
|
#include <errno.h>
|
2019-04-25 22:47:30 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2019-06-01 11:39:49 +00:00
|
|
|
#include <LibCore/CArgsParser.h>
|
|
|
|
|
|
|
|
int head(const String& filename, bool print_filename, int line_count);
|
2019-04-25 22:47:30 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2019-06-01 11:39:49 +00:00
|
|
|
CArgsParser args_parser("head");
|
|
|
|
|
|
|
|
args_parser.add_arg("n", "lines", "Number of lines to print (default 10)");
|
|
|
|
args_parser.add_arg("q", "Never print filenames");
|
|
|
|
args_parser.add_arg("v", "Always print filenames");
|
|
|
|
|
|
|
|
CArgsParserResult args = args_parser.parse(argc, (const char**)argv);
|
|
|
|
|
2019-04-25 22:47:30 +00:00
|
|
|
int line_count = 10;
|
2019-06-01 11:39:49 +00:00
|
|
|
if (args.is_present("n")) {
|
|
|
|
line_count = strtol(args.get("n").characters(), NULL, 10);
|
|
|
|
if (errno) {
|
|
|
|
args_parser.print_usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> files = args.get_single_values();
|
2019-04-25 22:47:30 +00:00
|
|
|
|
2019-06-01 11:39:49 +00:00
|
|
|
bool print_filenames = files.size() > 1;
|
|
|
|
|
|
|
|
if (args.is_present("v")) {
|
|
|
|
print_filenames = true;
|
|
|
|
} else if (args.is_present("q")) {
|
|
|
|
print_filenames = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.is_empty()) {
|
|
|
|
return head("", print_filenames, line_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
for (auto &file : files) {
|
|
|
|
if (head(file, print_filenames, line_count) != 0) {
|
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int head(const String& filename, bool print_filename, int line_count)
|
|
|
|
{
|
|
|
|
bool is_stdin = false;
|
|
|
|
FILE* fp = nullptr;
|
|
|
|
|
|
|
|
if (filename == "" || filename == "-") {
|
|
|
|
fp = stdin;
|
|
|
|
is_stdin = true;
|
|
|
|
} else {
|
|
|
|
fp = fopen(filename.characters(), "r");
|
|
|
|
if (!fp) {
|
|
|
|
fprintf(stderr, "can't open %s for reading: %s\n", filename.characters(), strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (print_filename) {
|
|
|
|
if (is_stdin) {
|
|
|
|
puts("==> standard input <==");
|
|
|
|
} else {
|
|
|
|
printf("==> %s <==\n", filename.characters());
|
2019-04-25 22:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int line = 0; line < line_count; ++line) {
|
|
|
|
char buffer[BUFSIZ];
|
2019-06-01 11:39:49 +00:00
|
|
|
auto* str = fgets(buffer, sizeof(buffer), fp);
|
2019-04-25 22:47:30 +00:00
|
|
|
if (!str)
|
|
|
|
break;
|
2019-06-01 11:39:49 +00:00
|
|
|
|
|
|
|
// specifically use fputs rather than puts, because fputs doesn't add
|
|
|
|
// its own newline.
|
2019-04-25 22:47:30 +00:00
|
|
|
fputs(str, stdout);
|
|
|
|
}
|
|
|
|
|
2019-06-01 11:39:49 +00:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
if (print_filename) {
|
|
|
|
puts("");
|
|
|
|
}
|
|
|
|
|
2019-04-25 22:47:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|