2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-01-16 03:58:50 +05:30
|
|
|
/*
|
|
|
|
* Mini wc implementation for busybox
|
|
|
|
*
|
2000-09-05 23:07:48 +05:30
|
|
|
* Copyright (C) 2000 Edward Betts <edward@debian.org>
|
2000-01-16 03:58:50 +05:30
|
|
|
*
|
|
|
|
* 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>
|
2000-07-20 05:38:10 +05:30
|
|
|
#include <getopt.h>
|
2001-03-09 20:06:42 +05:30
|
|
|
#include <string.h>
|
2001-01-27 13:54:39 +05:30
|
|
|
#include <stdlib.h>
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
2000-01-16 03:58:50 +05:30
|
|
|
|
2001-11-21 14:47:00 +05:30
|
|
|
enum print_e {
|
|
|
|
print_lines = 1,
|
|
|
|
print_words = 2,
|
|
|
|
print_chars = 4,
|
|
|
|
print_length = 8
|
|
|
|
};
|
2000-01-16 03:58:50 +05:30
|
|
|
|
2001-11-21 18:16:36 +05:30
|
|
|
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)
|
2000-02-09 01:28:47 +05:30
|
|
|
{
|
2001-12-21 02:41:59 +05:30
|
|
|
int output = 0;
|
|
|
|
|
2001-11-21 14:47:00 +05:30
|
|
|
if (print_type & print_lines) {
|
2001-12-21 02:41:59 +05:30
|
|
|
printf("%7d", lines);
|
|
|
|
output++;
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
2001-11-21 14:47:00 +05:30
|
|
|
if (print_type & print_words) {
|
2001-12-21 02:41:59 +05:30
|
|
|
if (output++)
|
|
|
|
putchar(' ');
|
|
|
|
printf("%7d", words);
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
2001-11-21 14:47:00 +05:30
|
|
|
if (print_type & print_chars) {
|
2001-12-21 02:41:59 +05:30
|
|
|
if (output++)
|
|
|
|
putchar(' ');
|
|
|
|
printf("%7d", chars);
|
2001-11-21 15:28:29 +05:30
|
|
|
}
|
|
|
|
if (print_type & print_length) {
|
2001-12-21 02:41:59 +05:30
|
|
|
if (output++)
|
|
|
|
putchar(' ');
|
|
|
|
printf("%7d", length);
|
2001-11-21 15:28:29 +05:30
|
|
|
}
|
|
|
|
if (*name) {
|
2001-12-21 02:41:59 +05:30
|
|
|
printf(" %s", name);
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
putchar('\n');
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
static void wc_file(FILE * file, const char *name)
|
2000-01-16 03:58:50 +05:30
|
|
|
{
|
2001-11-21 18:16:36 +05:30
|
|
|
unsigned int lines = 0;
|
|
|
|
unsigned int words = 0;
|
|
|
|
unsigned int chars = 0;
|
|
|
|
unsigned int length = 0;
|
|
|
|
unsigned int linepos = 0;
|
|
|
|
char in_word = 0;
|
2001-12-11 21:36:02 +05:30
|
|
|
int c;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2000-01-16 03:58:50 +05:30
|
|
|
while ((c = getc(file)) != EOF) {
|
|
|
|
chars++;
|
|
|
|
switch (c) {
|
2000-02-09 01:28:47 +05:30
|
|
|
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':
|
2001-11-21 18:16:36 +05:30
|
|
|
word_separator:
|
2000-02-09 01:28:47 +05:30
|
|
|
if (in_word) {
|
|
|
|
in_word = 0;
|
|
|
|
words++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
linepos++;
|
|
|
|
in_word = 1;
|
|
|
|
break;
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
|
|
|
}
|
2001-11-21 18:16:36 +05:30
|
|
|
if (linepos > length) {
|
2000-01-16 03:58:50 +05:30
|
|
|
length = linepos;
|
2001-11-21 18:16:36 +05:30
|
|
|
}
|
|
|
|
if (in_word) {
|
2000-01-16 03:58:50 +05:30
|
|
|
words++;
|
2001-11-21 18:16:36 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
print_counts(lines, words, chars, length, name);
|
2000-01-16 03:58:50 +05:30
|
|
|
total_lines += lines;
|
|
|
|
total_words += words;
|
|
|
|
total_chars += chars;
|
2001-11-21 18:16:36 +05:30
|
|
|
if (length > max_length) {
|
2000-01-16 03:58:50 +05:30
|
|
|
max_length = length;
|
2001-11-21 18:16:36 +05:30
|
|
|
}
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
int wc_main(int argc, char **argv)
|
|
|
|
{
|
2001-11-21 18:16:36 +05:30
|
|
|
int opt;
|
2000-01-16 03:58:50 +05:30
|
|
|
|
2000-07-20 05:38:10 +05:30
|
|
|
while ((opt = getopt(argc, argv, "clLw")) > 0) {
|
2001-11-21 18:16:36 +05:30
|
|
|
switch (opt) {
|
2000-02-09 01:28:47 +05:30
|
|
|
case 'c':
|
2001-11-21 14:47:00 +05:30
|
|
|
print_type |= print_chars;
|
2000-02-09 01:28:47 +05:30
|
|
|
break;
|
|
|
|
case 'l':
|
2001-11-21 14:47:00 +05:30
|
|
|
print_type |= print_lines;
|
2000-02-09 01:28:47 +05:30
|
|
|
break;
|
|
|
|
case 'L':
|
2001-11-21 14:47:00 +05:30
|
|
|
print_type |= print_length;
|
2000-02-09 01:28:47 +05:30
|
|
|
break;
|
|
|
|
case 'w':
|
2001-11-21 14:47:00 +05:30
|
|
|
print_type |= print_words;
|
2000-02-09 01:28:47 +05:30
|
|
|
break;
|
|
|
|
default:
|
2001-02-15 02:53:06 +05:30
|
|
|
show_usage();
|
2001-11-21 18:16:36 +05:30
|
|
|
}
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
|
|
|
|
2001-11-21 14:47:00 +05:30
|
|
|
if (print_type == 0) {
|
|
|
|
print_type = print_lines | print_words | print_chars;
|
|
|
|
}
|
2000-01-16 03:58:50 +05:30
|
|
|
|
2000-07-20 05:38:10 +05:30
|
|
|
if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
|
2001-11-21 15:31:29 +05:30
|
|
|
wc_file(stdin, "");
|
2000-02-09 01:28:47 +05:30
|
|
|
} else {
|
2001-11-21 18:16:36 +05:30
|
|
|
unsigned short num_files_counted = 0;
|
2000-07-20 05:38:10 +05:30
|
|
|
while (optind < argc) {
|
2001-11-21 15:56:28 +05:30
|
|
|
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 {
|
2001-11-21 18:16:36 +05:30
|
|
|
FILE *file;
|
2001-11-21 15:56:28 +05:30
|
|
|
file = xfopen(argv[optind], "r");
|
2000-10-09 23:51:44 +05:30
|
|
|
wc_file(file, argv[optind]);
|
2001-11-21 18:16:36 +05:30
|
|
|
fclose(file);
|
2001-11-21 15:56:28 +05:30
|
|
|
}
|
2000-07-20 05:38:10 +05:30
|
|
|
optind++;
|
2001-11-21 18:16:36 +05:30
|
|
|
num_files_counted++;
|
|
|
|
}
|
|
|
|
if (num_files_counted > 1) {
|
|
|
|
print_counts(total_lines, total_words, total_chars, max_length, "total");
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|
2000-07-20 05:38:10 +05:30
|
|
|
}
|
|
|
|
|
2001-11-21 18:16:36 +05:30
|
|
|
return(EXIT_SUCCESS);
|
2000-01-16 03:58:50 +05:30
|
|
|
}
|