diff --git a/src/utils.cpp b/src/utils.cpp index 7b09e1514b..b45868baba 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -32,6 +32,11 @@ #include // for readlink #endif +#if defined(__APPLE__) +#include +#include // for fcntl +#endif + /*! \file utils.cpp */ /* @@ -162,6 +167,13 @@ const char *utils::guesspath(char *buf, int len, FILE *fp) // get pathname from /proc or copy (unknown) if (readlink(fmt::format("/proc/self/fd/{}", fd).c_str(), buf, len - 1) <= 0) strncpy(buf, "(unknown)", len - 1); +#elif defined(__APPLE__) + int fd = fileno(fp); + char filepath[PATH_MAX]; + if (fcntl(fd,F_GETPATH,filepath) != -1) + strncpy(buf, filepath, len - 1); + else + strncpy(buf, "(unknown)", len - 1); #else strncpy(buf, "(unknown)", len - 1); #endif diff --git a/src/utils.h b/src/utils.h index 0ec1da379a..3d34b72b74 100644 --- a/src/utils.h +++ b/src/utils.h @@ -408,7 +408,7 @@ namespace utils { /*! Try to detect pathname from FILE pointer. * - * Currently only supported on Linux, otherwise will report "(unknown)". + * Currently only supported on Linux and macOS, otherwise will report "(unknown)". * * \param buf storage buffer for pathname. output will be truncated if not large enough * \param len size of storage buffer. output will be truncated to this length - 1 diff --git a/unittest/utils/test_utils.cpp b/unittest/utils/test_utils.cpp index 6db1c5c7ee..60d6d608b3 100644 --- a/unittest/utils/test_utils.cpp +++ b/unittest/utils/test_utils.cpp @@ -722,7 +722,7 @@ TEST(Utils, guesspath) { char buf[256]; FILE *fp = fopen("test_guesspath.txt", "w"); -#if defined(__linux__) +#if defined(__linux__) || (__APPLE__) const char *path = utils::guesspath(buf, sizeof(buf), fp); ASSERT_THAT(path, EndsWith("test_guesspath.txt")); #else