- 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 now, full, perc;
+ char batterynowfile[64];
+ char batteryfullfile[64];
+ FILE *fp = fopen(batterynowfile, "r");
+
+ strlcpy(batterynowfile, BATTERY_PATH, sizeof(batterynowfile));
+ strlcat(batterynowfile, battery, sizeof(batterynowfile));
+ strlcat(batterynowfile, "/", sizeof(batterynowfile));
+ strlcat(batterynowfile, BATTERY_NOW, sizeof(batterynowfile));
+
+ strlcpy(batteryfullfile, BATTERY_PATH, sizeof(batteryfullfile));
+ strlcat(batteryfullfile, battery, sizeof(batteryfullfile));
+ strlcat(batteryfullfile, "/", sizeof(batteryfullfile));
+ strlcat(batteryfullfile, BATTERY_FULL, sizeof(batteryfullfile));
+
+ if (fp == NULL ) {
+ fprintf(stderr, "Error opening battery file: %s: %s\n",
+ batterynowfile,
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%i", &now);
+ fclose(fp);
+
+ fp = fopen(batteryfullfile, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Error opening battery file: %s\n",
+ strerror(errno));
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%i", &full);
+ fclose(fp);
+
+ perc = now / (full / 100);
+
+ return smprintf("%d%%", perc);