blob: 8e3b5bbf438070cfab3b7d892b723c2363fc63c8 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen31638212000-01-15 22:28:50 +00002/*
3 * Mini wc implementation for busybox
4 *
Eric Andersen1b355eb2000-09-05 17:37:48 +00005 * Copyright (C) 2000 Edward Betts <edward@debian.org>
Erik Andersen31638212000-01-15 22:28:50 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Erik Andersen31638212000-01-15 22:28:50 +000023#include <stdio.h>
Mark Whitley39505962000-07-20 00:08:10 +000024#include <getopt.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000025#include <string.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000027#include "busybox.h"
Erik Andersen31638212000-01-15 22:28:50 +000028
Glenn L McGrath02d090d2001-11-21 09:17:00 +000029enum print_e {
30 print_lines = 1,
31 print_words = 2,
32 print_chars = 4,
33 print_length = 8
34};
Erik Andersen31638212000-01-15 22:28:50 +000035
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000036static unsigned int total_lines = 0;
37static unsigned int total_words = 0;
38static unsigned int total_chars = 0;
39static unsigned int max_length = 0;
40static char print_type = 0;
41
42static void print_counts(const unsigned int lines, const unsigned int words,
43 const unsigned int chars, const unsigned int length, const char *name)
Erik Andersene49d5ec2000-02-08 19:58:47 +000044{
Matt Kraai38c15be2001-12-20 21:11:59 +000045 int output = 0;
46
Glenn L McGrath02d090d2001-11-21 09:17:00 +000047 if (print_type & print_lines) {
Matt Kraai38c15be2001-12-20 21:11:59 +000048 printf("%7d", lines);
49 output++;
Erik Andersen31638212000-01-15 22:28:50 +000050 }
Glenn L McGrath02d090d2001-11-21 09:17:00 +000051 if (print_type & print_words) {
Matt Kraai38c15be2001-12-20 21:11:59 +000052 if (output++)
53 putchar(' ');
54 printf("%7d", words);
Erik Andersen31638212000-01-15 22:28:50 +000055 }
Glenn L McGrath02d090d2001-11-21 09:17:00 +000056 if (print_type & print_chars) {
Matt Kraai38c15be2001-12-20 21:11:59 +000057 if (output++)
58 putchar(' ');
59 printf("%7d", chars);
Erik Andersen31638212000-01-15 22:28:50 +000060 }
Glenn L McGrath1477ad82001-11-21 09:58:29 +000061 if (print_type & print_length) {
Matt Kraai38c15be2001-12-20 21:11:59 +000062 if (output++)
63 putchar(' ');
64 printf("%7d", length);
Glenn L McGrath1477ad82001-11-21 09:58:29 +000065 }
66 if (*name) {
Matt Kraai38c15be2001-12-20 21:11:59 +000067 printf(" %s", name);
Glenn L McGrath1477ad82001-11-21 09:58:29 +000068 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 putchar('\n');
Erik Andersen31638212000-01-15 22:28:50 +000070}
71
Erik Andersene49d5ec2000-02-08 19:58:47 +000072static void wc_file(FILE * file, const char *name)
Erik Andersen31638212000-01-15 22:28:50 +000073{
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000074 unsigned int lines = 0;
75 unsigned int words = 0;
76 unsigned int chars = 0;
77 unsigned int length = 0;
78 unsigned int linepos = 0;
79 char in_word = 0;
Matt Kraai741f40b2001-12-11 16:06:02 +000080 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081
Erik Andersen31638212000-01-15 22:28:50 +000082 while ((c = getc(file)) != EOF) {
83 chars++;
84 switch (c) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 case '\n':
86 lines++;
87 case '\r':
88 case '\f':
89 if (linepos > length)
90 length = linepos;
91 linepos = 0;
92 goto word_separator;
93 case '\t':
94 linepos += 8 - (linepos % 8);
95 goto word_separator;
96 case ' ':
97 linepos++;
98 case '\v':
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000099word_separator:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 if (in_word) {
101 in_word = 0;
102 words++;
103 }
104 break;
105 default:
106 linepos++;
107 in_word = 1;
108 break;
Erik Andersen31638212000-01-15 22:28:50 +0000109 }
110 }
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000111 if (linepos > length) {
Erik Andersen31638212000-01-15 22:28:50 +0000112 length = linepos;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000113 }
114 if (in_word) {
Erik Andersen31638212000-01-15 22:28:50 +0000115 words++;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000116 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117 print_counts(lines, words, chars, length, name);
Erik Andersen31638212000-01-15 22:28:50 +0000118 total_lines += lines;
119 total_words += words;
120 total_chars += chars;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000121 if (length > max_length) {
Erik Andersen31638212000-01-15 22:28:50 +0000122 max_length = length;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000123 }
Erik Andersen31638212000-01-15 22:28:50 +0000124}
125
Erik Andersene49d5ec2000-02-08 19:58:47 +0000126int wc_main(int argc, char **argv)
127{
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000128 int opt;
Erik Andersen31638212000-01-15 22:28:50 +0000129
Mark Whitley39505962000-07-20 00:08:10 +0000130 while ((opt = getopt(argc, argv, "clLw")) > 0) {
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000131 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000132 case 'c':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000133 print_type |= print_chars;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 break;
135 case 'l':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000136 print_type |= print_lines;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 break;
138 case 'L':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000139 print_type |= print_length;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 break;
141 case 'w':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000142 print_type |= print_words;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 break;
144 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000145 show_usage();
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000146 }
Erik Andersen31638212000-01-15 22:28:50 +0000147 }
148
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000149 if (print_type == 0) {
150 print_type = print_lines | print_words | print_chars;
151 }
Erik Andersen31638212000-01-15 22:28:50 +0000152
Mark Whitley39505962000-07-20 00:08:10 +0000153 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
Glenn L McGrathc29ab972001-11-21 10:01:29 +0000154 wc_file(stdin, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 } else {
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000156 unsigned short num_files_counted = 0;
Mark Whitley39505962000-07-20 00:08:10 +0000157 while (optind < argc) {
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000158 if (print_type == print_chars) {
159 struct stat statbuf;
160 stat(argv[optind], &statbuf);
161 print_counts(0, 0, statbuf.st_size, 0, argv[optind]);
162 total_chars += statbuf.st_size;
163 } else {
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000164 FILE *file;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000165 file = xfopen(argv[optind], "r");
Eric Andersen7a86e612000-10-09 18:21:44 +0000166 wc_file(file, argv[optind]);
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000167 fclose(file);
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000168 }
Mark Whitley39505962000-07-20 00:08:10 +0000169 optind++;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000170 num_files_counted++;
171 }
172 if (num_files_counted > 1) {
173 print_counts(total_lines, total_words, total_chars, max_length, "total");
Erik Andersen31638212000-01-15 22:28:50 +0000174 }
Mark Whitley39505962000-07-20 00:08:10 +0000175 }
176
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000177 return(EXIT_SUCCESS);
Erik Andersen31638212000-01-15 22:28:50 +0000178}