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;
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);
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);
}
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);
#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_ */
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");
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) {