path_to_executable.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "path_to_executable.h"
  9. #ifdef __APPLE__
  10. # include <mach-o/dyld.h>
  11. #endif
  12. IGL_INLINE std::string igl::path_to_executable()
  13. {
  14. // http://pastebin.com/ffzzxPzi
  15. using namespace std;
  16. std::string path;
  17. char buffer[1024];
  18. uint32_t size = sizeof(buffer);
  19. #if defined (WIN32) || defined (WIN64)
  20. GetModuleFileName(buffer, &size);
  21. path = buffer;
  22. #elif defined (__APPLE__)
  23. if(_NSGetExecutablePath(buffer, &size) == 0)
  24. {
  25. path = buffer;
  26. }
  27. #elif defined(UNIX)
  28. if (readlink("/proc/self/exe", buffer, sizeof(buffer)) == -1)
  29. {
  30. path = buffer;
  31. }
  32. #elif defined(__FreeBSD__)
  33. int mib[4];
  34. mib[0] = CTL_KERN;
  35. mib[1] = KERN_PROC;
  36. mib[2] = KERN_PROC_PATHNAME;
  37. mib[3] = -1;
  38. sysctl(mib, 4, buffer, sizeof(buffer), NULL, 0);
  39. path = buffer;
  40. #elif defined(SUNOS)
  41. path = getexecname();
  42. #endif
  43. return path;
  44. }