mbpfan-josie

Improve directory error checking

Found via clang-tidy.

b4cb72a6e920975c8532b4c6a9653f2fcb9d3a9e
Andrew Gaul <andrew@gaul.org> · 2024-10-18T12:15 · browse files at this commit

parents: a327a85

diff --git a/src/mbpfan.c b/src/mbpfan.c
index 955e0d3..729c7c0 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -552,7 +552,7 @@ void check_requirements(const char *program_path)
      */
     DIR *dir = opendir(CORETEMP_PATH);
 
-    if (ENOENT == errno) {
+    if (dir == NULL) {
         mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", program_path);
         exit(EXIT_FAILURE);
     }
@@ -563,15 +563,15 @@ void check_requirements(const char *program_path)
 
     dir = opendir(APPLESMC_PATH);
 
-    if (ENOENT != errno) {
+    if (dir != NULL) {
         strncpy((char *) &applesmc_path, APPLESMC_PATH, PATH_MAX);
+        closedir(dir);
     } else {
         /**
         * Check for alternate ACPI device path for newer macbooks
         */
-        closedir(dir);
         dir = opendir(ALT_APPLESMC_PATH);
-        if (ENOENT != errno) {
+        if (dir != NULL) {
             struct dirent *ent;
             while ((ent = readdir(dir)) != NULL) {
                 if (strncmp("APP", (const char *) &ent->d_name, 3) == 0) {
@@ -579,11 +579,10 @@ void check_requirements(const char *program_path)
                     break;
                 }
             }
+            closedir(dir);
         }
     }
 
-    closedir(dir);
-
     if (strlen(applesmc_path) != 0) {
         strncpy((char *) &applesmc_fan_path, (char *) &applesmc_path, PATH_MAX);
         strcat((char *) &applesmc_fan_path, "/fan");