summaryrefslogtreecommitdiff
path: root/output.c
blob: c0c36c2f77ba74a1f4ecd0a20af7d8bcb4fcc1d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*	$Id$	*/
/*
 * Copyright (c) 2004 demon <demon@vhost.dymdns.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <curses.h>
#include "main.h"
#define XPOS 10
#define	YPOS 5
#define ASTAT 30

u_long delta(u_long, u_long *);
void av(u_long, u_long, u_int);
u_int getmax(u_long *);

u_long ani[ASTAT];
u_long ano[ASTAT];
u_long api[ASTAT];
u_long apo[ASTAT];
u_long ilast = 0, olast = 0;

int out(struct ifdata *diff) {
    extern struct ifdata ifdata;
    u_long dli, dlo;
    WINDOW *scr;

    move(0,0);

    mvprintw(YPOS, XPOS,
	"IF: %s\n", ifdata.xname);
    mvprintw(YPOS, XPOS + 25,
	"BR: %d Mbps\n",  ifdata.baudrate / 1000000);
/*
    mvprintw(YPOS + 2, XPOS,
	"RX: %3lu kBps\n", diff->ibytes / 1024);
    mvprintw(YPOS + 3, XPOS,
	"RA: %3i kBps\n", delta(diff->ibytes, ani) / 1024);

    mvprintw(YPOS + 2, XPOS + 25,
	"TX: %3lu kBps\n", diff->obytes / 1024);
    mvprintw(YPOS + 3, XPOS + 25,
	"TA: %3i kBps\n", delta(diff->obytes, ano) / 1024);

    mvprintw(YPOS + 5, XPOS,
	"RX: %3lu pps\n", diff->ipackets);
    mvprintw(YPOS + 6, XPOS,
	"RA: %3lu pps\n", delta(diff->ipackets, api));

    mvprintw(YPOS + 5, XPOS + 25,
	"TX: %3lu pps\n", diff->opackets);
    mvprintw(YPOS + 6, XPOS + 25,
	"TA: %3lu pps\n", delta(diff->opackets, apo));

    mvprintw(YPOS + 8, XPOS,
	"RX: %lu MB\n", ifdata.ibytes / (1024 * 1024));
    mvprintw(YPOS + 8, XPOS + 25,
	"TX: %lu MB\n", ifdata.obytes / (1024 * 1024));
*/    

    dli = delta(diff->ibytes, ani);
    dlo = delta(diff->obytes, ano);

    mvprintw(YPOS + 2, XPOS,
	"/0   /.1  /.2  /.3  /.4  /.5  /.6  /.7  /.8  /.9  /1 x %3lu kBps",
	getmax(ani) / 1024);
    mvprintw(YPOS + 3, XPOS - 3,
	"RX ---------------------------------------------------    %3lu kBps",
	diff->ibytes / 1024);
    move(YPOS + 3, XPOS);
    av(diff->ibytes / 1024, ilast, getmax(ani) / 1024);

    mvprintw(YPOS + 8, XPOS,
	"/0   /.1  /.2  /.3  /.4  /.5  /.6  /.7  /.8  /.9  /1 x %3lu kBps",
	getmax(ano) / 1024);
    mvprintw(YPOS + 9, XPOS - 3,
    	"TX ---------------------------------------------------    %3lu kBps",
	diff->obytes / 1024);
    move(YPOS + 9, XPOS);
    av(diff->obytes / 1024, olast, getmax(ano) / 1024);

    ilast = diff->ibytes / 1024;
    olast = diff->obytes / 1024;

    refresh();
}

u_long delta(u_long n, u_long *an) {
    u_long nsum = 0;
    int i, del = ASTAT;
    for (i=ASTAT - 1; i>0; i--) {
	an[i] = an[i-1];
	nsum += an[i];
	if (an[i] == 0)
	    del -= 1;
    }
    an[0] = n;
    nsum += n;
    return (nsum / del);
}

void av(u_long bytes, u_long avbytes, u_int max) {
    u_int b, avb, i, min;
    if (max != 0) {
	b = bytes * 50 / max;
	avb = avbytes * 50 / max;
	min = ((bytes < avbytes)?bytes:avbytes) * 50 / max;
	for (i = 0; i < min; i++)
	    addch('=');
	if (b > avb)
    	    for (i = min; i < b; i++)
		addch(ACS_RARROW);
	else
    	    for (i = min; i < avb; i++)
		addch(ACS_LARROW);
    }
}

u_int getmax(u_long *an) {
    int i;
    u_int max = 0;
    for (i=0; i<ASTAT; i++) {
	if(an[i] > max)
	    max = an[i];
    }
    return max;
}