home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
WC2.ZIP
/
WC.C
< prev
next >
Wrap
Text File
|
1991-03-24
|
2KB
|
69 lines
/*
* Name: wc.c
* Synopsis: wc < -lwc > < file_name ... >
* Description: This program is modeled after the UNIX "wc" program.
* Input is from STDIN or from file_name. The program
* counts the lines, words, and characters in the file
* and displays the results to the console. The default
* is -lwc and displays in that order, but any order can
* be chosen.
*
* Options are:
* l - line count
* w - word count
* c - character count
*/
#include <stdio.h>
#define IN 1
#define OUT 0
char *defopts = "lwc";
long i, c, lines, words, chars, state;
char *opts, *p1;
FILE *fdi;
int f_ctr;
main(int argc, char *argv[], char *envp[])
{
f_ctr = argc;
opts = defopts;
for (i=1;i<argc;i++) {
if (*argv[i] == '-') {
opts = argv[i];
opts++;
f_ctr--;
}
else {
if( fdi = fopen(argv[i],"r")) lwc_file(argv[i]);
else printf("Could not open %s.\n",argv[i]);
}
}
if (f_ctr == 1) {
fdi = stdin;
lwc_file("");
}
}
lwc_file(char *file_name)
{
state = OUT;
lines = words = chars = 0;
while ((c = fgetc(fdi)) != EOF) {
++chars;
if (c == '\n') ++lines;
if (c == ' ' || c == '\n' || c == '\t') state = OUT;
else if (state == OUT) {
state = IN;
++words;
}
}
for (p1=opts;*p1;p1++) {
if (*p1 == 'l') printf("%10ld",lines);
else if(*p1 == 'w') printf("%10ld",words);
else if(*p1 == 'c') printf("%10ld",chars);
}
printf(" %s\n", file_name);
if (fdi != stdin) fclose(fdi);
}