1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is mozilla.org code
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Mike Hommey <mh@glandium.org>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef mozilla_BinaryPath_h
39 : #define mozilla_BinaryPath_h
40 :
41 : #include "nsXPCOMPrivate.h" // for MAXPATHLEN
42 : #include "prtypes.h"
43 : #ifdef XP_WIN
44 : #include <windows.h>
45 : #elif defined(XP_MACOSX)
46 : #include <CoreFoundation/CoreFoundation.h>
47 : #elif defined(XP_UNIX)
48 : #include <sys/stat.h>
49 : #include <string.h>
50 : #endif
51 :
52 : namespace mozilla {
53 :
54 : class BinaryPath {
55 : public:
56 : #ifdef XP_WIN
57 : static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
58 : {
59 : PRUnichar wide_path[MAXPATHLEN];
60 : nsresult rv = GetW(argv0, wide_path);
61 : if (NS_FAILED(rv))
62 : return rv;
63 : WideCharToMultiByte(CP_UTF8, 0, wide_path, -1,
64 : aResult, MAXPATHLEN, NULL, NULL);
65 : return NS_OK;
66 : }
67 :
68 : private:
69 : static nsresult GetW(const char *argv0, PRUnichar aResult[MAXPATHLEN])
70 : {
71 : if (::GetModuleFileNameW(0, aResult, MAXPATHLEN))
72 : return NS_OK;
73 : return NS_ERROR_FAILURE;
74 : }
75 :
76 : #elif defined(XP_MACOSX)
77 : static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
78 : {
79 : // Works even if we're not bundled.
80 : CFBundleRef appBundle = CFBundleGetMainBundle();
81 : if (!appBundle)
82 : return NS_ERROR_FAILURE;
83 :
84 : CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
85 : if (!executableURL)
86 : return NS_ERROR_FAILURE;
87 :
88 : nsresult rv;
89 : if (CFURLGetFileSystemRepresentation(executableURL, false, (UInt8 *)aResult, MAXPATHLEN))
90 : rv = NS_OK;
91 : else
92 : rv = NS_ERROR_FAILURE;
93 : CFRelease(executableURL);
94 : return rv;
95 : }
96 :
97 : #elif defined(MOZ_WIDGET_ANDROID)
98 : static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
99 : {
100 : // On Android, we use the GRE_HOME variable that is set by the Java
101 : // bootstrap code.
102 : const char *greHome = getenv("GRE_HOME");
103 : if (!greHome)
104 : return NS_ERROR_FAILURE;
105 :
106 : snprintf(aResult, MAXPATHLEN, "%s/%s", greHome, "dummy");
107 : aResult[MAXPATHLEN] = '\0';
108 : return NS_OK;
109 : }
110 :
111 : #elif defined(XP_UNIX)
112 1480 : static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
113 : {
114 : struct stat fileStat;
115 : // on unix, there is no official way to get the path of the current binary.
116 : // instead of using the MOZILLA_FIVE_HOME hack, which doesn't scale to
117 : // multiple applications, we will try a series of techniques:
118 : //
119 : // 1) use realpath() on argv[0], which works unless we're loaded from the
120 : // PATH. Only do so if argv[0] looks like a path (contains a /).
121 : // 2) manually walk through the PATH and look for ourself
122 : // 3) give up
123 2960 : if (strchr(argv0, '/') && realpath(argv0, aResult) &&
124 1480 : stat(aResult, &fileStat) == 0)
125 1480 : return NS_OK;
126 :
127 0 : const char *path = getenv("PATH");
128 0 : if (!path)
129 0 : return NS_ERROR_FAILURE;
130 :
131 0 : char *pathdup = strdup(path);
132 0 : if (!pathdup)
133 0 : return NS_ERROR_OUT_OF_MEMORY;
134 :
135 0 : bool found = false;
136 0 : char *token = strtok(pathdup, ":");
137 0 : while (token) {
138 : char tmpPath[MAXPATHLEN];
139 0 : sprintf(tmpPath, "%s/%s", token, argv0);
140 0 : if (realpath(tmpPath, aResult) && stat(aResult, &fileStat) == 0) {
141 0 : found = true;
142 0 : break;
143 : }
144 0 : token = strtok(NULL, ":");
145 : }
146 0 : free(pathdup);
147 0 : if (found)
148 0 : return NS_OK;
149 0 : return NS_ERROR_FAILURE;
150 : }
151 :
152 : #elif defined(XP_OS2)
153 : static nsresult Get(const char *argv0, char aResult[MAXPATHLEN])
154 : {
155 : PPIB ppib;
156 : PTIB ptib;
157 : DosGetInfoBlocks( &ptib, &ppib);
158 : DosQueryModuleName(ppib->pib_hmte, MAXPATHLEN, aResult);
159 : }
160 :
161 : #else
162 : #error Oops, you need platform-specific code here
163 : #endif
164 :
165 : public:
166 1434 : static nsresult GetFile(const char *argv0, nsILocalFile* *aResult)
167 : {
168 2868 : nsCOMPtr<nsILocalFile> lf;
169 : #ifdef XP_WIN
170 : PRUnichar exePath[MAXPATHLEN];
171 : nsresult rv = GetW(argv0, exePath);
172 : #else
173 : char exePath[MAXPATHLEN];
174 1434 : nsresult rv = Get(argv0, exePath);
175 : #endif
176 1434 : if (NS_FAILED(rv))
177 0 : return rv;
178 : #ifdef XP_WIN
179 : rv = NS_NewLocalFile(nsDependentString(exePath), true,
180 : getter_AddRefs(lf));
181 : #else
182 1434 : rv = NS_NewNativeLocalFile(nsDependentCString(exePath), true,
183 2868 : getter_AddRefs(lf));
184 : #endif
185 1434 : if (NS_FAILED(rv))
186 0 : return rv;
187 1434 : NS_ADDREF(*aResult = lf);
188 1434 : return NS_OK;
189 : }
190 : };
191 :
192 : }
193 :
194 : #endif /* mozilla_BinaryPath_h */
|