Commit d075c3e6971a10fd7e0bd44bbf5a5c9d610159d7

Peter Hutterer 2020-06-01T14:16:23

Factor the access check for paths out Easier to re-use without having to duplicate ifdefs. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>

diff --git a/src/context.c b/src/context.c
index fbb48dc..fe24516 100644
--- a/src/context.c
+++ b/src/context.c
@@ -63,13 +63,8 @@ xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
     if (!S_ISDIR(stat_buf.st_mode))
         goto err;
 
-#if defined(HAVE_EACCESS)
-    if (eaccess(path, R_OK | X_OK) != 0)
+    if (!check_eaccess(path, R_OK | X_OK))
         goto err;
-#elif defined(HAVE_EUIDACCESS)
-    if (euidaccess(path, R_OK | X_OK) != 0)
-        goto err;
-#endif
 
     darray_append(ctx->includes, tmp);
     return 1;
diff --git a/src/utils.h b/src/utils.h
index 303b93c..2c35a87 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -29,6 +29,15 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
+#if defined(HAVE_EACCESS) || defined(HAVE_EUIDACCESS)
+#include <unistd.h>
+#else
+/* Required on Windows where unistd.h doesn't exist */
+#define R_OK    4               /* Test for read permission.  */
+#define W_OK    2               /* Test for write permission.  */
+#define X_OK    1               /* Test for execute permission.  */
+#define F_OK    0               /* Test for existence.  */
+#endif
 
 #include "darray.h"
 
@@ -202,6 +211,20 @@ map_file(FILE *file, char **string_out, size_t *size_out);
 void
 unmap_file(char *string, size_t size);
 
+static inline bool
+check_eaccess(const char *path, int mode)
+{
+#if defined(HAVE_EACCESS)
+    if (eaccess(path, mode) != 0)
+        return false;
+#elif defined(HAVE_EUIDACCESS)
+    if (euidaccess(path, mode) != 0)
+        return false;
+#endif
+
+    return true;
+}
+
 #if defined(HAVE_SECURE_GETENV)
 # define secure_getenv secure_getenv
 #elif defined(HAVE___SECURE_GETENV)