Mercurial > minori
comparison 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 |
comparison
equal
deleted
inserted
replaced
298:dec4d3c9a909 | 299:246017a7907a |
---|---|
89 } | 89 } |
90 | 90 |
91 return true; | 91 return true; |
92 } | 92 } |
93 | 93 |
94 static bool GetProcessNameFromProcPidPath(pid_t pid, std::string& result) { | |
95 result.assign(PROC_PIDPATHINFO_MAXSIZE, '\0'); | |
96 | |
97 int ret = proc_pidpath(pid, result.data(), result.size() * sizeof(char)); | |
98 if (ret <= 0) | |
99 return false; | |
100 | |
101 /* find the last slash, if there's none, we're done here */ | |
102 size_t last_slash = result.rfind('/'); | |
103 if (last_slash == std::string::npos) | |
104 return true; | |
105 | |
106 result.erase(0, last_slash + 1); | |
107 return true; | |
108 } | |
109 | |
110 static bool GetProcessNameFromProcName(pid_t pid, std::string& result) { | |
111 result.assign(2 * MAXCOMLEN, '\0'); | |
112 | |
113 int size = proc_name(pid, &result.front(), result.length()); | |
114 | |
115 /* if size is MAXCOMLEN or 2 * MAXCOMLEN, assume | |
116 * this method won't work and our result is truncated */ | |
117 if (size <= 0 || size == MAXCOMLEN || size == 2 * MAXCOMLEN) | |
118 return false; | |
119 | |
120 result.resize(size); | |
121 return true; | |
122 } | |
123 | |
124 bool GetProcessName(pid_t pid, std::string& result) { | |
125 if (GetProcessNameFromProcName(pid, result)) | |
126 return true; | |
127 | |
128 if (GetProcessNameFromProcPidPath(pid, result)) | |
129 return true; | |
130 | |
131 return false; | |
132 } | |
133 | |
94 } // namespace animone::internal::xnu | 134 } // namespace animone::internal::xnu |