summaryrefslogtreecommitdiff
path: root/src/commands/wc.c
blob: 892ef2ec5e8a9ffac7b863eaed0f064557025c67 (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
#include "../command.h"

#include <ctype.h>

struct Flags {
    bool newlines;
    bool words;
    bool characters;
    bool bytes;
    bool longest_line;
};

static int lines = 0;
static int words = 0;
static int chars = 0;
static int bytes = 0;
static int logst = 0;

static void list(int l, int w, int c, int b, int lg, struct Flags* flags) {
    if (flags->newlines) {
        printf("\t%d", l);
    }
    if (flags->words) {
        printf("\t%d", w);
    }
    if (flags->characters) {
        printf("\t%d", c);
    }
    if (flags->bytes) {
        printf("\t%d", b);
    }
    if (flags->longest_line) {
        printf("\t%d", lg);
    }
}

#define BS 1024

static bool is_delimiter(char c) {
    return c == ' ' || c == '\t' || c == '\n';
}

static void run_wc(FILE* file, struct Flags* flags) {
    int l = 0, w = 0, c = 0, b = 0, lg = 0;

    bool in_word = false;
    int current_length = 0;

    int read;
    char buf[BS];
    while ((read = fread(buf, 1, 1024, file)) > 0) {
        for (int i = 0; i < read; i++) {
            char ch = buf[i];
            b++;
            if (ch == '\n') { 
                l++;
                lg = lg > current_length ? lg : current_length;
                current_length = 0;
            } else {
                current_length++;
            }
            if (isprint(ch) || is_delimiter(ch)) c++;
            if (in_word && is_delimiter(ch)) {
                in_word = false;
                w++;
            } else if (!in_word && !is_delimiter(ch) && isprint(ch)) {
                in_word = true;
            }
        }
    }
    if (in_word) w++;
    lg = lg > current_length ? lg : current_length;
    list(l, w, c, b, lg, flags);
    lines += l;
    words += w;
    chars += c;
    bytes += b;
    logst += lg;
    if (file != stdin)
        fclose(file);
}

static void help() {
    printf("Usage: wc [-cmlwL] [FILE]...\n\n");
    printf("Count lines, words, and bytes for FILEs (or stdin)\n\n");
	printf("\t-c	Count bytes\n");
	printf("\t-m	Count characters\n");
	printf("\t-l	Count newlines\n");
	printf("\t-w	Count words\n");
	printf("\t-L	Print longest line length\n");
    exit(EXIT_SUCCESS);
}

COMMAND(wc) {
    struct Flags flags;
    flags.newlines = false;
    flags.words = false;
    flags.characters = false;
    flags.bytes = false;
    flags.longest_line = false;

    bool has_flags = false;

    int start = 0;
    for (int i = 0; i < argc; i++) {
        if (!prefix("-", argv[i])) break;
        if (streql("--help", argv[i])) help();

        start++;
        for (size_t j = 1; j < strlen(argv[i]); j++) {
            char c = argv[i][j];
            switch (c) {
                case 'c':
                    flags.bytes = true;
                    break;
                case 'm':
                    flags.characters = true;
                    break;
                case 'l':
                    flags.newlines = true;
                    break;
                case 'w':
                    flags.words = true;
                    break;
                case 'L':
                    flags.longest_line = true;
                    break;
                default:
                    error("error: invald option -%c", c);
            }
            has_flags = true;
        }
    }

    if (!has_flags) {
        flags.newlines = true;
        flags.words = true;
        flags.characters = true;
    }

    if (argc - start < 1) {
        run_wc(stdin, &flags);
        printf("\n");
        return EXIT_SUCCESS;
    }

    for (int i = start; i < argc; i++) {
        FILE* file = get_file(argv[i], "r");
        run_wc(file, &flags);
        printf("\t%s\n", argv[i]);
    }

    if (argc - start > 1) {
        list(lines, words, chars, bytes, logst, &flags);
        printf("\ttotal\n");
    }

    return EXIT_SUCCESS;
}