path_to_executable.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #if defined(_WIN32)
  13. # include <windows.h>
  14. #endif
  15. #include <stdint.h>
  16. IGL_INLINE std::string igl::path_to_executable()
  17. {
  18. // http://pastebin.com/ffzzxPzi
  19. using namespace std;
  20. std::string path;
  21. char buffer[1024];
  22. uint32_t size = sizeof(buffer);
  23. #if defined (WIN32)
  24. GetModuleFileName(nullptr,buffer,size);
  25. path = buffer;
  26. #elif defined (__APPLE__)
  27. if(_NSGetExecutablePath(buffer, &size) == 0)
  28. {
  29. path = buffer;
  30. }
  31. #elif defined(UNIX)
  32. if (readlink("/proc/self/exe", buffer, sizeof(buffer)) == -1)
  33. {
  34. path = buffer;
  35. }
  36. #elif defined(__FreeBSD__)
  37. int mib[4];
  38. mib[0] = CTL_KERN;
  39. mib[1] = KERN_PROC;
  40. mib[2] = KERN_PROC_PATHNAME;
  41. mib[3] = -1;
  42. sysctl(mib, 4, buffer, sizeof(buffer), NULL, 0);
  43. path = buffer;
  44. #elif defined(SUNOS)
  45. path = getexecname();
  46. #endif
  47. return path;
  48. }