15 void sighandler(int sig)
20 int main(int argc, char **argv)
24 time_t starttime, endtime;
29 int thisrec, numrecs, numuses, maxrec;
34 fprintf(stderr, "usage: speedrec recfile\n");
38 starttime = endtime = 0;
41 signal(SIGHUP, sighandler);
42 signal(SIGINT, sighandler);
43 signal(SIGTERM, sighandler);
47 if(eof || (datalen >= sizeof(buf)))
50 pfd[0].events = POLLIN;
53 pfd[1].events = POLLOUT;
56 pfd[0].revents = pfd[1].revents = 0;
57 ret = poll(pfd, 2, -1);
58 if((ret < 0) && (errno != EINTR))
60 perror("cannot poll");
63 if(pfd[0].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))
65 ret = read(0, buf + datalen, sizeof(buf) - datalen);
66 if((ret < 0) && (errno != EINTR))
68 perror("cannot read");
77 starttime = time(NULL);
82 if(pfd[1].revents & (POLLOUT | POLLERR | POLLHUP | POLLNVAL))
84 ret = write(1, buf, datalen);
85 if((ret < 0) && (errno != EINTR))
87 perror("cannot write");
90 memmove(buf, buf + ret, datalen -= ret);
92 if(eof && (datalen == 0))
95 if((starttime == 0) || (endtime == 0) || (endtime == starttime))
99 thisrec = (int)(numbytes / ((long long)(endtime - starttime)));
100 if((fd = open(argv[1], O_RDWR | O_CREAT, 0666)) < 0)
105 recfile = fdopen(fd, "r+");
109 if(fscanf(recfile, "%i\n", &numuses) < 1)
111 if(fscanf(recfile, "%i\n", &maxrec) < 1)
113 if(fscanf(recfile, "%i\n", &numrecs) < 1)
115 for(i = 0; i < numrecs; i++)
116 fscanf(recfile, "%i\n", &recs[i]);
119 for(i = 0; i < 4; i++)
120 recs[i] = recs[i + 1];
123 recs[numrecs++] = thisrec;
126 fprintf(recfile, "%i\n", numuses + 1);
127 fprintf(recfile, "%i\n", (thisrec > maxrec)?thisrec:maxrec);
128 fprintf(recfile, "%i\n", numrecs);
129 for(i = 0; i < numrecs; i++)
130 fprintf(recfile, "%i\n", recs[i]);