Xinqi Bao's Git
30b57ab190f64b1600a6bfd84af971fd49c5cfed
1 /* See LICENSE file for copyright and license details. */
4 #include <linux/wireless.h>
5 #include <sys/socket.h>
15 wifi_perc(const char *iface
)
23 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/net/", iface
, "/operstate");
24 fp
= fopen(path
, "r");
26 warn("Failed to open file %s", path
);
29 p
= fgets(status
, 5, fp
);
31 if(!p
|| strcmp(status
, "up\n") != 0) {
35 fp
= fopen("/proc/net/wireless", "r");
37 warn("Failed to open file /proc/net/wireless");
41 for (i
= 0; i
< 3; i
++) {
42 if (!(p
= fgets(buf
, sizeof(buf
) - 1, fp
)))
49 if ((datastart
= strstr(buf
, iface
)) == NULL
)
52 datastart
= (datastart
+(strlen(iface
)+1));
53 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
55 return bprintf("%d", perc
);
59 wifi_essid(const char *iface
)
61 static char id
[IW_ESSID_MAX_SIZE
+1];
62 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
65 memset(&wreq
, 0, sizeof(struct iwreq
));
66 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
67 snprintf(wreq
.ifr_name
, sizeof(wreq
.ifr_name
), "%s", iface
);
70 warn("Failed to get ESSID for interface %s", iface
);
73 wreq
.u
.essid
.pointer
= id
;
74 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
75 warn("Failed to get ESSID for interface %s", iface
);
81 if (strcmp(id
, "") == 0)