/* -*- mode: c -*- * $Id: wc.gcc,v 1.4 2001/05/24 20:55:35 doug Exp $ * http://www.bagley.org/~doug/shootout/ * * this program is modified from: * http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html * Timing Trials, or, the Trials of Timing: Experiments with Scripting * and User-Interface Languages</a> by Brian W. Kernighan and * Christopher J. Van Wyk. * */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define IN 1 #define OUT 0 int main() { int i, c, nl, nw, nc, state, nread; char buf[4096]; state = OUT; nl = nw = nc = 0; while ((nread = read(0, buf, sizeof(buf))) > 0) { nc += nread; for (i=0; i<nread; i++) { c = buf[i]; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } } printf("%d %d %d\n", nl, nw, nc); return(0); }