I'm trying to rewrite a duplicate of wc -l
that displays partial results, as it receives input (for example,
My current version is a simple
while(!feof(in) &&
//(readc=fread(buf, 1,BUFSIZE,in))) {
(readc=read(0,buf, BUFSIZE))) {
for(i=0;i<readc;i++) {
lines += (buf[i] == '\n');
}
}
The problem is that my stdin
is still getting block-buffered. The entire point of this exercise is to have output not have to wait for each 4KB block to fill. I suppose line-buffering would be fine.
Example application: find | partial_wc
awk 'NR%1000==0 {printf "%d\r",NR} END {print NR}'
has a similar output, except that I would like to choose to output based on time (every 1s, for example), rather than rows. Also, it's an interesting learning question.
I tried taking the advice given in why grep is fast, but can't figure out which set of system calls to use.