[net.sources] Newline counter for stdio

dave@utcsrgv.UUCP (Dave Sherman) (11/23/83)

The following changes to stdio implement a line counter, which will
tell you how many newlines are transmitted, provided all I/O goes
through stdio. It's useful for predicting Datapac or Telenet packet
charges, among other things.

All you change is two files, filbuf.c and flsbuf.c. You can keep
you own copies of these and compile the .o files with your source
when you need the feature.

The routines you use to look at the data are
	intell(fp)     FILE *fp;
and	outtell(fp)     FILE *fp;

They return, respectively, the number of newlines read from and written
on the file pointer. A useful macro is:

#define CRLIST(iop)  "FD %d: %4d newlines in, %4d newlines out", fileno(iop), intell(iop), outtell(iop)

This can be used as argument to printf, sprintf or fprintf, e.g.
	printf(CRLIST(stdin));

I have not posted the entire source files, since they are part of
Bell Labs proprietary source. Here are the diff files from vanilla
v7 stdio. (If you have a different stdio, it should be pretty
obvious where the changes go.)

++++++++++++++++++++++++++
filbuf.c:
++++++++++++++++++++++++++
3,4d2
< static int incount[_NFILE] = 0;
< 
13d10
< 	register i;
45,60d41
< 
< 	/* Dave Sherman mod - count newlines going by */
< 	if(iop->_flag&_IONBF)
< 	{
< 		if(*iop->_ptr == '\n')
< 			incount[iop-_iob]++;
< 	}
< 	else
< 	{
< 		/* the <= is needed here because _cnt was decremented above! */
< 		for(i=0; i<=iop->_cnt; i++)
< 			if(*(iop->_ptr+i) ==  '\n')
< 				incount[iop-_iob]++;
< 	}
< 	/* end DS mod */
< 
62,67d42
< }
< 
< intell(iop)	/* tell how many input newlines */
< 	register FILE *iop;
< {
< 	return(incount[iop-_iob]);
++++++++++++++++++++++++++
flsbuf.c:
++++++++++++++++++++++++++
3,4d2
< static int outcount[_NFILE] = 0;
< 
16d13
< 	int i;
28,30d24
< 		/* Dave Sherman mod: count newlines */
< 		if(c1 == '\n')
< 			outcount[iop-_iob]++;
52,54d45
< 			for(i=0; i<n; i++)
< 				if(base[i]=='\n')
< 					outcount[iop-_iob]++;
72,73c63
< 	register i;
< 	int n;
---
> 	register n;
83,85d72
< 		for(i=0; i<n; i++)
< 			if(base[i]=='\n')
< 				outcount[iop-_iob]++;
124,129d110
< }
< 
< outtell(iop)
< 	register FILE *iop;
< {
< 	return(outcount[iop-_iob]);

====================================================================

Dave Sherman
-- 
 {allegra,cornell,decvax,ihnp4,linus,utzoo}!utcsrgv!dave