summaryrefslogtreecommitdiff
path: root/src/commands/head.c
blob: 446a9e64237f8921502b379f5422cf9f3e355eca (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "../command.h"

#include <stdio.h>
#include <string.h>

struct Flags {
    int count;
    bool lines;
    bool print_headers;
    bool dont_print_headers;
};

static void head_file_lines(FILE* file, struct Flags* flags) {
    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, struct Flags* flags) {
    char c;
    int count = flags->count;
    while(count > 0 && (c = getc(file)) != EOF) {
        putchar(c);
        count--;
    }

    fclose(file);
}

static void help() {
    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");
    exit(EXIT_SUCCESS);
}

static char* next_arg(int argc, char** argv, int index) {
    if (index >= argc) {
        error("error: expected another argument after option");
    }
    return argv[index];
}

static void print_header(char* path, struct Flags* flags, 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, struct Flags* flags, bool many) {
    FILE* file = get_file(path, "r");
    print_header(path, flags, many);
    if (flags->lines) {
        head_file_lines(file, flags);
    } else {
        head_file_chars(file, flags);
    }
}

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

    int start = 0;
    for (int i = 0; i < argc; i++) { 
        if (!prefix("-", argv[i])) break;
        if (streql(argv[0], "--help")) help();
        start++;
        int i_s = i;
        for (size_t j = 1; j < strlen(argv[i_s]); j++) {
            char c = argv[i_s][j];
            switch(c) {
                case 'c': {
                    start++;
                    flags.lines = false;
                    char* arg = next_arg(argc, argv, ++i);
                    long int bkm = get_blkm(arg);
                    if (bkm < 1) {
                        error("error: bkm cannot be less than 1");
                    }
                    flags.count = bkm;
                    break;
                }
                case 'n': {
                    start++;
                    flags.lines = true;
                    char* arg = next_arg(argc, argv, ++i);
                    long int bkm = get_blkm(arg);
                    if (bkm < 1) {
                        error("error: bkm cannot be less than 1");
                    }
                    flags.count = bkm;
                    break;
                }
                case 'q':
                    flags.dont_print_headers = true;
                    break;
                case 'v':
                    flags.print_headers = true;
                    break;
                default: {
                    error("error: unknown option -%c", c);
                }
            }
        }
    }

    int count = argc - start;

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

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

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

    return EXIT_SUCCESS;
}