- 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);