whereami.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses
  2. // without any warranty.
  3. // by Gregory Pakosz (@gpakosz)
  4. // https://github.com/gpakosz/whereami
  5. #ifndef WHEREAMI_H
  6. #define WHEREAMI_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #ifndef WAI_FUNCSPEC
  11. #define WAI_FUNCSPEC
  12. #endif
  13. #ifndef WAI_PREFIX
  14. #define WAI_PREFIX(function) wai_##function
  15. #endif
  16. /**
  17. * Returns the path to the current executable.
  18. *
  19. * Usage:
  20. * - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to
  21. * retrieve the length of the path
  22. * - allocate the destination buffer with `path = (char*)malloc(length + 1);`
  23. * - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the
  24. * path
  25. * - add a terminal NUL character with `path[length] = '\0';`
  26. *
  27. * @param out destination buffer, optional
  28. * @param capacity destination buffer capacity
  29. * @param dirname_length optional recipient for the length of the dirname part
  30. * of the path.
  31. *
  32. * @return the length of the executable path on success (without a terminal NUL
  33. * character), otherwise `-1`
  34. */
  35. WAI_FUNCSPEC
  36. int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length);
  37. /**
  38. * Returns the path to the current module
  39. *
  40. * Usage:
  41. * - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve
  42. * the length of the path
  43. * - allocate the destination buffer with `path = (char*)malloc(length + 1);`
  44. * - call `wai_getModulePath(path, length, NULL)` again to retrieve the path
  45. * - add a terminal NUL character with `path[length] = '\0';`
  46. *
  47. * @param out destination buffer, optional
  48. * @param capacity destination buffer capacity
  49. * @param dirname_length optional recipient for the length of the dirname part
  50. * of the path.
  51. *
  52. * @return the length of the module path on success (without a terminal NUL
  53. * character), otherwise `-1`
  54. */
  55. WAI_FUNCSPEC
  56. int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length);
  57. #ifdef __cplusplus
  58. }
  59. #endif
  60. #endif // #ifndef WHEREAMI_H