X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/f7a6d6c8f7d621fe7b5a9c37a130f8bf3b9bd892..82eb6e3832dea63be4c22ac4415c3190add43f0b:/components/wifi.c diff --git a/components/wifi.c b/components/wifi.c index 13b630a..24dca36 100644 --- a/components/wifi.c +++ b/components/wifi.c @@ -2,10 +2,10 @@ #if defined(__linux__) #include #include + #include #include #include #include - #include #include #include #include @@ -16,7 +16,6 @@ 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]; @@ -30,10 +29,11 @@ strerror(errno)); return NULL; } - if(!(p = fgets(status, 5, fp)) || strcmp(status, "up\n") != 0) { + p = fgets(status, 5, fp); + fclose(fp); + if(!p || strcmp(status, "up\n") != 0) { return NULL; } - fclose(fp); if (!(fp = fopen("/proc/net/wireless", "r"))) { fprintf(stderr, "fopen '/proc/net/wireless': %s\n", @@ -46,19 +46,19 @@ break; } fclose(fp); - if (i < 2 || !p) + if (i < 2 || !p) { return NULL; + } - if ((datastart = strstr(buf, iface)) == NULL) + if (!(datastart = strstr(buf, iface))) { 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); - perc = (float)cur / total * 100.0; - - return bprintf("%.0f", perc); + return bprintf("%d", (int)((float)cur / total * 100)); } const char * @@ -72,23 +72,23 @@ wreq.u.essid.length = IW_ESSID_MAX_SIZE+1; snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface); - if (sockfd == -1) { + if (sockfd < 0) { 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)); + if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) { + fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno)); close(sockfd); return NULL; } close(sockfd); - if (strcmp(id, "") == 0) + if (!strcmp(id, "")) { return NULL; + } return id; }