wmnet: simplify updateStats_dev() a bit
There is no need to have a variable holding the interface name and doing the exercise of stripping the trailing colon. It is enough to just compare the line to check whether it contains the device name. And only if it does we sscanf() its contents.
This commit is contained in:
parent
cd4947f139
commit
49944a01ae
|
@ -316,36 +316,25 @@ int updateStats_ipchains(void) {
|
||||||
|
|
||||||
int updateStats_dev(void) {
|
int updateStats_dev(void) {
|
||||||
FILE *dev;
|
FILE *dev;
|
||||||
char *ptr;
|
|
||||||
unsigned int flag = 0;
|
|
||||||
static char interface[16];
|
|
||||||
rx = False;
|
rx = False;
|
||||||
tx = False;
|
tx = False;
|
||||||
|
|
||||||
|
dev = fopen("/proc/net/dev", "r");
|
||||||
if ((dev = fopen("/proc/net/dev", "r")) == NULL) {
|
if (!dev) {
|
||||||
fprintf(stderr, "/proc/net/dev does not exist?\n"
|
fprintf(stderr, "/proc/net/dev does not exist\n");
|
||||||
"Perhaps we are not running Linux?\n");
|
|
||||||
exit(4);
|
exit(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* the first two lines we can skip */
|
/* the first two lines we can skip */
|
||||||
fgets(buffer, 256, dev);
|
fgets(buffer, 256, dev);
|
||||||
fgets(buffer, 256, dev);
|
fgets(buffer, 256, dev);
|
||||||
|
|
||||||
/* IP Chain Rules for Linux kernel 2_1.x */
|
while(fgets(buffer, 256, dev)) {
|
||||||
while(flag != (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND) && fgets(buffer, 256, dev)) {
|
|
||||||
|
|
||||||
sscanf(buffer, "%16s %llu %llu %*d %*d %*d %*d %*d %*d %llu %llu %*d %*d %*d %*d %*d %*d",
|
if (strcmp(buffer, device) > 0){
|
||||||
interface, &totalbytes_in, &totalpackets_in, &totalbytes_out, &totalpackets_out);
|
|
||||||
|
|
||||||
/* strip trailing colon */
|
sscanf(buffer, "%*s %llu %llu %*d %*d %*d %*d %*d %*d %llu %llu %*d %*d %*d %*d %*d %*d",
|
||||||
ptr = interface;
|
&totalbytes_in, &totalpackets_in, &totalbytes_out, &totalpackets_out);
|
||||||
while(*ptr != ':') ptr++;
|
|
||||||
*ptr = '\0';
|
|
||||||
|
|
||||||
if (!strcmp(interface, device)) {
|
|
||||||
|
|
||||||
flag = (ACCOUNT_IN_FOUND|ACCOUNT_OUT_FOUND);
|
|
||||||
|
|
||||||
if (totalpackets_in != lastpackets_in) {
|
if (totalpackets_in != lastpackets_in) {
|
||||||
diffbytes_in += totalbytes_in - lastbytes_in;
|
diffbytes_in += totalbytes_in - lastbytes_in;
|
||||||
|
@ -360,6 +349,8 @@ int updateStats_dev(void) {
|
||||||
lastbytes_out = totalbytes_out;
|
lastbytes_out = totalbytes_out;
|
||||||
tx = True;
|
tx = True;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue