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 | |
20 | #include "config.h" |
21 | #include <wtf/glib/GLibUtilities.h> |
22 | |
23 | #include <glib.h> |
24 | #include <wtf/glib/GUniquePtr.h> |
25 | |
26 | #if OS(WINDOWS) |
27 | #include <windows.h> |
28 | #include <wtf/text/WTFString.h> |
29 | #else |
30 | #include <limits.h> |
31 | #include <unistd.h> |
32 | #endif |
33 | |
34 | #if OS(LINUX) |
35 | CString getCurrentExecutablePath() |
36 | { |
37 | static char readLinkBuffer[PATH_MAX]; |
38 | ssize_t result = readlink("/proc/self/exe" , readLinkBuffer, PATH_MAX); |
39 | if (result == -1) |
40 | return CString(); |
41 | return CString(readLinkBuffer, result); |
42 | } |
43 | #elif OS(HURD) |
44 | CString getCurrentExecutablePath() |
45 | { |
46 | return CString(); |
47 | } |
48 | #elif OS(UNIX) |
49 | CString getCurrentExecutablePath() |
50 | { |
51 | static char readLinkBuffer[PATH_MAX]; |
52 | ssize_t result = readlink("/proc/curproc/file" , readLinkBuffer, PATH_MAX); |
53 | if (result == -1) |
54 | return CString(); |
55 | return CString(readLinkBuffer, result); |
56 | } |
57 | #elif OS(WINDOWS) |
58 | CString getCurrentExecutablePath() |
59 | { |
60 | static WCHAR buffer[MAX_PATH]; |
61 | DWORD length = GetModuleFileNameW(0, buffer, MAX_PATH); |
62 | if (!length || (length == MAX_PATH && GetLastError() == ERROR_INSUFFICIENT_BUFFER)) |
63 | return CString(); |
64 | |
65 | String path(buffer, length); |
66 | return path.utf8(); |
67 | } |
68 | #endif |
69 | |
70 | CString getCurrentExecutableName() |
71 | { |
72 | auto executablePath = getCurrentExecutablePath(); |
73 | if (!executablePath.isNull()) { |
74 | GUniquePtr<char> basename(g_path_get_basename(executablePath.data())); |
75 | return basename.get(); |
76 | } |
77 | |
78 | return g_get_prgname(); |
79 | } |
80 | |