2017-09-17 18:45:03 +03:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 19:10:39 +03:00
|
|
|
#if defined(__linux__)
|
2018-05-01 20:01:22 +03:00
|
|
|
#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>
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
#include "../util.h"
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
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;
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
|
|
|
|
"/operstate");
|
2018-05-02 09:49:06 +03:00
|
|
|
if (!(fp = fopen(path, "r"))) {
|
2018-05-01 20:01:22 +03:00
|
|
|
fprintf(stderr, "fopen '%s': %s\n", path,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-02 09:49:06 +03:00
|
|
|
if(!(p = fgets(status, 5, fp)) || strcmp(status, "up\n") != 0) {
|
2018-05-01 20:01:22 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-02 09:49:06 +03:00
|
|
|
fclose(fp);
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-02 09:49:06 +03:00
|
|
|
if (!(fp = fopen("/proc/net/wireless", "r"))) {
|
2018-05-01 20:01:22 +03:00
|
|
|
fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
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;
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
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);
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
perc = (float)cur / total * 100.0;
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
return bprintf("%.0f", perc);
|
|
|
|
}
|
2017-10-24 12:03:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
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;
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
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);
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
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;
|
|
|
|
}
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-01-07 00:37:09 +02:00
|
|
|
close(sockfd);
|
2017-09-17 17:18:17 +03:00
|
|
|
|
2018-05-01 20:01:22 +03:00
|
|
|
if (strcmp(id, "") == 0)
|
|
|
|
return NULL;
|
2018-05-02 09:49:06 +03:00
|
|
|
|
|
|
|
return id;
|
2018-05-01 20:01:22 +03:00
|
|
|
}
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
/* unimplemented */
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 19:10:39 +03:00
|
|
|
#endif
|