packages = glob_packages("/var/lib/pacman/local/*");
#elif DISTRO == FRUGALWARE
- FILE *pkgs_file = popen("pacman-g2 -Q 2> /dev/null | wc -l", "r");
+ FILE *pkgs_file;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(pkgs_file, SCAN_UFINT, &packages);
+ CHECK_POPEN(pkgs_file, "pacman-g2 -Q 2> /dev/null | wc -l", &packages);
#pragma GCC diagnostic pop
- pclose(pkgs_file);
-
#elif DISTRO == DEBIAN
packages = glob_packages("/var/lib/dpkg/info/*.list");
packages = glob_packages("/var/db/pkg/*/*");
#elif DISTRO == RHEL
- FILE *pkgs_file = popen("rpm -qa 2> /dev/null | wc -l", "r");
+ FILE *pkgs_file;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(pkgs_file, SCAN_UFINT, &packages);
+ CHECK_POPEN(pkgs_file, "rpm -qa 2> /dev/null | wc -l", &packages);
#pragma GCC diagnostic pop
- pclose(pkgs_file);
-
#elif DISTRO == ANGSTROM
- FILE *pkgs_file = popen("opkg list-installed 2> /dev/null | wc -l", "r");
+ FILE *pkgs_file;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(pkgs_file, SCAN_UFINT, &packages);
+ CHECK_POPEN(pkgs_file, "opkg list-installed 2> /dev/null | wc -l", &packages);
#pragma GCC diagnostic pop
- pclose(pkgs_file);
-
#elif DISTRO == FREEBSD
- FILE *pkgs_file = popen("pkg info | wc -l", "r");
+ FILE *pkgs_file;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(pkgs_file, SCAN_UFINT, &packages);
+ CHECK_POPEN(pkgs_file, "pkg info | wc -l", &packages);
#pragma GCC diagnostic pop
- pclose(pkgs_file);
-
#endif
FILL_ARR(str1, UFINT, packages);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(fp, UFINT, &rpm[z]);
+ CHECK_FSCANF(fp, UFINT, &rpm[z]);
#pragma GCC diagnostic pop
- fclose(fp);
+ CLOSE_X(fp);
}
#else
if (found_fans) {
for (x = 0; x < z; x++) {
- if (0 != rpm[x])
+ if (0 != rpm[x]) {
GLUE2(all_fans, UFINT" ", rpm[x]);
- else
+ } else {
++y; /* non-spinning | removed | failed fan */
+ }
}
FILL_STR_ARR(1, str1, (y != x ? buffer : NOT_FOUND));
}
FILL_STR_ARR(1, str1, "Null");
if (NULL == p_cdio) {
- FUNC_FAILED("cdio_open()");
+ return;
}
if (mmc_get_hwinfo(p_cdio, &hwinfo)) {
FILL_STR_ARR(2, str1, hwinfo.psz_vendor, hwinfo.psz_model);
#if defined(__linux__)
FILE *fp = fopen("/proc/stat", "r");
- if (NULL == fp) {
- exit_with_err(CANNOT_OPEN, "/proc/stat");
- }
+ CHECK_FP(fp);
/* Some kernels will produce 7, 8 and 9 columns
* We rely on 10, refer to `man proc' for more details */
&cpu_active[0], &cpu_active[1], &cpu_active[2], &cpu_active[3],
&cpu_active[4], &cpu_active[5], &cpu_active[6], &cpu_active[7],
&cpu_active[8], &cpu_active[9]) == EOF) {
- fclose(fp);
+ CLOSE_X(fp);
exit_with_err(ERR,"Upgrade to a newer kernel");
}
#pragma GCC diagnostic pop
- fclose(fp);
+ CLOSE_X(fp);
#endif /* __linux__ */
for (x = 0; x < LOOP_ITERZ; x++) {
#if defined(__linux__)
FILE *fp = fopen("/proc/stat", "r");
- if (NULL == fp) {
- exit_with_err(CANNOT_OPEN, "/proc/stat");
- }
+ CHECK_FP(fp);
if (NULL == fgets(buf, VLA, fp)) {
- fclose(fp);
+ CLOSE_X(fp);
exit_with_err(ERR, "reached /proc/stat EOF");
}
for (x = 0; x < MAX_CORES; x++, z++) {
if (NULL == fgets(buf, VLA, fp)) {
- fclose(fp);
+ CLOSE_X(fp);
exit_with_err(ERR, "reached /proc/stat EOF");
}
&core_active[x][0], &core_active[x][1], &core_active[x][2], &core_active[x][3],
&core_active[x][4], &core_active[x][5], &core_active[x][6], &core_active[x][7],
&core_active[x][8], &core_active[x][9]) == EOF) {
- fclose(fp);
+ CLOSE_X(fp);
exit_with_err(ERR,"Upgrade to a newer kernel");
}
}
- fclose(fp);
+ CLOSE_X(fp);
for (x = 0; x < z; x++) {
MA 02110-1301, USA.
*/
-/* The pragma directives are here
- * to mute the gcc twisted vision,
- * and clangs inabillity to distinguish
- * C from C++
- *
- * https://llvm.org/bugs/show_bug.cgi?id=21689
- * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
- * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509
- *
- * Do not add any -Wno flags just to mute the compilers snafus
- * */
-
#include "config.h" /* Auto-generated */
#include <sys/stat.h>
/* exit with error */
#define CANNOT_OPEN "Could not open"
+#define CANNOT_CLOSE "Could not close a file handle"
+#define CANNOT_OPEN_FP "Could not open a file handle"
+#define FSCANF_FAILED "fscanf() failed"
#define ERR "Error:"
#define NOT_FOUND "Not found, "
#define FUNC_FAILED(x) (exit_with_err(ERR, x " failed"))
#define RECOMPILE_WITH(x) (exit_with_err(ERR, "recompile the program --with-" x))
/* Let the preprocessor Do Repeat Myself */
+
+#define CHECK_FSCANF(fp, x, z) \
+ if (EOF == (fscanf(fp, x, z))) { \
+ exit_with_err(ERR, FSCANF_FAILED); \
+ }
+
+#define CHECK_FP(fp) \
+ if (NULL == fp) { \
+ exit_with_err(ERR, CANNOT_OPEN_FP); \
+ }
+
+#define CLOSE_X(fp) \
+ if (EOF == (fclose(fp))) { \
+ exit_with_err(ERR, CANNOT_CLOSE); \
+ }
+
#define OPEN_X(fp, x, y, z) \
if (NULL == (fp = fopen(x, "r"))) { \
exit_with_err(CANNOT_OPEN, x); \
} \
- fscanf(fp, y, z); \
- fclose(fp);
+ CHECK_FSCANF(fp, y, z); \
+ CLOSE_X(fp);
+
+#define CHECK_POPEN(fp, x, packs) \
+ if (NULL == (fp = popen(x, "r"))) { \
+ exit_with_err(CANNOT_OPEN, x); \
+ } \
+ CHECK_FSCANF(fp, SCAN_UFINT, packs); \
+ if (-1 == (pclose(fp))) { \
+ exit_with_err(CANNOT_CLOSE, x); \
+ }
/* How many fans to try for detection */
#define MAX_FANS 20
if (found_fans) {
for (x = 0; x < z; x++) {
- if (0 != rpm[x])
+ if (0 != rpm[x]) {
GLUE2(all, UFINT" ", rpm[x]);
- else
+ } else {
++y; /* non-spinning | removed | failed fan */
+ }
}
FILL_STR_ARR(1, str1, (y != x ? buffer : NOT_FOUND));
}
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(fp, "%f", &voltage[x]);
+ CHECK_FSCANF(fp, "%f", &voltage[x]);
#pragma GCC diagnostic pop
- fclose(fp);
+ CLOSE_X(fp);
voltage[x] /= (float)1000.0;
}
memset(statio, 0, sizeof(statio));
FILE *fp = fopen(stat_file, "r");
- if (NULL == fp) {
- exit_with_err(CANNOT_OPEN, stat_file);
- }
+ CHECK_FP(fp);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
if (fscanf(fp, FMT_UINT FMT_UINT FMT_UINT FMT_UINT FMT_UINT FMT_UINT FMT_UINT,
&statio[0], &statio[1], &statio[2], &statio[3],
&statio[4], &statio[5], &statio[6]) == EOF) {
- fclose(fp);
+ CLOSE_X(fp);
exit_with_err(ERR, "reading the stat file failed");
}
#pragma GCC diagnostic pop
- fclose(fp);
+ CLOSE_X(fp);
FILL_ARR(str1, "Read " FMT_UINT " MB, Written " FMT_UINT " MB",
BYTES_TO_MB(statio[2]), BYTES_TO_MB(statio[6]));
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
- fscanf(fp, FMT_UINT, &total);
+ CHECK_FSCANF(fp, FMT_UINT, &total);
#pragma GCC diagnostic pop
- fclose(fp);
+ CLOSE_X(fp);
BATTERY_USED(temp, num);