GlibUtilities.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (C) 2010 Igalia, S.L.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this library; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include "config.h"
  20. #include "GlibUtilities.h"
  21. #if OS(WINDOWS)
  22. #include <windows.h>
  23. #include <wtf/text/WTFString.h>
  24. #else
  25. #include <limits.h>
  26. #include <unistd.h>
  27. #endif
  28. #if OS(LINUX)
  29. CString getCurrentExecutablePath()
  30. {
  31. static char readLinkBuffer[PATH_MAX];
  32. ssize_t result = readlink("/proc/self/exe", readLinkBuffer, PATH_MAX);
  33. if (result == -1)
  34. return CString();
  35. return CString(readLinkBuffer, result);
  36. }
  37. #elif OS(UNIX)
  38. CString getCurrentExecutablePath()
  39. {
  40. static char readLinkBuffer[PATH_MAX];
  41. ssize_t result = readlink("/proc/curproc/file", readLinkBuffer, PATH_MAX);
  42. if (result == -1)
  43. return CString();
  44. return CString(readLinkBuffer, result);
  45. }
  46. #elif OS(WINDOWS)
  47. CString getCurrentExecutablePath()
  48. {
  49. static WCHAR buffer[MAX_PATH];
  50. DWORD length = GetModuleFileNameW(0, buffer, MAX_PATH);
  51. if (!length || (length == MAX_PATH && GetLastError() == ERROR_INSUFFICIENT_BUFFER))
  52. return CString();
  53. String path(buffer, length);
  54. return path.utf8();
  55. }
  56. #endif