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
|
/* SPDX-License-Identifier: BSD-3-Clause */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
#define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
#define DESCRIPTION "Word, line, and byte/character count."
#define OPERANDS "[-clwm] [file...]"
#include "../common/common.h"
int main(int argc, char *const argv[]) {
int argument, length;
FILE *file;
size_t len_getd = 4096;
char *line = NULL, *argv0 = strdup(argv[0]);
char param[256];
ssize_t bytes = 0, newlines = 0, words = 0,
total_bytes = 0, total_newlines = 0, total_words = 0;
if (argc < 2)
return print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
while ((argument = getopt(argc, argv, "clmw")) != -1) {
if (argument == '?') {
print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
return 1;
}
param[(uint8_t)argument] = argument;
} argc -= optind; argv += optind;
if (!param['l'] && !param['m'] && !param['c'] && !param['w']) {
param['l'] = 'l';
param['m'] = 'm';
param['c'] = 'c';
param['w'] = 'w';
}
for (int i = 0; i != argc; i++) {
words = 0; bytes = 0; newlines = 0;
file = fopen(argv[i], "r");
if (errno) return errprint(argv0, argv[i], errno);
while ((length = getline(&line, &len_getd, file)) != -1) {
if (param['l']) newlines++;
if (param['c'] || param['m']) bytes += length;
} rewind(file);
if (errno) return errprint(argv0, argv[i], errno);
if (param['w']) while ((length = getdelim(&line, &len_getd, (int)' ', file)) != -1) {
words++;
}
if (errno) return errprint(argv0, argv[i], errno);
if (param['l']) printf("%zu ", newlines);
if (param['w']) printf("%zu ", words);
if (param['m'] || param['c']) printf("%zu ", bytes);
printf("%s\n", argv[i]);
fclose(file);
if (errno) return errprint(argv0, argv[i], errno);
total_bytes += bytes;
total_words += words;
total_newlines += newlines;
}
if (total_bytes != bytes) {
if (param['l']) printf("%zu ", total_newlines);
if (param['w']) printf("%zu ", total_words);
if (param['m'] || param['c']) printf("%zu ", total_bytes);
printf("total\n");
}
free(line);
if (errno) return errprint(argv0, NULL, errno);
return 0;
}
|