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 "GlibUtilities.h"
22
23#if OS(WINDOWS)
24#include <windows.h>
25#include <wtf/text/WTFString.h>
26#else
27#include <limits.h>
28#include <unistd.h>
29#endif
30
31#if OS(LINUX)
32CString getCurrentExecutablePath()
33{
34    static char readLinkBuffer[PATH_MAX];
35    ssize_t result = readlink("/proc/self/exe", readLinkBuffer, PATH_MAX);
36    if (result == -1)
37        return CString();
38    return CString(readLinkBuffer, result);
39}
40#elif OS(HURD)
41CString getCurrentExecutablePath()
42{
43    return CString();
44}
45#elif OS(UNIX)
46CString getCurrentExecutablePath()
47{
48    static char readLinkBuffer[PATH_MAX];
49    ssize_t result = readlink("/proc/curproc/file", readLinkBuffer, PATH_MAX);
50    if (result == -1)
51        return CString();
52    return CString(readLinkBuffer, result);
53}
54#elif OS(WINDOWS)
55CString getCurrentExecutablePath()
56{
57    static WCHAR buffer[MAX_PATH];
58    DWORD length = GetModuleFileNameW(0, buffer, MAX_PATH);
59    if (!length || (length == MAX_PATH && GetLastError() == ERROR_INSUFFICIENT_BUFFER))
60        return CString();
61
62    String path(buffer, length);
63    return path.utf8();
64}
65#endif
66