summaryrefslogtreecommitdiff
path: root/command/tail.c
blob: 4d6a2589f322a0a50861bb49c37ab7630aa16149 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "command.h"
#include "lslib.h"

#include <stdlib.h>
#include <string.h>
#include <unistd.h>

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

static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) {

    char** ring;
    int* ring_len;
    int index, read;
    unsigned int size, i;
    size_t len;
    char* line;

    ring = xalloc(sizeof(char*) * count);
    memset(ring, 0, sizeof(char*) * count);

    ring_len = xalloc(sizeof(int) * count);
    
    index = 0;
    size = 0;

    fseek(file, skip, SEEK_SET);
    
    len = skip;
    line = NULL;
    
    while ((read = getline(&line, &len, file)) != -1) {

        if (ring[index] != NULL) free(ring[index]);
        ring[index] = line;
        ring_len[index] = read;

        index++;
        index %= count;
        if (size < count) size++;

        line = NULL;
    }

    index += count - size;
    index %= count;
   
    for (i = 0; i < size; i++) {
        fwrite(ring[index], ring_len[index], 1, stdout);
        free(ring[index]);
        index += 1;
        index %= count;
    }

    free(line);
    fclose(file);
    free(ring);
    free(ring_len);

    return len;
}

static size_t tail_file_chars(FILE* file, unsigned int count, size_t skip) {

    char* ring;
    int index;
    unsigned int size, i;
    int read, c;

    ring = xalloc(sizeof(char) * count);
    memset(ring, 0, count);

    index = 0;
    size = 0;

    fseek(file, skip, SEEK_SET);
    read = skip;
    
    while((c = getc(file)) != EOF) {
        ring[index] = c;
        index++;
        read++;
        index %= count;
        if (size < count) size++;
    }

    index += count - size;
    index %= count;

    for (i = 0; i < size; i++) {
        putchar(ring[index]);
        index += 1;
        index %= count;
    }

    fclose(file);

    return read;
}

static void help(void) {
    printf("Usage: tail [OPTIONS] [FILE]...\n\n");
    printf("Print last 10 lines of FILEs (or stdin) to.\n");
    printf("With more than one FILE, precede each with a filename header.\n\n");
    printf("\t-c [+]N[bkm]\tPrint last N bytes\n");
	printf("\t-n N[bkm]\tPrint last 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");
	printf("\t-f\t\tPrint data as file grows\n");
	printf("\t-s SECONDS\tWait SECONDS between reads with -f\n");
    exit(EXIT_SUCCESS);
}

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 tail_file(char* path, bool many) {

    FILE* file;
    size_t skip;

    file = get_file(path, "r");
    print_header(path, many);
    
    skip = 0;
    while (true) {
        if (flags.lines) {
            skip = tail_file_lines(file, flags.count, skip);
        } else {
            skip = tail_file_chars(file, flags.count, skip);
        }
        if (!flags.print_as_grow) break;
        sleep(flags.grow_wait);
        get_file(path, "r");
    };
}

static int short_arg(char c, char* next) {
    switch (c) {
        case 'c': {
            long int bkm;

            flags.lines = false;
            
            check_arg(next);
            bkm = get_blkm(next);
            
            if (bkm < 1) {
                error("bkm cannot be less than 1");
            }
            
            flags.count = bkm;
            return ARG_USED;
        }
        case 'n': {
            long int bkm;

            flags.lines = true;
            
            check_arg(next);
            bkm = get_blkm(next);
            
            if (bkm < 1) {
                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;
        case 'f':
            flags.print_as_grow = true;
            break;
        case 's': {
            long int sec;

            check_arg(next);
            sec = get_number(next);
            
            if (sec < 1) {
                error("wait seconds cannot be less than 1");
            }
            
            flags.grow_wait = sec;
            return ARG_USED;
        }
        default: 
            return ARG_INVALID;
    }
    return ARG_UNUSED;
}

COMMAND(tail_main) {

    int start, count, i;

    flags.count = 10;
    flags.dont_print_headers = false;
    flags.print_headers = false;
    flags.lines = true;
    flags.print_as_grow = false;
    flags.grow_wait = 10;

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

    count = argc - start;

    if (count < 1) {
        tail_file_lines(stdin, 10, 0);
        return EXIT_SUCCESS;
    }

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

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

    return EXIT_SUCCESS;
}