Commits

William Giokas committed 3e9318f

Added in ip addr stuff

only for ipv4.
took out std=c99 stuff that didn't let me use the networking function
from i3status.

Signed-off-by: William Giokas <1007380@gmail.com>

Comments (0)

Files changed (2)

 
 # flags
 CPPFLAGS = -DVERSION=\"${VERSION}\"
-CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
+CFLAGS = -g -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
+#CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${INCS} ${CPPFLAGS}
 #CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
 LDFLAGS = -g ${LIBS}
 #LDFLAGS = -s ${LIBS}
 #include <dirent.h>
 #include <sys/wait.h>
 
+/* i3 */
+#include <stdbool.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include <netdb.h>
+#include <ifaddrs.h>
+#include <net/if.h>
+/* end i3 */
 #include <X11/Xlib.h>
 
 /*char *tzutc = "UTC";*/
 /* END BATTERY USAGE
  *
  * NETWORK STUFF
- * TODO ip address display (need new func, not in net/dev)
- * TODO connection status (Put in ip address func?)
+ * DONE ip address display (need new func, not in net/dev)
+ * TODO KINDA DONE: connection status (Put in ip address func?)
  * DONE make more modular (for use with wlan0/eth0/others)
  */
 int 
 	return retstr;
 }
 
+/* All taken from i3status. Credit should not go to me...I am still learning
+ * and this was all pretty shamelessly copy/pasted from i3status/src/print_ip_addr.c
+ */
+char *
+get_ip_addr(const char *interface)
+{
+    static char part[512];
+    socklen_t len = sizeof(struct sockaddr_in);
+    memset(part, 0, sizeof(part));
+    struct ifaddrs *ifaddr, *addrp;
+    bool found = false;
+
+    getifaddrs(&ifaddr);
+
+    if (ifaddr == NULL)
+        return NULL;
+
+    /* Skip till we are at the AF_INET address of the interface */
+    for (addrp = ifaddr;
+            (addrp != NULL &&
+             (strcmp(addrp->ifa_name, interface) != 0 ||
+              addrp->ifa_addr == NULL ||
+              addrp->ifa_addr->sa_family != AF_INET));
+            addrp = addrp->ifa_next) {
+        /* check if interface is down/up */
+        if (strcmp(addrp->ifa_name, interface) != 0)
+            continue;
+        found = true;
+        if (strcmp(addrp->ifa_name, interface) != 0) {
+            freeifaddrs(ifaddr);
+            return NULL;
+        }
+    }
+
+    if (addrp == NULL) {
+        freeifaddrs(ifaddr);
+        return (found ? "no IP" : NULL);
+    }
+
+    int ret;
+    if ((ret = getnameinfo(addrp->ifa_addr, len, part, sizeof(part), NULL, 0, NI_NUMERICHOST)) != 0) {
+        fprintf(stderr, "dwmstatus: getnameinfo(): %s\n", gai_strerror(ret));
+        freeifaddrs(ifaddr);
+        return "no IP";
+    }
+    freeifaddrs(ifaddr);
+    return part;
+}
+
+
+
 /* END NETWORK STUFF
  *
  * Temp stuff
     char *batt = NULL;
     char *net = NULL;
     char *temp = NULL;
+    char *ipaddr = NULL;
     time_t count60 = 0;
     time_t count10 = 0;
 
 	for (;;sleep(1)) {
         if (runevery(&count60, 60)) {
             free(tmchi);
+            free(ipaddr);
 
 		    tmchi  = mktimes("%Y/%d/%m %H:%M", tzchicago);
+            ipaddr = get_ip_addr("wlan0");
         }
         if (runevery(&count10, 10)) {
             free(avgs);
         }
         net    = get_netusage("wlan0");
 
-		status = smprintf("%s | %s | [%s] | T: %s | %s",
-				net, batt, avgs, temp, tmchi);
+		status = smprintf("%s %s | %s | [%s] | T: %s | %s",
+				net, ipaddr, batt, avgs, temp, tmchi);
         free(net);
 		setstatus(status);
 	}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.