commit ed32ab8516670b9692c11ce96dba52e8c8981620
Author: Silvan Jegen <s.jegen_AT_gmail.com>
Date: Thu Dec 6 21:01:51 2012 +0100
Add the netusage helper function for dwmstatus
diff --git a/dwm.suckless.org/dwmstatus/dwmstatus-netusage.c b/dwm.suckless.org/dwmstatus/dwmstatus-netusage.c
new file mode 100644
index 0000000..68dd012
--- /dev/null
+++ b/dwm.suckless.org/dwmstatus/dwmstatus-netusage.c
_AT_@ -0,0 +1,211 @@
+#define _BSD_SOURCE
+#include <unistd.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <string.h>
+#include <strings.h>
+#include <sys/time.h>
+#include <time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <X11/Xlib.h>
+
+char *tzargentina = "America/Buenos_Aires";
+char *tzutc = "UTC";
+char *tzberlin = "Europe/Berlin";
+
+static Display *dpy;
+
+char *
+smprintf(char *fmt, ...)
+{
+ va_list fmtargs;
+ char *ret;
+ int len;
+
+ va_start(fmtargs, fmt);
+ len = vsnprintf(NULL, 0, fmt, fmtargs);
+ va_end(fmtargs);
+
+ ret = malloc(++len);
+ if (ret == NULL) {
+ perror("malloc");
+ exit(1);
+ }
+
+ va_start(fmtargs, fmt);
+ vsnprintf(ret, len, fmt, fmtargs);
+ va_end(fmtargs);
+
+ return ret;
+}
+
+void
+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.
");
+ exit(1);
+ }
+
+ sleep(1);
+ retval = parse_netdev(&newrec, &newsent);
+ if (retval) {
+ fprintf(stdout, "Error when parsing /proc/net/dev file.
");
+ 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)
+{
+ char buf[129];
+ time_t tim;
+ struct tm *timtm;
+
+ bzero(buf, sizeof(buf));
+ settz(tzname);
+ tim = time(NULL);
+ timtm = localtime(&tim);
+ if (timtm == NULL) {
+ perror("localtime");
+ exit(1);
+ }
+
+ if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) {
+ fprintf(stderr, "strftime == 0
");
+ exit(1);
+ }
+
+ return smprintf("%s", buf);
+}
+
+void
+setstatus(char *str)
+{
+ XStoreName(dpy, DefaultRootWindow(dpy), str);
+ XSync(dpy, False);
+}
+
+char *
+loadavg(void)
+{
+ double avgs[3];
+
+ if (getloadavg(avgs, 3) < 0) {
+ perror("getloadavg");
+ exit(1);
+ }
+
+ return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
+}
+
+int
+main(void)
+{
+ char *status;
+ char *avgs;
+ char *tmar;
+ char *tmutc;
+ char *tmbln;
+ char *netstats;
+
+ if (!(dpy = XOpenDisplay(NULL))) {
+ fprintf(stderr, "dwmstatus: cannot open display.
");
+ return 1;
+ }
+
+ for (;;sleep(90)) {
+ avgs = loadavg();
+ 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|N: %s|A: %s|U: %s|%s]",
+ avgs, netstats, tmar, tmutc, tmbln);
+ setstatus(status);
+ free(avgs);
+ free(netstats);
+ free(tmar);
+ free(tmutc);
+ free(tmbln);
+ free(status);
+ }
+
+ XCloseDisplay(dpy);
+
+ return 0;
+}
+
diff --git a/dwm.suckless.org/dwmstatus/index.md b/dwm.suckless.org/dwmstatus/index.md
index d905f61..1f1901e 100644
--- a/dwm.suckless.org/dwmstatus/index.md
+++ b/dwm.suckless.org/dwmstatus/index.md
_AT_@ -30,6 +30,7 @@ add them here as file or as code example.
* [Support for ACPI battery status Linux](new-acpi-battery.c)
* [Reading out a temperature from /sys](dwmstatus-temperature.c)
+* [Reading eth0 up-, and downspeed from /proc/net](dwmstatus-netusage.c)
Questions
---------
Received on Thu Dec 06 2012 - 21:04:55 CET