Finding path to the running binary

I’m working on porting the basic Android tools group to Haiku, using the build system from the android-tools package used on several Linux distros.

I have the build system installed and assembled finally, so now I can actually start on the tools themselves.

The current issue is that this function only knows what to do for linux, APPLE and WIN32:

std::string GetExecutablePath() {
#if defined(__linux__)
  std::string path;
  android::base::Readlink("/proc/self/exe", &path);
  return path;
#elif defined(__APPLE__)
  char path[PATH_MAX + 1];
  uint32_t path_len = sizeof(path);
  int rc = _NSGetExecutablePath(path, &path_len);
  if (rc < 0) {
    std::unique_ptr<char> path_buf(new char[path_len]);
    _NSGetExecutablePath(path_buf.get(), &path_len);
    return path_buf.get();
  }
  return path;
#elif defined(_WIN32)
  char path[PATH_MAX + 1];
  DWORD result = GetModuleFileName(NULL, path, sizeof(path) - 1);
  if (result == 0 || result == sizeof(path) - 1) return "";
  path[PATH_MAX - 1] = 0;
  return path;
#elif defined(__EMSCRIPTEN__)
  abort();
#else
#error unknown OS

So, what’s the Haiku way of getting the path to the currently-running executable?

The standard method is to use the get_next_image_info() call. You can see one example in the recently committed openrct2 patch at HaikuPorts. There are many other examples of using this technique in the HaikuPorts repository.

1 Like

Thanks muchly!