#include "ProcessCwd.h" #ifdef __APPLE__ #include #elifdef __linux__ #include #include #include #endif #ifdef __APPLE__ QString processCwd(qint64 pid) { if (pid <= 0) { return {}; } struct proc_vnodepathinfo info{}; const int ret = proc_pidinfo(static_cast(pid), PROC_PIDVNODEPATHINFO, 0, &info, sizeof(info)); if (ret != sizeof(info)) { return {}; } const char* path = info.pvi_cdir.vip_path; if (path[0] == '\0') { return {}; } return QString::fromLocal8Bit(path); } #elifdef __linux__ QString processCwd(qint64 pid) { if (pid <= 0) { return {}; } std::error_code ec; const std::filesystem::path link = std::filesystem::read_symlink( "/proc/" + std::to_string(pid) + "/cwd", ec); if (ec) { return {}; } const std::string path = link.string(); if (path.empty()) { return {}; } return QString::fromLocal8Bit(path.data(), static_cast(path.size())); } #else QString processCwd(qint64 pid) { Q_UNUSED(pid); return {}; } #endif