summaryrefslogtreecommitdiff
path: root/components/wifi.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2018-05-01 19:01:22 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-01 19:43:49 +0200
commit9750a3d731cd381e832bcacf1d03e48ddb46cc16 (patch)
treed3b6d97ad8434a14e8fed7dfe3a2e733df4e05bb /components/wifi.c
parent42edfea89a47cbf0623d1aaf5673a4e5ca79fb52 (diff)
Use indentation to increase readability
Granted, this style is definitely not common, but for the short utility-functions of this program it's just the right choice. This provides great flexibility, such that in the long run, it will be possible to also share code between the OS-implementations. This also keeps the state-keeping at a minimum and makes it clearer which functions are implemented on which OS without having to jiggle around with too many files in the process.
Diffstat (limited to 'components/wifi.c')
-rw-r--r--components/wifi.c158
1 files changed, 83 insertions, 75 deletions
diff --git a/components/wifi.c b/components/wifi.c
index 388a30d..33e09b7 100644
--- a/components/wifi.c
+++ b/components/wifi.c
@@ -1,92 +1,100 @@
/* See LICENSE file for copyright and license details. */
#if defined(__linux__)
-#include <errno.h>
-#include <ifaddrs.h>
-#include <linux/wireless.h>
-#include <sys/socket.h>
-#include <stdio.h>
-#include <limits.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
+ #include <errno.h>
+ #include <ifaddrs.h>
+ #include <linux/wireless.h>
+ #include <sys/socket.h>
+ #include <stdio.h>
+ #include <limits.h>
+ #include <string.h>
+ #include <sys/ioctl.h>
+ #include <unistd.h>
-#include "../util.h"
+ #include "../util.h"
-const char *
-wifi_perc(const char *iface)
-{
- int i, cur;
- float perc;
- int total = 70; /* the max of /proc/net/wireless */
- char *p, *datastart;
- char path[PATH_MAX];
- char status[5];
- FILE *fp;
+ const char *
+ wifi_perc(const char *iface)
+ {
+ int i, cur;
+ float perc;
+ int total = 70; /* the max of /proc/net/wireless */
+ char *p, *datastart;
+ char path[PATH_MAX];
+ char status[5];
+ FILE *fp;
- snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
- fp = fopen(path, "r");
- if (fp == NULL) {
- fprintf(stderr, "fopen '%s': %s\n", path, strerror(errno));
- return NULL;
- }
- p = fgets(status, 5, fp);
- fclose(fp);
- if(!p || strcmp(status, "up\n") != 0) {
- return NULL;
- }
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
+ "/operstate");
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "fopen '%s': %s\n", path,
+ strerror(errno));
+ return NULL;
+ }
+ p = fgets(status, 5, fp);
+ fclose(fp);
+ if(!p || strcmp(status, "up\n") != 0) {
+ return NULL;
+ }
- fp = fopen("/proc/net/wireless", "r");
- if (fp == NULL) {
- fprintf(stderr, "fopen '/proc/net/wireless': %s\n", strerror(errno));
- return NULL;
- }
+ fp = fopen("/proc/net/wireless", "r");
+ if (fp == NULL) {
+ fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
+ strerror(errno));
+ return NULL;
+ }
- for (i = 0; i < 3; i++) {
- if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
- break;
- }
- fclose(fp);
- if (i < 2 || !p)
- return NULL;
+ for (i = 0; i < 3; i++) {
+ if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
+ break;
+ }
+ fclose(fp);
+ if (i < 2 || !p)
+ return NULL;
+
+ if ((datastart = strstr(buf, iface)) == NULL)
+ return NULL;
- if ((datastart = strstr(buf, iface)) == NULL)
- return NULL;
+ datastart = (datastart+(strlen(iface)+1));
+ sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
+ "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
- datastart = (datastart+(strlen(iface)+1));
- sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &cur);
+ perc = (float)cur / total * 100.0;
- perc = (float)cur / total * 100.0;
+ return bprintf("%.0f", perc);
+ }
- return bprintf("%.0f", perc);
-}
+ const char *
+ wifi_essid(const char *iface)
+ {
+ static char id[IW_ESSID_MAX_SIZE+1];
+ int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ struct iwreq wreq;
-const char *
-wifi_essid(const char *iface)
-{
- static char id[IW_ESSID_MAX_SIZE+1];
- int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- struct iwreq wreq;
+ memset(&wreq, 0, sizeof(struct iwreq));
+ wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
+ snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
- memset(&wreq, 0, sizeof(struct iwreq));
- wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
- snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
+ if (sockfd == -1) {
+ fprintf(stderr, "socket 'AF_INET': %s\n",
+ strerror(errno));
+ return NULL;
+ }
+ wreq.u.essid.pointer = id;
+ if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
+ fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n",
+ strerror(errno));
+ close(sockfd);
+ return NULL;
+ }
- if (sockfd == -1) {
- fprintf(stderr, "socket 'AF_INET': %s\n", strerror(errno));
- return NULL;
- }
- wreq.u.essid.pointer = id;
- if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
- fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
close(sockfd);
- return NULL;
- }
- close(sockfd);
-
- if (strcmp(id, "") == 0)
- return NULL;
- else
- return id;
-}
+ if (strcmp(id, "") == 0)
+ return NULL;
+ else
+ return id;
+ }
+#elif defined(__OpenBSD__)
+ /* unimplemented */
#endif