summaryrefslogtreecommitdiff
path: root/coreutils/wc.c
blob: 8e3b5bbf438070cfab3b7d892b723c2363fc63c8 (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
/* vi: set sw=4 ts=4: */
/*
 * Mini wc implementation for busybox
 *
 * Copyright (C) 2000  Edward Betts <edward@debian.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */

#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include "busybox.h"

enum print_e {
	print_lines = 1,
	print_words = 2,
	print_chars = 4,
	print_length = 8
};

static unsigned int total_lines = 0;
static unsigned int total_words = 0;
static unsigned int total_chars = 0;
static unsigned int max_length = 0;
static char print_type = 0;

static void print_counts(const unsigned int lines, const unsigned int words,
	const unsigned int chars, const unsigned int length, const char *name)
{
	int output = 0;

	if (print_type & print_lines) {
		printf("%7d", lines);
		output++;
	}
	if (print_type & print_words) {
		if (output++)
			putchar(' ');
		printf("%7d", words);
	}
	if (print_type & print_chars) {
		if (output++)
			putchar(' ');
		printf("%7d", chars);
	}
	if (print_type & print_length) {
		if (output++)
			putchar(' ');
		printf("%7d", length);
	}
	if (*name) {
		printf(" %s", name);
	}
	putchar('\n');
}

static void wc_file(FILE * file, const char *name)
{
	unsigned int lines = 0;
	unsigned int words = 0;
	unsigned int chars = 0;
	unsigned int length = 0;
	unsigned int linepos = 0;
	char in_word = 0;
	int c;

	while ((c = getc(file)) != EOF) {
		chars++;
		switch (c) {
		case '\n':
			lines++;
		case '\r':
		case '\f':
			if (linepos > length)
				length = linepos;
			linepos = 0;
			goto word_separator;
		case '\t':
			linepos += 8 - (linepos % 8);
			goto word_separator;
		case ' ':
			linepos++;
		case '\v':
word_separator:
			if (in_word) {
				in_word = 0;
				words++;
			}
			break;
		default:
			linepos++;
			in_word = 1;
			break;
		}
	}
	if (linepos > length) {
		length = linepos;
	}
	if (in_word) {
		words++;
	}
	print_counts(lines, words, chars, length, name);
	total_lines += lines;
	total_words += words;
	total_chars += chars;
	if (length > max_length) {
		max_length = length;
	}
}

int wc_main(int argc, char **argv)
{
	int opt;

	while ((opt = getopt(argc, argv, "clLw")) > 0) {
		switch (opt) {
			case 'c':
				print_type |= print_chars;
				break;
			case 'l':
				print_type |= print_lines;
				break;
			case 'L':
				print_type |= print_length;
				break;
			case 'w':
				print_type |= print_words;
				break;
			default:
				show_usage();
		}
	}

	if (print_type == 0) {
		print_type = print_lines | print_words | print_chars;
	}

	if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
		wc_file(stdin, "");
	} else {
		unsigned short num_files_counted = 0;
		while (optind < argc) {
			if (print_type == print_chars) {
				struct stat statbuf;
				stat(argv[optind], &statbuf);
				print_counts(0, 0, statbuf.st_size, 0, argv[optind]);
				total_chars += statbuf.st_size;
			} else {
				FILE *file;
				file = xfopen(argv[optind], "r");
				wc_file(file, argv[optind]);
				fclose(file);
			}
			optind++;
			num_files_counted++;
		}
		if (num_files_counted > 1) {
			print_counts(total_lines, total_words, total_chars, max_length, "total");
		}
	}

	return(EXIT_SUCCESS);
}