Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utcsrgv.UUCP Path: utzoo!utcsrgv!dave From: dave@utcsrgv.UUCP (Dave Sherman) Newsgroups: net.sources,net.unix Subject: Newline counter for stdio Message-ID: <2798@utcsrgv.UUCP> Date: Wed, 23-Nov-83 12:36:14 EST Article-I.D.: utcsrgv.2798 Posted: Wed Nov 23 12:36:14 1983 Date-Received: Wed, 23-Nov-83 16:11:10 EST Organization: The Law Society of Upper Canada, Toronto Lines: 96 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 register n; 83,85d72 < for(i=0; i