* Get path information. */
| 178 | * Get path information. |
| 179 | */ |
| 180 | static void getPath(bool exe, std::vector<std::string> &paths) |
| 181 | { |
| 182 | if (exe) |
| 183 | { |
| 184 | char *path = getenv("PATH"), *save, *dir; |
| 185 | if (path == nullptr) |
| 186 | return; |
| 187 | path = strDup(path); |
| 188 | strtok_r(path, ":", &save); |
| 189 | while ((dir = strtok_r(nullptr, ":", &save)) != nullptr) |
| 190 | paths.push_back(dir); |
| 191 | free(path); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | void *handle = dlopen(nullptr, RTLD_LAZY); |
| 196 | if (handle == nullptr) |
| 197 | return; |
| 198 | Dl_serinfo serinfo_0, *serinfo = nullptr; |
| 199 | if (dlinfo(handle, RTLD_DI_SERINFOSIZE, &serinfo_0) != 0) |
| 200 | { |
| 201 | dlinfo_error: |
| 202 | free(serinfo); |
| 203 | dlclose(handle); |
| 204 | return; |
| 205 | } |
| 206 | serinfo = (Dl_serinfo *)malloc(serinfo_0.dls_size); |
| 207 | if (serinfo == nullptr) |
| 208 | goto dlinfo_error; |
| 209 | if (dlinfo(handle, RTLD_DI_SERINFOSIZE, serinfo) != 0) |
| 210 | goto dlinfo_error; |
| 211 | if (dlinfo(handle, RTLD_DI_SERINFO, serinfo) != 0) |
| 212 | goto dlinfo_error; |
| 213 | for (unsigned i = 0; i < serinfo->dls_cnt; i++) |
| 214 | paths.push_back(serinfo->dls_serpath[i].dls_name); |
| 215 | free(serinfo); |
| 216 | dlclose(handle); |
| 217 | return; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Find an exe file in PATH. |