00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "assert.h"
00005 #include "xnet.h"
00006
00007 static time_t times[2];
00008 static unsigned long long bytes[2];
00009
00010 int __GetNetPoint()
00011 {
00012 FILE *F1 = NULL;
00013 char iface[1025] = { '\0' };
00014 char line[1025] = { '\0' };
00015 char *word = NULL;
00016 unsigned long long iface_bytes[2] = { 0 };
00017 unsigned long long iface_byte_total = 0;
00018 int skip = 0;
00019
00020 F1 = fopen("/proc/net/dev", "r");
00021 if (F1 == NULL) {
00022 perror ("/proc/net/dev\n");
00023 return(1);
00024 }
00025
00026 fgets(line, 1024, F1);
00027 fgets(line, 1024, F1);
00028
00029 while (!feof(F1)) {
00030 fgets(line, 1024, F1);
00031
00032 if (feof(F1))
00033 break;
00034
00035 word = strtok(line, ": ");
00036 if (word != NULL) {
00037 strcpy(iface, word);
00038 }
00039 if ((!xnet_resources.interface) && !strcasecmp(iface,"lo")) {
00040 continue;
00041 }
00042
00043 word = strtok(NULL, " ");
00044 if (word != NULL) {
00045 sscanf(word, "%llu", &iface_bytes[0]);
00046 }
00047
00048 for (skip = 0; skip < 7; skip++) {
00049 word = strtok(NULL, " ");
00050 }
00051
00052 word = strtok(NULL, " ");
00053 if (word != NULL) {
00054 sscanf(word, "%llu", &iface_bytes[1]);
00055 }
00056
00057 if (!xnet_resources.interface) {
00058 iface_byte_total += iface_bytes[0];
00059 iface_byte_total += iface_bytes[1];
00060 }
00061 else {
00062 int found = 0;
00063
00064 word = strtok(xnet_resources.interface, ", \t");
00065 while (word != NULL) {
00066 if (!strcasecmp(word,iface)) {
00067 iface_byte_total += iface_bytes[0];
00068 iface_byte_total += iface_bytes[1];
00069 }
00070 word = strtok(NULL, ", \t");
00071 }
00072 }
00073 }
00074 fclose(F1);
00075
00076 bytes[0] = bytes[1];
00077 bytes[1] = iface_byte_total;
00078 times[0] = times[1];
00079 times[1] = time(NULL);
00080
00081
00082
00083 return(0);
00084 }
00085
00086 void InitNetPoint()
00087 {
00088 times[0] = 0;
00089 times[1] = 0;
00090 bytes[0] = 0;
00091 bytes[1] = 0;
00092 __GetNetPoint();
00093 __GetNetPoint();
00094 }
00095
00096 void GetNetPoint( w, closure, call_data )
00097 Widget w;
00098 XtPointer closure;
00099 XtPointer call_data;
00100 {
00101 unsigned long long ull_value = 0;
00102 unsigned long long ull_tdiff = 0;
00103 int i_value = 0.0;
00104 int i_tdiff = 0.0;
00105
00106 ASSERT(w != NULL);
00107 ASSERT(call_data != NULL);
00108
00109 __GetNetPoint();
00110
00111 ASSERT(w != NULL);
00112
00113 if (bytes[1] > bytes[0]) {
00114 ull_value = bytes[1] - bytes[0];
00115 }
00116 else {
00117 ull_value = bytes[0] - bytes[1];
00118 }
00119
00120
00121 if (times[1] > times[0]) {
00122 ull_tdiff = times[1] - times[0];
00123 }
00124 else {
00125 ull_tdiff = times[0] - times[1];
00126 }
00127
00128 if (ull_tdiff == 0) {
00129 *(int *)call_data = 0;
00130 ASSERT(w != NULL);
00131 return;
00132 }
00133
00134 i_tdiff = (int)ull_tdiff;
00135
00136
00137 i_value = (int)ull_value;
00138
00139
00140 i_value /= i_tdiff;
00141
00142
00143
00144
00145 *(int *)call_data = i_value;
00146
00147 ASSERT(w != NULL);
00148 }
00149