1 /*
2 vrerror.c - Error reporing functions
3 Copyright (C) 2001  Jeff Carneal
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19 
20 #include "config.h"
21 #include <stdio.h>
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include "progress.h"
25 
26 PM pmeter;
27 
pm_showmeter(int mode)28 void pm_showmeter(int mode) {
29 	char buf[128];
30 	long lastupdate = 0;
31 	float kdone = 0;
32 	float percent = 0;
33 	int elapsed_time = 0;
34 	int done_diff = 0;
35 	int kleft = 0;
36 	float kbps = 100;
37 	float secsleft = 0;
38 	int mins = 0;
39 	int secs = 0;
40 
41 	memset(buf,0,128);
42 
43 	lastupdate = pmeter.current.tv_sec;
44 	gettimeofday(&pmeter.current, NULL);
45 	elapsed_time = pmeter.current.tv_sec - lastupdate;
46 
47 	done_diff = pmeter.bytesdone - pmeter.lastbytesdone;
48 	if(elapsed_time)
49 		kbps = (float)(done_diff / (float)elapsed_time / 1024);
50 	kdone = pmeter.bytesdone/1024;
51 	percent = (kdone/pmeter.totalkbytes)*100;
52 
53 	kleft = pmeter.totalkbytes - (int)kdone;
54 	if(kbps)
55 		secsleft = (int)(kleft / kbps);
56 
57 	mins = (int)(secsleft / 60);
58 	secs = (int)secsleft % 60;
59 
60 	switch(mode) {
61 		case PM_START:
62 			snprintf(buf, 128, "\n");
63 
64 		case PM_UPDATE:
65 			snprintf(buf, 128, "\r -- [%dk/%dk] | %5.2f KB/s | %3d%% | %02d:%02ds ETA --", (int)kdone, pmeter.totalkbytes, kbps, (int)percent, mins, secs);
66 			fprintf(stderr, "%s", buf);
67 			break;
68 
69 		case PM_END:
70 			snprintf(buf, 128, "\r -- [%dk/%dk] | %5.2f KB/s | 100%% | DONE --          ", pmeter.totalkbytes, pmeter.totalkbytes, kbps);
71 			fprintf(stderr, "%s", buf);
72 			secsleft = pmeter.current.tv_sec - pmeter.start.tv_sec;
73 			mins = (int)(secsleft / 60);
74 			secs = (int)secsleft % 60;
75 			fprintf(stderr, "\nElapsed time:  %02d:%02d\n\n", mins, secs);
76 	}
77 	pmeter.lastbytesdone = pmeter.bytesdone;
78 
79 }
80 
pm_updatemeter(void)81 void pm_updatemeter(void) {
82 	alarm(1);
83 	pm_showmeter(PM_UPDATE);
84 }
85 
86