+/*
+ * Older realpath implementations (e.g. 4.4BSD) required the second
+ * argument to be non-NULL, and then POSIX added the option of NULL
+ * with the semantics of malloc'ing a big-enough buffer. Define a
+ * helper function with the NULL semantics to accomodate older
+ * platforms.
+ *
+ * gnulib has a list of systems that are known to reject NULL as the
+ * 2nd argument:
+ * https://www.gnu.org/software/gnulib/manual/html_node/realpath.html
+ */
+
+#define BUP_HAVE_POSIX_REALPATH
+
+// FreeBSD < 7: bup's FreeBSD code does not use realpath(3)
+#if defined(__NetBSD__)
+# if !__NetBSD_Prereq__(7,0,0)
+# undef BUP_HAVE_POSIX_REALPATH
+# endif
+// OpenBSD: https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/sys/sys/param.h.diff?r1=1.91&r2=1.92&f=h
+#elif defined(__OpenBSD__) && __OpenBSD__ < 201111
+# undef BUP_HAVE_POSIX_REALPATH
+#endif
+
+char *
+bup_realpath(const char *pathname)
+{
+#ifdef BUP_HAVE_POSIX_REALPATH
+ return realpath(pathname, NULL);
+#else
+ char resolvedname[PATH_MAX];
+ char *ret = realpath(pathname, resolvedname);
+ if (ret != NULL) {
+ assert(ret == resolvedname);
+ ret = strdup(ret);
+ }
+ return ret;
+#endif
+}
+