libbb: move isqrt from factor, use it in diff too
[oweals/busybox.git] / coreutils / wc.c
index ecadae59bea050e60eda4a9c2bdcc1c03a15cbf3..4c53049b0d564f64de1cc6a69b3e6544d0d12b1e 100644 (file)
@@ -6,10 +6,6 @@
  *
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-
-/* BB_AUDIT SUSv3 compliant. */
-/* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
-
 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
  *
  * Rewritten to fix a number of problems and do some size optimizations.
  *
  * for which 'wc -c' should output '0'.
  */
+//config:config WC
+//config:      bool "wc"
+//config:      default y
+//config:      help
+//config:        wc is used to print the number of bytes, words, and lines,
+//config:        in specified files.
+//config:
+//config:config FEATURE_WC_LARGE
+//config:      bool "Support very large counts"
+//config:      default y
+//config:      depends on WC
+//config:      help
+//config:        Use "unsigned long long" for counter variables.
+
+//applet:IF_WC(APPLET(wc, BB_DIR_USR_BIN, BB_SUID_DROP))
+
+//kbuild:lib-$(CONFIG_WC) += wc.o
+
+/* BB_AUDIT SUSv3 compliant. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
+
 #include "libbb.h"
 #include "unicode.h"
 
@@ -64,7 +81,6 @@
 //usage:
 //usage:#define wc_full_usage "\n\n"
 //usage:       "Count lines, words, and bytes for each FILE (or stdin)\n"
-//usage:     "\nOptions:"
 //usage:     "\n       -c      Count bytes"
 //usage:       IF_UNICODE_SUPPORT(
 //usage:     "\n       -m      Count characters"
  * column order in "wc -cmlwL" output:
  */
 enum {
-       WC_LINES    = 0,
-       WC_WORDS    = 1,
-       WC_UNICHARS = 2,
-       WC_CHARS    = 3,
-       WC_LENGTH   = 4,
+       WC_LINES    = 0, /* -l */
+       WC_WORDS    = 1, /* -w */
+       WC_UNICHARS = 2, /* -m */
+       WC_BYTES    = 3, /* -c */
+       WC_LENGTH   = 4, /* -L */
        NUM_WCS     = 5,
 };
 
@@ -104,10 +120,10 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
 
        init_unicode();
 
-       print_type = getopt32(argv, "lwcmL");
+       print_type = getopt32(argv, "lwmcL");
 
        if (print_type == 0) {
-               print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
+               print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_BYTES);
        }
 
        argv += optind;
@@ -153,11 +169,11 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
                                        bb_simple_perror_msg(arg);
                                        status = EXIT_FAILURE;
                                }
-                               goto DO_EOF;            /* Treat an EOF as '\r'. */
+                               goto DO_EOF;  /* Treat an EOF as '\r'. */
                        }
 
                        /* Cater for -c and -m */
-                       ++counts[WC_CHARS];
+                       ++counts[WC_BYTES];
                        if (unicode_status != UNICODE_ON /* every byte is a new char */
                         || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
                        ) {
@@ -179,7 +195,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
                                 */
                                if (c == '\t') {
                                        linepos = (linepos | 7) + 1;
-                               } else {                        /* '\n', '\r', '\f', or '\v' */
+                               } else {  /* '\n', '\r', '\f', or '\v' */
  DO_EOF:
                                        if (linepos > counts[WC_LENGTH]) {
                                                counts[WC_LENGTH] = linepos;
@@ -230,7 +246,7 @@ int wc_main(int argc UNUSED_PARAM, char **argv)
         * effect of trashing the totals array after outputting it, but that's
         * irrelavent since we no longer need it. */
        if (num_files > 1) {
-               num_files = 0;                          /* Make sure we don't get here again. */
+               num_files = 0;  /* Make sure we don't get here again. */
                arg = "total";
                pcounts = totals;
                --argv;