---
dwmstatus.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 84 insertions(+), 2 deletions(-)
diff --git a/dwmstatus.c b/dwmstatus.c
index 9b2703b..68dd012 100644
--- a/dwmstatus.c
+++ b/dwmstatus.c
_AT_@ -1,5 +1,6 @@
#define _BSD_SOURCE
#include <unistd.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
_AT_@ -48,6 +49,84 @@ settz(char *tzname)
setenv("TZ", tzname, 1);
}
+int
+parse_netdev(unsigned long long int *receivedabs, unsigned long long int *sentabs)
+{
+ char *buf;
+ char *eth0start;
+ static int bufsize;
+ FILE *devfd;
+
+ buf = (char *) calloc(255, 1);
+ bufsize = 255;
+ devfd = fopen("/proc/net/dev", "r");
+
+ // ignore the first two lines of the file
+ fgets(buf, bufsize, devfd);
+ fgets(buf, bufsize, devfd);
+
+ while (fgets(buf, bufsize, devfd)) {
+ if ((eth0start = strstr(buf, "eth0:")) != NULL) {
+
+ // With thanks to the conky project at http://conky.sourceforge.net/
+ sscanf(eth0start + 6, "%llu %*d %*d %*d %*d %*d %*d %*d %llu",\
+ receivedabs, sentabs);
+ free(buf);
+ return 0;
+ }
+ }
+ free(buf);
+ return 1;
+}
+
+char *
+get_netusage()
+{
+ unsigned long long int oldrec, oldsent, newrec, newsent;
+ double downspeed, upspeed;
+ char *downspeedstr, *upspeedstr;
+ char *retstr;
+ int retval;
+
+ downspeedstr = (char *) malloc(15);
+ upspeedstr = (char *) malloc(15);
+ retstr = (char *) malloc(42);
+
+ retval = parse_netdev(&oldrec, &oldsent);
+ if (retval) {
+ fprintf(stdout, "Error when parsing /proc/net/dev file.\n");
+ exit(1);
+ }
+
+ sleep(1);
+ retval = parse_netdev(&newrec, &newsent);
+ if (retval) {
+ fprintf(stdout, "Error when parsing /proc/net/dev file.\n");
+ exit(1);
+ }
+
+ downspeed = (newrec - oldrec) / 1024.0;
+ if (downspeed > 1024.0) {
+ downspeed /= 1024.0;
+ sprintf(downspeedstr, "%.3f MB/s", downspeed);
+ } else {
+ sprintf(downspeedstr, "%.2f KB/s", downspeed);
+ }
+
+ upspeed = (newsent - oldsent) / 1024.0;
+ if (upspeed > 1024.0) {
+ upspeed /= 1024.0;
+ sprintf(upspeedstr, "%.3f MB/s", upspeed);
+ } else {
+ sprintf(upspeedstr, "%.2f KB/s", upspeed);
+ }
+ sprintf(retstr, "down: %s up: %s", downspeedstr, upspeedstr);
+
+ free(downspeedstr);
+ free(upspeedstr);
+ return retstr;
+}
+
char *
mktimes(char *fmt, char *tzname)
{
_AT_@ -100,6 +179,7 @@ main(void)
char *tmar;
char *tmutc;
char *tmbln;
+ char *netstats;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
_AT_@ -111,11 +191,13 @@ main(void)
tmar = mktimes("%H:%M", tzargentina);
tmutc = mktimes("%H:%M", tzutc);
tmbln = mktimes("KW %W %a %d %b %H:%M %Z %Y", tzberlin);
+ netstats = get_netusage();
- status = smprintf("[L: %s|A: %s|U: %s|%s]",
- avgs, tmar, tmutc, tmbln);
+ status = smprintf("[L: %s|N: %s|A: %s|U: %s|%s]",
+ avgs, netstats, tmar, tmutc, tmbln);
setstatus(status);
free(avgs);
+ free(netstats);
free(tmar);
free(tmutc);
free(tmbln);
--
1.8.0.1
--7JfCtLOvnd9MIVvH--
Received on Mon Sep 17 2001 - 00:00:00 CEST
This archive was generated by hypermail 2.3.0 : Thu Dec 06 2012 - 20:48:04 CET