add support for utils::guesspath() on macos

This commit is contained in:
Axel Kohlmeyer
2021-09-08 12:09:14 -04:00
parent 22f295ffd8
commit cfa94dfbaf
3 changed files with 14 additions and 2 deletions

View File

@ -32,6 +32,11 @@
#include <unistd.h> // for readlink #include <unistd.h> // for readlink
#endif #endif
#if defined(__APPLE__)
#include <sys/syslimits.h>
#include <fcntl.h> // for fcntl
#endif
/*! \file utils.cpp */ /*! \file utils.cpp */
/* /*
@ -162,6 +167,13 @@ const char *utils::guesspath(char *buf, int len, FILE *fp)
// get pathname from /proc or copy (unknown) // get pathname from /proc or copy (unknown)
if (readlink(fmt::format("/proc/self/fd/{}", fd).c_str(), buf, len - 1) <= 0) if (readlink(fmt::format("/proc/self/fd/{}", fd).c_str(), buf, len - 1) <= 0)
strncpy(buf, "(unknown)", len - 1); 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 #else
strncpy(buf, "(unknown)", len - 1); strncpy(buf, "(unknown)", len - 1);
#endif #endif

View File

@ -408,7 +408,7 @@ namespace utils {
/*! Try to detect pathname from FILE pointer. /*! 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 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 * \param len size of storage buffer. output will be truncated to this length - 1

View File

@ -722,7 +722,7 @@ TEST(Utils, guesspath)
{ {
char buf[256]; char buf[256];
FILE *fp = fopen("test_guesspath.txt", "w"); FILE *fp = fopen("test_guesspath.txt", "w");
#if defined(__linux__) #if defined(__linux__) || (__APPLE__)
const char *path = utils::guesspath(buf, sizeof(buf), fp); const char *path = utils::guesspath(buf, sizeof(buf), fp);
ASSERT_THAT(path, EndsWith("test_guesspath.txt")); ASSERT_THAT(path, EndsWith("test_guesspath.txt"));
#else #else