Merge pull request #1295 from carlosmn/obsd Fix p_realpath on OpenBSD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/unix/posix.h b/src/unix/posix.h
index 2c169bd..c738b53 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -16,7 +16,12 @@
#define p_unlink(p) unlink(p)
#define p_mkdir(p,m) mkdir(p, m)
#define p_fsync(fd) fsync(fd)
-#define p_realpath(p, po) realpath(p, po)
+
+/* The OpenBSD realpath function behaves differently */
+#if !defined(__OpenBSD__)
+# define p_realpath(p, po) realpath(p, po)
+#endif
+
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a)
#define p_snprintf(b, c, f, ...) snprintf(b, c, f, __VA_ARGS__)
#define p_mkstemp(p) mkstemp(p)
diff --git a/src/unix/realpath.c b/src/unix/realpath.c
new file mode 100644
index 0000000..f382c2b
--- /dev/null
+++ b/src/unix/realpath.c
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#include <git2/common.h>
+
+#ifdef __OpenBSD__
+
+#include <limits.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+char *p_realpath(const char *pathname, char *resolved)
+{
+ char *ret;
+
+ if ((ret = realpath(pathname, resolved)) == NULL)
+ return NULL;
+
+ /* Figure out if the file exists */
+ if (!access(ret, F_OK))
+ ret;
+
+ return NULL;
+}
+
+#endif