- int now, full, perc;
- char batterynowfile[64] = "";
- char batteryfullfile[64] = "";
- FILE *fp;
-
- /* generate battery nowfile path */
- strcat(batterynowfile, batterypath);
- strcat(batterynowfile, battery);
- strcat(batterynowfile, "/");
- strcat(batterynowfile, batterynow);
-
- /* generate battery fullfile path */
- strcat(batteryfullfile, batterypath);
- strcat(batteryfullfile, battery);
- strcat(batteryfullfile, "/");
- strcat(batteryfullfile, batteryfull);
-
- /* open battery now file */
- if (!(fp = fopen(batterynowfile, "r"))) {
- fprintf(stderr, "Error opening battery file.%s",batterynowfile);
- return smprintf("n/a");
- }
-
- /* read value */
- fscanf(fp, "%i", &now);
-
- /* close battery now file */
- fclose(fp);
-
- /* open battery full file */
- if (!(fp = fopen(batteryfullfile, "r"))) {
- fprintf(stderr, "Error opening battery file.");
- return smprintf("n/a");
- }
-
- /* read value */
- fscanf(fp, "%i", &full);
-
- /* close battery full file */
- fclose(fp);
-
- /* calculate percent */
- perc = now / (full / 100);
-
- /* return perc as string */
- return smprintf("%d%%", perc);
+ int perc;
+ long double a[4], b[4];
+ FILE *fp;
+
+ fp = fopen("/proc/stat", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/stat");
+ return smprintf(UNKNOWN_STR);
+ }
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
+ fclose(fp);
+
+ delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1));
+ sleep(delay);
+
+ fp = fopen("/proc/stat", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/stat");
+ return smprintf(UNKNOWN_STR);
+ }
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
+ fclose(fp);
+
+ perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+ return smprintf("%d%%", perc);