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