- int bufsize = 255;
- int strength;
- char buf[bufsize];
- char *datastart;
- char path_start[16] = "/sys/class/net/";
- char path_end[11] = "/operstate";
- char path[32];
- char status[5];
- char needle[sizeof wificard + 1];
- FILE *fp;
-
- /* generate the path name */
- memset(path, 0, sizeof path);
- strcat(path, path_start);
- strcat(path, wificard);
- strcat(path, path_end);
-
- /* open wifi file */
- if(!(fp = fopen(path, "r"))) {
- fprintf(stderr, "Error opening wifi operstate file.");
- return smprintf("n/a");
- }
-
- /* read the status */
- fgets(status, 5, fp);
-
- /* close wifi file */
- fclose(fp);
-
- /* check if interface down */
- if(strcmp(status, "up\n") != 0){
- return smprintf("n/a");
- }
-
- /* open wifi file */
- if (!(fp = fopen("/proc/net/wireless", "r"))) {
- fprintf(stderr, "Error opening wireless file.");
- return smprintf("n/a");
- }
-
- /* extract the signal strength */
- strcpy(needle, wificard);
- strcat(needle, ":");
- fgets(buf, bufsize, fp);
- fgets(buf, bufsize, fp);
- fgets(buf, bufsize, fp);
- if ((datastart = strstr(buf, needle)) != NULL) {
- datastart = strstr(buf, ":");
- sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &strength);
- }
-
- /* close wifi file */
- fclose(fp);
-
- /* return strength in percent */
- return smprintf("%d%%", strength);
+ 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;
+ sprintf(wreq.ifr_name, wificard);
+ if(sockfd == -1) {
+ fprintf(stderr, "Cannot open socket for interface: %s: %s\n",
+ wificard, strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+ wreq.u.essid.pointer = id;
+ if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
+ fprintf(stderr, "Get ESSID ioctl failed for interface %s: %s\n",
+ wificard, strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
+ return smprintf(UNKNOWN_STR);
+ else
+ return smprintf("%s", (char *)wreq.u.essid.pointer);