Aztec C source - wc.c
Dave Newkirk
dcn at ihuxl.UUCP
Tue Nov 15 00:24:24 AEST 1983
/* wc - word count */
#include "stdio.h"
int tl, tw, tc ;
int lines, words, chars ;
main(argc, argv)
int argc ;
char *argv[] ;
{
char *s ;
FILE *input ;
int fcount ;
tl = tw = tc = 0 ;
lines = words = chars = FALSE ;
fcount = 0 ;
while( --argc>0 && **++argv == '-' )
for( s=&argv[0][1] ; *s != EOS ; s++ )
switch( *s ) {
case 'l':
lines = TRUE ;
break ;
case 'w':
words = TRUE ;
break ;
case 'c':
chars = TRUE ;
break ;
default:
fprintf(stderr, "wc: unknown option %c\n", *s ) ;
argc = -1 ;
break ;
}
if( argc < 0 ) {
fprintf(stderr, "usage: wc -lwc files\n");
exit(1) ;
}
if( !words && !lines && !chars )
lines = words = chars = TRUE ;
if( argc == 0 )
wc( stdin, "" ) ;
else
for( ; argc>0 ; argc--,argv++)
if( (input=fopen(*argv,"r")) == NULL ) {
fprintf(stderr, "wc: can't open %s\n", *argv) ;
exit(1) ;
}
else {
wc( input, *argv ) ;
fcount++ ;
fclose( input ) ;
}
if( fcount > 1 ) {
if( lines )
printf("%d ", tl ) ;
if( words )
printf("%d ", tw ) ;
if( chars )
printf("%d ", tc ) ;
printf("total\n") ;
}
exit(0) ;
} /* end main */
/* wc - count lines, words & chars */
wc( in, fname )
FILE *in ;
char *fname ;
{
int c, nl, nw, nc, inword ;
inword = FALSE ;
nl = nw = nc = 0 ;
while( (c=agetc(in)) != EOF ) {
nc++ ;
if( c == NL )
nl++ ;
if( c == BLANK || c == NL || c == TAB )
inword = FALSE ;
else if( ! inword ) {
inword = TRUE ;
nw++ ;
}
}
tl += nl ; tw += nw ; tc += nc ;
if( lines )
printf("%d ", nl ) ;
if( words )
printf("%d ", nw ) ;
if( chars )
printf("%d ", nc ) ;
printf( "%s\n", fname ) ;
} /* end wc */
More information about the Comp.sources.unix
mailing list