From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/wc.c | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/wc.c (limited to 'reference/C/CONTRIB/SNIP/wc.c') diff --git a/reference/C/CONTRIB/SNIP/wc.c b/reference/C/CONTRIB/SNIP/wc.c new file mode 100755 index 0000000..62bc276 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/wc.c @@ -0,0 +1,66 @@ +/* + File wc.c - a sample word count program + Written and submitted to public domain by Jay Elkes + April, 1992 +*/ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + FILE *infileptr; + char infile[80]; + + long int nl = 0; + long int nc = 0; + long int nw = 0; + + int state = 0; + const int NEWLINE = '\n'; + int c; + +/* The program name itself is the first command line arguement so we + ignore it (argv[0]) when showing user entered parameters. */ + + switch (argc - 1) + { + case (0): + printf("no parameters\n"); + return 12; + case (1): + break; + default: + printf("too many parameters\n"); + return 12; + } + + strcpy(infile,argv[1]); + + infileptr = fopen(infile,"rb"); + if (infileptr == NULL) + { + printf("Cannot open %s\n",infile); + return 12; + } + + while ((c = getc(infileptr)) != EOF) + { + ++nc; + if (c == NEWLINE) + ++nl; + if (isspace(c)) + state = 0; + else if (state == 0) + { + state = 1; + ++nw; + } + } + + /* Final Housekeeping */ + + printf("%ld Lines, %ld Words, %ld Characters", nl, nw, nc); + return 0; +} -- cgit v1.2.3-54-g00ecf