OSDN Git Service

Use the preprocessor more extensively
authorwifiextender <router@archlinux.info>
Thu, 11 Aug 2016 21:24:52 +0000 (23:24 +0200)
committerwifiextender <router@archlinux.info>
Thu, 11 Aug 2016 21:24:52 +0000 (23:24 +0200)
src/common.c
src/functions.c
src/include/functions_constants.h
src/net.c

index 4359e9f..b051002 100644 (file)
@@ -31,16 +31,12 @@ exit_with_err(const char *str1, const char *str2) {
 void
 get_temp(const char *str1, char *str2) {
   uintmax_t temp;
+  FILE *fp;
 
-  FILE *fp = fopen(str1, "r");
-  if (NULL == fp) {
-    exit_with_err(CANNOT_OPEN, str1);
-  }
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, FMT_UINT, &temp);
+  OPEN_X(fp, str1, FMT_UINT, &temp);
 #pragma GCC diagnostic pop
-  fclose(fp);
 
   temp /= (uintmax_t)1000;
 
index 9b3c0cc..24faee2 100644 (file)
@@ -77,19 +77,14 @@ get_ssd(char *str1) {
 
 void
 get_ssd_model(char *str1, char *str2) {
+  FILE *fp;
   char model[VLA];
   FILL_ARR(model, "%s%s%s", "/sys/block/", str2, "/device/model");
 
-  FILE *fp = fopen(model, "r");
-  if (NULL == fp) {
-    exit_with_err(CANNOT_OPEN, model);
-  }
-
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, "%[^\n]", model);
+  OPEN_X(fp, model, "%[^\n]", model);
 #pragma GCC diagnostic pop
-  fclose(fp);
 
   FILL_STR_ARR(1, str1, model);
 
@@ -250,28 +245,18 @@ get_fans(char *str1) {
 
 void 
 get_mobo(char *str1) {
+  FILE *fp;
   char vendor[VLA], name[VLA];
 
-  FILE *fp = fopen(MOBO_VENDOR, "r");
-  if (NULL == fp) {
-    exit_with_err(CANNOT_OPEN, MOBO_VENDOR);
-  }
-  /* use %[^\n] to get the whole line */
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, "%s", vendor);
+  OPEN_X(fp, MOBO_VENDOR, "%s", vendor);
 #pragma GCC diagnostic pop
-  fclose(fp);
 
-  if (NULL == (fp = fopen(MOBO_NAME, "r"))) {
-    exit_with_err(CANNOT_OPEN, MOBO_NAME);
-  }
-  /* use %[^\n] to get the whole line */
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, "%s", name);
+  OPEN_X(fp, MOBO_NAME, "%s", name);
 #pragma GCC diagnostic pop
-  fclose(fp);
 
   FILL_STR_ARR(2, str1, vendor, name);
 }
@@ -369,15 +354,11 @@ get_battery(char *str1) {
   fclose(fp);
 
   BATTERY_USED(temp, num);
-  if (NULL == (fp = fopen(temp, "r"))) {
-    exit_with_err(CANNOT_OPEN, temp);
-  }
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, FMT_UINT, &used);
+  OPEN_X(fp, temp, FMT_UINT, &used);
 #pragma GCC diagnostic pop
-  fclose(fp);
 
   percent = (used * 100) / total;
   FILL_UINT_ARR(str1, percent);
index fa8ebf6..46505c2 100644 (file)
 #define NOT_FOUND "Not found, "
 #define FUNC_FAILED(x) (exit_with_err(ERR, x " failed"))
 
+/* Let the preprocessor Do Repeat Myself */
+#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);
+
 #endif /* CONSTANTS2_H_ */
index e24e343..03ae81a 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -172,6 +172,15 @@ get_nic_info2(char *str1, char *str2, unsigned char num) {
     return;
   }
 
+  /* The `ethtool_cmd_speed' negotiation concept
+   * is great, but the `SPEED_X' macros are not.
+   * They are constants representing the link speed
+   * from 10 Mbps up to 100 Gbps, but as we know,
+   * the wifi/wireless "things" suffer from various
+   * degradations, thus those macros are only reliable
+   * to detect wired NIC that doesn't tend to change
+   * it's link speed from various real-life factors.
+  */
   switch(num) {
     case 1:
       FILL_ARR(str1, "%d%s", ecmd.speed, "Mbps");
@@ -267,31 +276,22 @@ get_nic_info(char *str1, char *str2) {
   char temp[VLA];
   struct pci_access *pacc = NULL;
   struct pci_dev *dev = NULL;
+  FILE *fp;
 
   FILL_STR_ARR(1, str1, "Null");
   NIC_VEND(temp, str2);
 
-  FILE *fp = fopen(temp, "r");
-  if (NULL == fp) {
-    exit_with_err(CANNOT_OPEN, temp);
-  }
-
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, FMT_UINTX, &vendor); /* hex */
+  OPEN_X(fp, temp, FMT_UINTX, &vendor); /* hex */
 #pragma GCC diagnostic pop
-  fclose(fp);
 
   NIC_MODEL(temp, str2);
-  if (NULL == (fp = fopen(temp, "r"))) {
-    exit_with_err(CANNOT_OPEN, temp);
-  }
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wunused-result"
-  fscanf(fp, FMT_UINTX, &model); /* hex */
+  OPEN_X(fp, temp, FMT_UINTX, &model); /* hex */
 #pragma GCC diagnostic pop
-  fclose(fp);
 
   pacc = pci_alloc();
   if (NULL == pacc) {