Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * wc implementation for busybox |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame^] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */ |
| 11 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */ |
| 12 | |
| 13 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 14 | * |
| 15 | * Rewritten to fix a number of problems and do some size optimizations. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 16 | * Problems in the previous busybox implementation (besides bloat) included: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 17 | * 1) broken 'wc -c' optimization (read note below) |
| 18 | * 2) broken handling of '-' args |
| 19 | * 3) no checking of ferror on EOF returns |
| 20 | * 4) isprint() wasn't considered when word counting. |
| 21 | * |
| 22 | * TODO: |
| 23 | * |
| 24 | * When locale support is enabled, count multibyte chars in the '-m' case. |
| 25 | * |
| 26 | * NOTES: |
| 27 | * |
| 28 | * The previous busybox wc attempted an optimization using stat for the |
| 29 | * case of counting chars only. I omitted that because it was broken. |
| 30 | * It didn't take into account the possibility of input coming from a |
| 31 | * pipe, or input from a file with file pointer not at the beginning. |
| 32 | * |
| 33 | * To implement such a speed optimization correctly, not only do you |
| 34 | * need the size, but also the file position. Note also that the |
| 35 | * file position may be past the end of file. Consider the example |
| 36 | * (adapted from example in gnu wc.c) |
| 37 | * |
| 38 | * echo hello > /tmp/testfile && |
| 39 | * (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile |
| 40 | * |
| 41 | * for which 'wc -c' should output '0'. |
| 42 | */ |
| 43 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 45 | #include <stdlib.h> |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | #include <string.h> |
| 47 | #include <unistd.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 48 | #include "busybox.h" |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 49 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | #ifdef CONFIG_LOCALE_SUPPORT |
| 51 | #include <locale.h> |
| 52 | #include <ctype.h> |
| 53 | #define isspace_given_isprint(c) isspace(c) |
| 54 | #else |
| 55 | #undef isspace |
| 56 | #undef isprint |
| 57 | #define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9)))) |
| 58 | #define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20)) |
| 59 | #define isspace_given_isprint(c) ((c) == ' ') |
| 60 | #endif |
| 61 | |
| 62 | enum { |
| 63 | WC_LINES = 0, |
| 64 | WC_WORDS = 1, |
| 65 | WC_CHARS = 2, |
| 66 | WC_LENGTH = 3 |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 67 | }; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 68 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 69 | /* Note: If this changes, remember to change the initialization of |
| 70 | * 'name' in wc_main. It needs to point to the terminating nul. */ |
| 71 | static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */ |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 72 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | enum { |
| 74 | OP_INC_LINE = 1, /* OP_INC_LINE must be 1. */ |
| 75 | OP_SPACE = 2, |
| 76 | OP_NEWLINE = 4, |
| 77 | OP_TAB = 8, |
| 78 | OP_NUL = 16, |
| 79 | }; |
Matt Kraai | 38c15be | 2001-12-20 21:11:59 +0000 | [diff] [blame] | 80 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 81 | /* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section |
| 82 | * will need to be updated. */ |
| 83 | static const char fmt_str[] = " %7u\0 %s\n"; |
| 84 | static const char total_str[] = "total"; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 85 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | int wc_main(int argc, char **argv) |
| 87 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | FILE *fp; |
| 89 | const char *s; |
| 90 | unsigned int *pcounts; |
| 91 | unsigned int counts[4]; |
| 92 | unsigned int totals[4]; |
| 93 | unsigned int linepos; |
| 94 | unsigned int u; |
| 95 | int num_files = 0; |
| 96 | int c; |
| 97 | char status = EXIT_SUCCESS; |
| 98 | char in_word; |
| 99 | char print_type; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 100 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | print_type = bb_getopt_ulflags(argc, argv, wc_opts); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 103 | if (print_type == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 104 | print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS); |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 105 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 106 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | argv += optind; |
| 108 | if (!*argv) { |
| 109 | *--argv = (char *) bb_msg_standard_input; |
| 110 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 111 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | memset(totals, 0, sizeof(totals)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 113 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 114 | pcounts = counts; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 115 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 116 | do { |
| 117 | ++num_files; |
| 118 | if (!(fp = bb_wfopen_input(*argv))) { |
| 119 | status = EXIT_FAILURE; |
| 120 | continue; |
| 121 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 122 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 123 | memset(counts, 0, sizeof(counts)); |
| 124 | linepos = 0; |
| 125 | in_word = 0; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 126 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | do { |
| 128 | ++counts[WC_CHARS]; |
| 129 | c = getc(fp); |
| 130 | if (isprint(c)) { |
| 131 | ++linepos; |
| 132 | if (!isspace_given_isprint(c)) { |
| 133 | in_word = 1; |
| 134 | continue; |
| 135 | } |
| 136 | } else if (((unsigned int)(c - 9)) <= 4) { |
| 137 | /* \t 9 |
| 138 | * \n 10 |
| 139 | * \v 11 |
| 140 | * \f 12 |
| 141 | * \r 13 |
| 142 | */ |
| 143 | if (c == '\t') { |
| 144 | linepos = (linepos | 7) + 1; |
| 145 | } else { /* '\n', '\r', '\f', or '\v' */ |
| 146 | DO_EOF: |
| 147 | if (linepos > counts[WC_LENGTH]) { |
| 148 | counts[WC_LENGTH] = linepos; |
| 149 | } |
| 150 | if (c == '\n') { |
| 151 | ++counts[WC_LINES]; |
| 152 | } |
| 153 | if (c != '\v') { |
| 154 | linepos = 0; |
| 155 | } |
| 156 | } |
| 157 | } else if (c == EOF) { |
| 158 | if (ferror(fp)) { |
| 159 | bb_perror_msg("%s", *argv); |
| 160 | status = EXIT_FAILURE; |
| 161 | } |
| 162 | --counts[WC_CHARS]; |
| 163 | goto DO_EOF; /* Treat an EOF as '\r'. */ |
Glenn L McGrath | 74afa9a | 2001-11-21 10:26:28 +0000 | [diff] [blame] | 164 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | continue; |
Glenn L McGrath | 74afa9a | 2001-11-21 10:26:28 +0000 | [diff] [blame] | 166 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 167 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 168 | counts[WC_WORDS] += in_word; |
| 169 | in_word = 0; |
| 170 | if (c == EOF) { |
| 171 | break; |
| 172 | } |
| 173 | } while (1); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 174 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 175 | if (totals[WC_LENGTH] < counts[WC_LENGTH]) { |
| 176 | totals[WC_LENGTH] = counts[WC_LENGTH]; |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 177 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 178 | totals[WC_LENGTH] -= counts[WC_LENGTH]; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 179 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 180 | bb_fclose_nonstdin(fp); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 181 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 182 | OUTPUT: |
| 183 | s = fmt_str + 1; /* Skip the leading space on 1st pass. */ |
| 184 | u = 0; |
| 185 | do { |
| 186 | if (print_type & (1 << u)) { |
| 187 | bb_printf(s, pcounts[u]); |
| 188 | s = fmt_str; /* Ok... restore the leading space. */ |
| 189 | } |
| 190 | totals[u] += pcounts[u]; |
| 191 | } while (++u < 4); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 192 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 193 | s += 8; /* Set the format to the empty string. */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 194 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 195 | if (*argv != bb_msg_standard_input) { |
| 196 | s -= 3; /* We have a name, so do %s conversion. */ |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 197 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 198 | bb_printf(s, *argv); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 199 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 200 | } while (*++argv); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 201 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 202 | /* If more than one file was processed, we want the totals. To save some |
| 203 | * space, we set the pcounts ptr to the totals array. This has the side |
| 204 | * effect of trashing the totals array after outputting it, but that's |
| 205 | * irrelavent since we no longer need it. */ |
| 206 | if (num_files > 1) { |
| 207 | num_files = 0; /* Make sure we don't get here again. */ |
| 208 | *--argv = (char *) total_str; |
| 209 | pcounts = totals; |
| 210 | goto OUTPUT; |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 211 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 212 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 213 | bb_fflush_stdout_and_exit(status); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 214 | } |