]> granicus.if.org Git - p11-kit/commitdiff
common: Fix compilation of runtime.c under mingw
authorDaiki Ueno <dueno@redhat.com>
Thu, 29 Mar 2018 08:28:59 +0000 (10:28 +0200)
committerDaiki Ueno <ueno@gnu.org>
Thu, 29 Mar 2018 12:34:35 +0000 (14:34 +0200)
common/runtime.c
common/test-runtime.c

index f713e7dea6eaa7c0896221fdbe0988e177d3517b..316c5be660264413d3ccc050a4a3aeab0ae10f52 100644 (file)
 #include "runtime.h"
 
 #include "compat.h"
-#include <pwd.h>
+
 #include <stdio.h>
 #include <string.h>
+
+#ifdef OS_UNIX
+#include <pwd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 static const char * const _p11_runtime_bases_default[] = { "/run", "/var/run", NULL };
 const char * const *_p11_runtime_bases = _p11_runtime_bases_default;
+#endif
 
 CK_RV
 p11_get_runtime_directory (char **directoryp)
 {
        const char *envvar;
+       char *directory;
+#ifdef OS_UNIX
        const char * const *bases = _p11_runtime_bases;
        char prefix[13 + 1 + 20 + 6 + 1];
-       char *directory;
        uid_t uid;
        struct stat sb;
        struct passwd pwbuf, *pw;
        char buf[1024];
        int i;
+#endif
 
        /* We can't always assume the XDG_RUNTIME_DIR envvar here,
         * because the PKCS#11 module can be loaded by a program that
@@ -74,6 +80,7 @@ p11_get_runtime_directory (char **directoryp)
                return CKR_OK;
        }
 
+#ifdef OS_UNIX
        uid = getuid ();
 
        for (i = 0; bases[i] != NULL; i++) {
@@ -87,6 +94,7 @@ p11_get_runtime_directory (char **directoryp)
                        return CKR_OK;
                }
        }
+#endif
 
        /* We can't use /run/user/<UID>, fallback to ~/.cache.  */
        envvar = secure_getenv ("XDG_CACHE_HOME");
@@ -100,12 +108,15 @@ p11_get_runtime_directory (char **directoryp)
                return CKR_OK;
        }
 
-       if (getpwuid_r (uid, &pwbuf, buf, sizeof buf, &pw) < 0 ||
-           pw == NULL || pw->pw_dir == NULL || *pw->pw_dir != '/')
-               return CKR_GENERAL_ERROR;
+#ifdef OS_UNIX
+       if (getpwuid_r (uid, &pwbuf, buf, sizeof buf, &pw) == 0 &&
+           pw != NULL && pw->pw_dir != NULL && *pw->pw_dir == '/') {
+               if (asprintf (&directory, "%s/.cache", pw->pw_dir) < 0)
+                       return CKR_HOST_MEMORY;
+               *directoryp = directory;
+               return CKR_OK;
+       }
+#endif
 
-       if (asprintf (&directory, "%s/.cache", pw->pw_dir) < 0)
-               return CKR_HOST_MEMORY;
-       *directoryp = directory;
-       return CKR_OK;
+       return CKR_GENERAL_ERROR;
 }
index 294a7ad8543995ea59fb651524f9f147cdde6277..1594a9ca707ed5cc14346625d3f497b6a74fc9ad 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 
+#ifdef OS_UNIX
+#include <unistd.h>
+#include <sys/types.h>
+#endif
+
 static struct {
        char *directory;
 } test;
@@ -71,6 +76,7 @@ test_xdg_runtime_dir (void)
        free (directory);
 }
 
+#ifdef OS_UNIX
 static void
 test_bases (void)
 {
@@ -103,17 +109,21 @@ test_bases (void)
        free (path);
        free (directory);
 }
+#endif
 
 static void
 test_xdg_cache_home (void)
 {
        char *directory;
+#ifdef OS_UNIX
        const char * bases[] = {
                NULL
        };
        _p11_runtime_bases = bases;
+#endif
 
-       unsetenv ("XDG_RUNTIME_DIR");
+       /* MinGW doesn't have unsetenv */
+       setenv ("XDG_RUNTIME_DIR", "", 1);
        setenv ("XDG_CACHE_HOME", "/cache", 1);
        p11_get_runtime_directory (&directory);
        assert_str_eq ("/cache", directory);
@@ -126,7 +136,9 @@ main (int argc,
 {
        p11_fixture (setup, teardown);
        p11_test (test_xdg_runtime_dir, "/runtime/xdg-runtime-dir");
+#ifdef OS_UNIX
        p11_test (test_bases, "/runtime/bases");
+#endif
        p11_test (test_xdg_cache_home, "/runtime/xdg-cache-home");
        p11_test_run (argc, argv);
 }