summaryrefslogtreecommitdiff
path: root/Userland/head.cpp
blob: 49ae45ab183a916b069d3b45ae2f536dadeb69d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <LibCore/CArgsParser.h>

int head(const String& filename, bool print_filename, int line_count);

int main(int argc, char** argv)
{
    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);

    int line_count = 10;
    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();

    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());
        }
    }

    for (int line = 0; line < line_count; ++line) {
        char buffer[BUFSIZ];
        auto* str = fgets(buffer, sizeof(buffer), fp);
        if (!str)
            break;

        // specifically use fputs rather than puts, because fputs doesn't add
        // its own newline.
        fputs(str, stdout);
    }

    fclose(fp);

    if (print_filename) {
        puts("");
    }

    return 0;
}