[net.sources] Apple AZTEC wc.c

jed@mb2c.UUCP (John E. Duncan III) (12/16/83)

/* wc - word count */

/* Apple II AZTEC C System version.  Wherin we do big blocking on input
 * since the system won't do it for us, and we avoid STDIO so that the
 * program can be re-'run'.
 */

#define TRUE 1
#define FALSE 0
#define BLANK ' '
#define TAB '\t'
#define NULL 0
#define READ 0
#define STDIN 0
#define STDOUT 1
#define STDERR 2
#define BUFSIZ 30000

long int tl, tw, tc;
int lines, words, chars;
char unknown[] = "wc: Unknown option, x\n";

main(argc, argv)
	int argc;
	char *argv[];
{
	char *s;
	register int input;		/* file number */
	int fcount;

	tl = tw = tc = 0;
	lines = words = chars = FALSE;
	fcount = 0;

	while( --argc>0 && **++argv == '-' )
		for( s=&argv[0][1]; *s != NULL; s++ )
			switch( *s ) {
				case 'l':
					lines = TRUE;
					break;
				case 'w':
					words = TRUE;
					break;
				case 'c':
					chars = TRUE;
					break;
				default:
					unknown[20] = *s;
					emsg( unknown );
					argc = -1;
					break;
			}

	if( argc < 0 ) {
		emsg( "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=open(*argv, READ)) < 0 ) {
				emsg( "wc: Can't open " );
				emsg( *argv );
				emsg( "\n" );
			}
			else {
				wc( input, *argv );
				fcount++;
				close( input );
			}

	if( fcount > 1 ) {
		if( lines ) {
			dwrite( tl );
			mesg( "\t" );
		}
		if( words ) {
			dwrite( tw );
			mesg( "\t" );
		}
		if( chars ) {
			dwrite( tc );
			mesg( "\t" );
		}
		mesg( "total\n" );
	}	
	exit(0);

} /* end main */

/* wc - count lines, words & chars */

wc( in, fname )
	int *in;
	char *fname;
{
	char buf[BUFSIZ];
	register char *ptr, *endbuf;
	register char c, inword;
	register long int nl, nw, nc;
	static unsigned int incnt;

	inword = FALSE;
	nl = nw = nc = 0;

	while( incnt = read( in, buf, BUFSIZ )) {
		endbuf = buf + incnt;
		ptr = buf - 1;
		while( ++ptr < endbuf ) {
			c = *ptr;
			++nc;
			if( c == '\r' || c == '\n' ) {
				++nl;
				inword = FALSE;
				continue;
			}
			if( c == BLANK || c == TAB ) {
				inword = FALSE;
				continue;
			}
			if( ! inword ) {
				inword = TRUE;
				++nw;
			}
		}
	}
	if( incnt < 0 ) {
		emsg( "wc: Input error, file '" );
		emsg( fname );
		emsg( "'\n" );
		exit( 3 );
	}

	tl += nl ; tw += nw ; tc += nc ;

	if( lines ) {
		dwrite( nl );
		mesg( "\t" );
	}
	if( words ) {
		dwrite( nw );
		mesg( "\t" );
	}
	if( chars )	{
		dwrite( nc );
		mesg( "\t" );
	}
	mesg( fname );
	mesg( "\n" );

} /* end wc */

emsg( str )
	char *str;
{
	write( STDERR, str, strlen( str ));
}

mesg( str )
	char *str;
{
	write( STDOUT, str, strlen( str ));
}

/* dwrite
 *   Print out the ascii representation of a long integer and avoid
 * dragging in 'printf' in the process.
 */
dwrite( num )
	register long num;
{
	register long int i;
	int digit;

	if( (i = num/10) != 0)
		dwrite( i );
	digit = num%10 + '0';
	write( STDOUT, &digit, 1 );
}