path_to_executable.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <[email protected]>
  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. #else
  15. #include <unistd.h>
  16. #endif
  17. #include <stdint.h>
  18. IGL_INLINE std::string igl::path_to_executable()
  19. {
  20. // http://pastebin.com/ffzzxPzi
  21. using namespace std;
  22. std::string path;
  23. char buffer[1024];
  24. uint32_t size = sizeof(buffer);
  25. #if defined (WIN32)
  26. GetModuleFileName(nullptr,buffer,size);
  27. path = buffer;
  28. #elif defined (__APPLE__)
  29. if(_NSGetExecutablePath(buffer, &size) == 0)
  30. {
  31. path = buffer;
  32. }
  33. #elif defined(UNIX) || defined(unix) || defined(__unix) || defined(__unix__)
  34. int byte_count = readlink("/proc/self/exe", buffer, size);
  35. if (byte_count != -1)
  36. {
  37. path = std::string(buffer, byte_count);
  38. }
  39. #elif defined(__FreeBSD__)
  40. int mib[4];
  41. mib[0] = CTL_KERN;
  42. mib[1] = KERN_PROC;
  43. mib[2] = KERN_PROC_PATHNAME;
  44. mib[3] = -1;
  45. sysctl(mib, 4, buffer, sizeof(buffer), NULL, 0);
  46. path = buffer;
  47. #elif defined(SUNOS)
  48. path = getexecname();
  49. #endif
  50. return path;
  51. }