view toys/posix/wc.c @ 1775:57f2a26fa92c draft toast

To ensure that toybox can be installed alongside busybox without confusing update-alternatives, the paths of the links installed by toybox should match those installed by busybox. This is accomplished by changing the flags of a few tools within toybox.
author Paul Barker <>
date Sat, 04 Apr 2015 11:58:06 -0500
parents 3b85d2ce34aa
line wrap: on
line source

/* wc.c - Word count
 * Copyright 2011 Rob Landley <>
 * See


config WC
  bool "wc"
  default y
    usage: wc -lwcm [FILE...]

    Count lines, words, and characters in input.

    -l	show lines
    -w	show words
    -c	show bytes
    -m	show characters

    By default outputs lines, words, bytes, and filename for each
    argument (or from stdin if none). Displays only either bytes
    or characters.

#define FOR_wc
#include "toys.h"

  unsigned long totals[3];

static void show_lengths(unsigned long *lengths, char *name)
  int i, nospace = 1;
  for (i=0; i<3; i++) {
    if (!toys.optflags || (toys.optflags&(1<<i))) {
      xprintf(" %ld"+nospace, lengths[i]);
      nospace = 0;
    TT.totals[i] += lengths[i];
  if (*toys.optargs) xprintf(" %s", name);

static void do_wc(int fd, char *name)
  int i, len, clen=1, space;
  unsigned long word=0, lengths[]={0,0,0};

  for (;;) {
    len = read(fd, toybuf, sizeof(toybuf));
    if (len<0) perror_msg("%s", name);
    if (len<1) break;
    for (i=0; i<len; i+=clen) {
      wchar_t wchar;

      if (CFG_TOYBOX_I18N && (toys.optflags&FLAG_m)) {
        clen = mbrtowc(&wchar, toybuf+i, len-i, 0);
        if (clen == -1) {
          clen = 1;
        if (clen == -2) break;
        if (clen == 0) clen=1;
        space = iswspace(wchar);
      } else space = isspace(toybuf[i]);

      if (toybuf[i]==10) lengths[0]++;
      if (space) word=0;
      else {
        if (!word) lengths[1]++;

  show_lengths(lengths, name);

void wc_main(void)
  toys.optflags |= (toys.optflags&8)>>1;
  loopfiles(toys.optargs, do_wc);
  if (toys.optc>1) show_lengths(TT.totals, "total");