summaryrefslogtreecommitdiff
path: root/src/commands/head.c
blob: 44bc45bcaec599ac828d10e127e3b20d38fc804a (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "../command.h"

static struct {
    int count;
    bool lines;
    bool print_headers;
    bool dont_print_headers;
} flags;

static void head_file_lines(FILE* file) {
    size_t len = 0;
    char* line = NULL;

    int count = flags.count;
    while(count > 0 && getline(&line, &len, file) != -1) {
        printf("%s", line);
        count--;
    }

    free(line);
    fclose(file);
}

static void head_file_chars(FILE* file) {
    char c;
    int count = flags.count;
    while(count > 0 && (c = getc(file)) != EOF) {
        putchar(c);
        count--;
    }

    fclose(file);
}

static void help(void) {
    printf("Usage: head [OPTIONS] [FILE]...\n\n");
    printf("Print first 10 lines of FILEs (or stdin)\n");
    printf("With more than one FILE, precede each with a filename header.\n\n");
    printf("\t-c [+]N[bkm]\tPrint first N bytes\n");
	printf("\t-n N[bkm]\tPrint first N lines\n");
	printf("\t\t\t(b:*512 k:*1024 m:*1024^2)\n");
	printf("\t-q\t\tNever print headers\n");
	printf("\t-v\t\tAlways print headers\n");
}

static void print_header(char* path, bool many) {
    if (flags.dont_print_headers) return;
    if (!many && !flags.print_headers) return;
    if (streql("-", path)) {
        printf("\n==> standard input <==\n");
    } else {
        printf("\n=>> %s <==\n", path);
    }
}

static void head_file(char* path, bool many) {
    FILE* file = get_file(path, "r");
    print_header(path, many);
    if (flags.lines) {
        head_file_lines(file);
    } else {
        head_file_chars(file);
    }
}

static int short_arg(char c, char* next) {
    switch(c) {
        case 'c': {
            flags.lines = false;
            check_arg(next);
            long int bkm = get_blkm(next);
            if (bkm < 1) {
                error("error: bkm cannot be less than 1");
            }
            flags.count = bkm;
            return ARG_USED;
        }
        case 'n': {
            flags.lines = true;
            check_arg(next);
            long int bkm = get_blkm(next);
            if (bkm < 1) {
                error("error: bkm cannot be less than 1");
            }
            flags.count = bkm;
            return ARG_USED;
        }
        case 'q':
            flags.dont_print_headers = true;
            break;
        case 'v':
            flags.print_headers = true;
            break;
        default: {
            error("error: unknown option -%c", c);
        }
    }
    return ARG_UNUSED;
}

COMMAND(head) {
    flags.count = 10;
    flags.lines = true;
    flags.print_headers = false;
    flags.dont_print_headers = false;

    int start = parse_args(argc, argv, help, short_arg, NULL);

    int count = argc - start;

    if (count < 1) {
        head_file_lines(stdin);
        return EXIT_SUCCESS;
    }

    if (count == 1) {
        head_file(argv[start], false);
        return EXIT_SUCCESS;
    }

    for (int i = 0; i < count; i++) {
        head_file(argv[start + i], true);
    }

    return EXIT_SUCCESS;
}