- FILE *fp;
- long total, free, available;
- int ram_perc;
-
- /* read meminfo file, extract and close */
- if (!(fp = fopen("/proc/meminfo", "r"))) {
- fprintf(stderr, "Error opening meminfo file.");
- exit(1);
- }
- fscanf(fp, "MemTotal: %ld kB\n", &total);
- fscanf(fp, "MemFree: %ld kB\n", &free);
- fscanf(fp, "MemAvailable: %ld kB\n", &available);
- fclose(fp);
-
- /* calculate percentage */
- ram_perc = 100 * (total - available) / total;
-
- /* return in percent */
- return smprintf("%d%%",ram_perc);
+ 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.");
+ exit(1);
+ }
+
+ /* read the status */
+ fgets(status, 5, fp);
+
+ /* close wifi file */
+ fclose(fp);
+
+ /* check if interface down */
+ if(strcmp(status, "up\n") != 0){
+ return "n/a";
+ }
+
+ /* open wifi file */
+ if (!(fp = fopen("/proc/net/wireless", "r"))) {
+ fprintf(stderr, "Error opening wireless file.");
+ exit(1);
+ }
+
+ /* 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);