/* wcw.c -- to count the lines, words, and bytes in a file */ /* (added 5/22/90: also find the length of the longest line */ /* This code by William F. Hammond; hammond@math.albany.edu */ #include "stdio.h" #define OSTRSZ 79 #define TABSIZE 3 #define LWIDTH 3 #define BSPCH 0x08 #define FFCH 0x0c #define ESCCH 0x1b #define CSICH 0x9b #define BLOASC 0x21 #define LITASC 0x40 #define ELOASC 0x7f #define BHIASC 0xa0 #define EHIASC 0xff #define MAX(a,b) ((a)>(b)?(a):(b)) FILE *filearg; static char ostr[OSTRSZ + 1]; static char ltstr[20]; main(argc, argv) int argc; char **argv; { long bcount, lcount, wcount, wide, maxwide, xpos; register int chx; register FILE *stream; char *filename; int wflag, escflag, csiflag, lflag, ltlen, dtlen, jj; if((argc ==2)&&((strcmp(argv[1], "?")==0)|| (strcmp(argv[1], "-h")==0)||(strcmp(argv[1], "h")==0))) { syntax(argv[0]); exit(0); } if(argc > 3) { syntax(argv[0]); exit(5); } if((argc ==3)&&(strcmp(argv[1], "-l"))) { syntax(argv[0]); exit(5); } lflag = 0; /* flag for long output (the width of each line) */ filename = (char *)NULL; if(argc == 3) { lflag = 1; filename = argv[2]; } if((argc == 2)) { if(strcmp(argv[1],"-l")) { filename = argv[1]; } else lflag = 1; } stream = stdin; filearg = NULL; if(filename) { filearg = fopen(filename, "r"); if (filearg == NULL) { strcpy(ostr, "Cannot open the file \""); strcat(ostr, filename); strcat(ostr, "\" for input.\n"); fputs(ostr, stderr); exit(6); } else stream = filearg; } bcount = 0L; lcount = 0L; wcount = 0L; xpos = 0L; wide = 0L; maxwide = 0L; ltlen = 0; dtlen = 0; wflag = 0; /* 1 if currently building a word */ escflag = 0; /* 1 if last character was an "escape" = ESCCH */ csiflag = 0; /* 1 if "control sequence inducer" governs */ while ( ( chx = getc(stream) ) != EOF ) { bcount ++; /* First, the word-ending characters */ if( (chx == ' ') || (chx == '\n') || (chx == '\t') || (chx == FFCH) ) { if( (chx == '\n') || (chx == FFCH) ) /* line-enders */ { lcount ++; maxwide = (MAX(maxwide, wide)); if(lflag) { ltoa(ltstr, wide, 10); ltlen = strlen(ltstr); if(ltlen > LWIDTH) { for(jj=0; jj=0; jj--) { ltstr[jj+dtlen]=ltstr[jj]; } for(jj=0; jj= LITASC)&&(chx <= ELOASC)) { csiflag = 0; continue; } escflag = 0; if((chx == ESCCH)&&(csiflag == 0)) { escflag = 1; continue; } if(csiflag) continue; if(chx == BSPCH) { xpos --; } if(((BLOASC <= chx) && (chx <= ELOASC))|| ((BHIASC <= chx) && (chx <= EHIASC))) { wflag = 1; xpos ++; } } wide = MAX(wide, xpos); } /* end of while chx loop */ if(!lflag) /* Omit summary info when in long output mode */ { ltoa(ltstr, lcount, 10); strcpy(ostr, ltstr); strcat(ostr, " lines, "); ltoa(ltstr, wcount, 10); strcat(ostr, ltstr); strcat(ostr, " words, "); ltoa(ltstr, bcount, 10); strcat(ostr, ltstr); strcat(ostr, " bytes. Width: "); ltoa(ltstr, maxwide, 10); strcat(ostr, ltstr); strcat(ostr, "\n"); fputs(ostr, stdout); } if(filearg) fclose(filearg); exit(0); } syntax(xstr) char *xstr; { strcpy(ostr, "Syntax: "); strcat(ostr, xstr); strcat(ostr, " [-l] file -- OR -- "); strcat(ostr, xstr); strcat(ostr, " < file [-l]\n"); fputs(ostr, stderr); }