blob: fb81c0a8f7d7c21f53c0a841435c3ddbc8d02e6a [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{
Glenn L McGrath02d090d2001-11-21 09:17:00 +000045 if (print_type & print_lines) {
Glenn L McGrath1477ad82001-11-21 09:58:29 +000046 printf("%7d ", lines);
Erik Andersen31638212000-01-15 22:28:50 +000047 }
Glenn L McGrath02d090d2001-11-21 09:17:00 +000048 if (print_type & print_words) {
Glenn L McGrath1477ad82001-11-21 09:58:29 +000049 printf("%7d ", words);
Erik Andersen31638212000-01-15 22:28:50 +000050 }
Glenn L McGrath02d090d2001-11-21 09:17:00 +000051 if (print_type & print_chars) {
Glenn L McGrath1477ad82001-11-21 09:58:29 +000052 printf("%7d ", chars);
Erik Andersen31638212000-01-15 22:28:50 +000053 }
Glenn L McGrath1477ad82001-11-21 09:58:29 +000054 if (print_type & print_length) {
55 printf("%7d ", length);
56 }
57 if (*name) {
58 printf("%s", name);
59 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 putchar('\n');
Erik Andersen31638212000-01-15 22:28:50 +000061}
62
Erik Andersene49d5ec2000-02-08 19:58:47 +000063static void wc_file(FILE * file, const char *name)
Erik Andersen31638212000-01-15 22:28:50 +000064{
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000065 unsigned int lines = 0;
66 unsigned int words = 0;
67 unsigned int chars = 0;
68 unsigned int length = 0;
69 unsigned int linepos = 0;
70 char in_word = 0;
Matt Kraai741f40b2001-12-11 16:06:02 +000071 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000072
Erik Andersen31638212000-01-15 22:28:50 +000073 while ((c = getc(file)) != EOF) {
74 chars++;
75 switch (c) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 case '\n':
77 lines++;
78 case '\r':
79 case '\f':
80 if (linepos > length)
81 length = linepos;
82 linepos = 0;
83 goto word_separator;
84 case '\t':
85 linepos += 8 - (linepos % 8);
86 goto word_separator;
87 case ' ':
88 linepos++;
89 case '\v':
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +000090word_separator:
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 if (in_word) {
92 in_word = 0;
93 words++;
94 }
95 break;
96 default:
97 linepos++;
98 in_word = 1;
99 break;
Erik Andersen31638212000-01-15 22:28:50 +0000100 }
101 }
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000102 if (linepos > length) {
Erik Andersen31638212000-01-15 22:28:50 +0000103 length = linepos;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000104 }
105 if (in_word) {
Erik Andersen31638212000-01-15 22:28:50 +0000106 words++;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000107 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000108 print_counts(lines, words, chars, length, name);
Erik Andersen31638212000-01-15 22:28:50 +0000109 total_lines += lines;
110 total_words += words;
111 total_chars += chars;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000112 if (length > max_length) {
Erik Andersen31638212000-01-15 22:28:50 +0000113 max_length = length;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000114 }
Erik Andersen31638212000-01-15 22:28:50 +0000115}
116
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117int wc_main(int argc, char **argv)
118{
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000119 int opt;
Erik Andersen31638212000-01-15 22:28:50 +0000120
Mark Whitley39505962000-07-20 00:08:10 +0000121 while ((opt = getopt(argc, argv, "clLw")) > 0) {
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000122 switch (opt) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 case 'c':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000124 print_type |= print_chars;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 break;
126 case 'l':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000127 print_type |= print_lines;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 break;
129 case 'L':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000130 print_type |= print_length;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 break;
132 case 'w':
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000133 print_type |= print_words;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000134 break;
135 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000136 show_usage();
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000137 }
Erik Andersen31638212000-01-15 22:28:50 +0000138 }
139
Glenn L McGrath02d090d2001-11-21 09:17:00 +0000140 if (print_type == 0) {
141 print_type = print_lines | print_words | print_chars;
142 }
Erik Andersen31638212000-01-15 22:28:50 +0000143
Mark Whitley39505962000-07-20 00:08:10 +0000144 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
Glenn L McGrathc29ab972001-11-21 10:01:29 +0000145 wc_file(stdin, "");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 } else {
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000147 unsigned short num_files_counted = 0;
Mark Whitley39505962000-07-20 00:08:10 +0000148 while (optind < argc) {
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000149 if (print_type == print_chars) {
150 struct stat statbuf;
151 stat(argv[optind], &statbuf);
152 print_counts(0, 0, statbuf.st_size, 0, argv[optind]);
153 total_chars += statbuf.st_size;
154 } else {
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000155 FILE *file;
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000156 file = xfopen(argv[optind], "r");
Eric Andersen7a86e612000-10-09 18:21:44 +0000157 wc_file(file, argv[optind]);
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000158 fclose(file);
Glenn L McGrath74afa9a2001-11-21 10:26:28 +0000159 }
Mark Whitley39505962000-07-20 00:08:10 +0000160 optind++;
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000161 num_files_counted++;
162 }
163 if (num_files_counted > 1) {
164 print_counts(total_lines, total_words, total_chars, max_length, "total");
Erik Andersen31638212000-01-15 22:28:50 +0000165 }
Mark Whitley39505962000-07-20 00:08:10 +0000166 }
167
Glenn L McGrath9e6c9f72001-11-21 12:46:36 +0000168 return(EXIT_SUCCESS);
Erik Andersen31638212000-01-15 22:28:50 +0000169}