OSDN Git Service

Use the preprocessor more extensively
authorwifiextender <router@archlinux.info>
Sun, 28 Aug 2016 08:51:25 +0000 (10:51 +0200)
committerwifiextender <router@archlinux.info>
Sun, 28 Aug 2016 08:51:25 +0000 (10:51 +0200)
src/common.c
src/cpu.c
src/freebsd_functions.c
src/include/functions_constants.h
src/linux_functions.c

index 6a619bb..01fe0c1 100644 (file)
@@ -185,15 +185,13 @@ get_packs(char *str1) {
   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");
 
@@ -204,35 +202,29 @@ get_packs(char *str1) {
   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);
@@ -346,9 +338,9 @@ get_fans(char *str1) {
 
 #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
@@ -374,10 +366,11 @@ get_fans(char *str1) {
 
   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));
   }
@@ -393,7 +386,7 @@ get_dvd(char *str1) {
 
   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);
index bd18baa..c7295c9 100644 (file)
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -52,9 +52,7 @@ get_cpu(char *str1) {
 
 #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 */
@@ -64,11 +62,11 @@ get_cpu(char *str1) {
     &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++) {
@@ -118,18 +116,16 @@ get_cores_load(char *str1) {
 
 #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");
     }
 
@@ -141,11 +137,11 @@ get_cores_load(char *str1) {
       &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++) {
 
index 57acb38..7d03a12 100644 (file)
    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>
index 5c8bf91..e690860 100644 (file)
 
 /* 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
index 1627ae4..e9488ff 100644 (file)
@@ -208,10 +208,11 @@ match_feature(char *str1, uint8_t num) {
 
   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));
   }
@@ -260,9 +261,9 @@ get_voltage(char *str1) {
     }
 #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;
   }
@@ -305,20 +306,18 @@ get_statio(char *str1, char *str2) {
   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]));
@@ -345,9 +344,9 @@ get_battery(char *str1) {
 
 #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);