Mercurial > minori
diff dep/animone/src/fd/xnu.cc @ 299:246017a7907a
dep/animone: clean up OS X code
GetProcessName() really belongs in fd.cc after removing the
stupid unnecessary LaunchServices code that was stolen from...
some library :)
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 13 May 2024 14:15:47 -0400 |
parents | 0718f538c5f9 |
children | b1f625b0227c |
line wrap: on
line diff
--- a/dep/animone/src/fd/xnu.cc Mon May 13 03:28:42 2024 -0400 +++ b/dep/animone/src/fd/xnu.cc Mon May 13 14:15:47 2024 -0400 @@ -91,4 +91,44 @@ return true; } +static bool GetProcessNameFromProcPidPath(pid_t pid, std::string& result) { + result.assign(PROC_PIDPATHINFO_MAXSIZE, '\0'); + + int ret = proc_pidpath(pid, result.data(), result.size() * sizeof(char)); + if (ret <= 0) + return false; + + /* find the last slash, if there's none, we're done here */ + size_t last_slash = result.rfind('/'); + if (last_slash == std::string::npos) + return true; + + result.erase(0, last_slash + 1); + return true; +} + +static bool GetProcessNameFromProcName(pid_t pid, std::string& result) { + result.assign(2 * MAXCOMLEN, '\0'); + + int size = proc_name(pid, &result.front(), result.length()); + + /* if size is MAXCOMLEN or 2 * MAXCOMLEN, assume + * this method won't work and our result is truncated */ + if (size <= 0 || size == MAXCOMLEN || size == 2 * MAXCOMLEN) + return false; + + result.resize(size); + return true; +} + +bool GetProcessName(pid_t pid, std::string& result) { + if (GetProcessNameFromProcName(pid, result)) + return true; + + if (GetProcessNameFromProcPidPath(pid, result)) + return true; + + return false; +} + } // namespace animone::internal::xnu