Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
| 32 | /* Return the initial module search path. */ |
Guido van Rossum | c499572 | 1998-08-08 20:05:31 +0000 | [diff] [blame] | 33 | /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 35 | /* ---------------------------------------------------------------- |
| 36 | PATH RULES FOR WINDOWS: |
| 37 | This describes how sys.path is formed on Windows. It describes the |
| 38 | functionality, not the implementation (ie, the order in which these |
| 39 | are actually fetched is different) |
| 40 | |
| 41 | * Python always adds an empty entry at the start, which corresponds |
| 42 | to the current directory. |
| 43 | |
| 44 | * If the PYTHONPATH env. var. exists, it's entries are added next. |
| 45 | |
| 46 | * We look in the registry for "application paths" - that is, sub-keys |
| 47 | under the main PythonPath registry key. These are added next (the |
| 48 | order of sub-key processing is undefined). |
| 49 | HKEY_CURRENT_USER is searched and added first. |
| 50 | HKEY_LOCAL_MACHINE is searched and added next. |
| 51 | (Note that all known installers only use HKLM, so HKCU is typically |
| 52 | empty) |
| 53 | |
| 54 | * We attempt to locate the "Python Home" - if the PYTHONHOME env var |
| 55 | is set, we believe it. Otherwise, we use the path of our host .EXE's |
| 56 | to try and locate our "landmark" (lib\\string.py) and deduce our home. |
| 57 | - If we DO have a Python Home: The relevant sub-directories (Lib, |
| 58 | plat-win, lib-tk, etc) are based on the Python Home |
| 59 | - If we DO NOT have a Python Home, the core Python Path is |
| 60 | loaded from the registry. This is the main PythonPath key, |
| 61 | and both HKLM and HKCU are combined to form the path) |
| 62 | |
| 63 | * Iff - we can not locate the Python Home, have not had a PYTHONPATH |
| 64 | specified, and can't locate any Registry entries (ie, we have _nothing_ |
| 65 | we can assume is a good path), a default path with relative entries is |
| 66 | used (eg. .\Lib;.\plat-win, etc) |
| 67 | |
| 68 | |
| 69 | The end result of all this is: |
| 70 | * When running python.exe, or any other .exe in the main Python directory |
| 71 | (either an installed version, or directly from the PCbuild directory), |
| 72 | the core path is deduced, and the core paths in the registry are |
| 73 | ignored. Other "application paths" in the registry are always read. |
| 74 | |
| 75 | * When Python is hosted in another exe (different directory, embedded via |
| 76 | COM, etc), the Python Home will not be deduced, so the core path from |
| 77 | the registry is used. Other "application paths "in the registry are |
| 78 | always read. |
| 79 | |
| 80 | * If Python can't find its home and there is no registry (eg, frozen |
| 81 | exe, some very strange installation setup) you get a path with |
| 82 | some default, but relative, paths. |
| 83 | |
| 84 | ---------------------------------------------------------------- */ |
| 85 | |
| 86 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 87 | #include "Python.h" |
| 88 | #include "osdefs.h" |
| 89 | |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 90 | #ifdef MS_WIN32 |
| 91 | #include <windows.h> |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 92 | #include <tchar.h> |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 95 | #include <sys/types.h> |
| 96 | #include <sys/stat.h> |
| 97 | #include <string.h> |
| 98 | |
| 99 | #if HAVE_UNISTD_H |
| 100 | #include <unistd.h> |
| 101 | #endif /* HAVE_UNISTD_H */ |
| 102 | |
| 103 | /* Search in some common locations for the associated Python libraries. |
| 104 | * |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 105 | * Py_GetPath() tries to return a sensible Python module search path. |
| 106 | * |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 107 | * The approach is an adaptation for Windows of the strategy used in |
| 108 | * ../Modules/getpath.c; it uses the Windows Registry as one of its |
| 109 | * information sources. |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 110 | */ |
| 111 | |
| 112 | #ifndef LANDMARK |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 113 | #define LANDMARK "lib\\string.py" |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 114 | #endif |
| 115 | |
| 116 | static char prefix[MAXPATHLEN+1]; |
| 117 | static char progpath[MAXPATHLEN+1]; |
| 118 | static char *module_search_path = NULL; |
| 119 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 121 | static int |
| 122 | is_sep(ch) /* determine if "ch" is a separator character */ |
| 123 | char ch; |
| 124 | { |
| 125 | #ifdef ALTSEP |
| 126 | return ch == SEP || ch == ALTSEP; |
| 127 | #else |
| 128 | return ch == SEP; |
| 129 | #endif |
| 130 | } |
| 131 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 133 | static void |
| 134 | reduce(dir) |
| 135 | char *dir; |
| 136 | { |
| 137 | int i = strlen(dir); |
| 138 | while (i > 0 && !is_sep(dir[i])) |
| 139 | --i; |
| 140 | dir[i] = '\0'; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | static int |
| 145 | exists(filename) |
| 146 | char *filename; |
| 147 | { |
| 148 | struct stat buf; |
| 149 | return stat(filename, &buf) == 0; |
| 150 | } |
| 151 | |
| 152 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 153 | static int |
| 154 | ismodule(filename) /* Is module -- check for .pyc/.pyo too */ |
| 155 | char *filename; |
| 156 | { |
| 157 | if (exists(filename)) |
| 158 | return 1; |
| 159 | |
| 160 | /* Check for the compiled version of prefix. */ |
| 161 | if (strlen(filename) < MAXPATHLEN) { |
| 162 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 163 | if (exists(filename)) |
| 164 | return 1; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 170 | static void |
| 171 | join(buffer, stuff) |
| 172 | char *buffer; |
| 173 | char *stuff; |
| 174 | { |
| 175 | int n, k; |
| 176 | if (is_sep(stuff[0])) |
| 177 | n = 0; |
| 178 | else { |
| 179 | n = strlen(buffer); |
| 180 | if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN) |
| 181 | buffer[n++] = SEP; |
| 182 | } |
| 183 | k = strlen(stuff); |
| 184 | if (n + k > MAXPATHLEN) |
| 185 | k = MAXPATHLEN - n; |
| 186 | strncpy(buffer+n, stuff, k); |
| 187 | buffer[n+k] = '\0'; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static int |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 192 | gotlandmark(landmark) |
| 193 | char *landmark; |
| 194 | { |
| 195 | int n, ok; |
| 196 | |
| 197 | n = strlen(prefix); |
| 198 | join(prefix, landmark); |
| 199 | ok = ismodule(prefix); |
| 200 | prefix[n] = '\0'; |
| 201 | return ok; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | static int |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 206 | search_for_prefix(argv0_path, landmark) |
| 207 | char *argv0_path; |
| 208 | char *landmark; |
| 209 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 210 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 211 | /* Search from argv0_path, until landmark is found */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 212 | strcpy(prefix, argv0_path); |
| 213 | do { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 214 | if (gotlandmark(landmark)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 215 | return 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 216 | reduce(prefix); |
| 217 | } while (prefix[0]); |
| 218 | return 0; |
| 219 | } |
| 220 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 221 | #ifdef MS_WIN32 |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 223 | /* a string loaded from the DLL at startup.*/ |
| 224 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 226 | |
| 227 | /* Load a PYTHONPATH value from the registry. |
| 228 | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
| 229 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 230 | Works in both Unicode and 8bit environments. Only uses the |
| 231 | Ex family of functions so it also works with Windows CE. |
| 232 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 233 | Returns NULL, or a pointer that should be freed. |
| 234 | */ |
| 235 | |
| 236 | static char * |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 237 | getpythonregpath(HKEY keyBase, int skipcore) |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 238 | { |
| 239 | HKEY newKey = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 240 | DWORD dataSize = 0; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 241 | DWORD numKeys = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 242 | LONG rc; |
| 243 | char *retval = NULL; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 244 | TCHAR *dataBuf = NULL; |
| 245 | static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\"); |
| 246 | static const TCHAR keySuffix[] = _T("\\PythonPath"); |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 247 | int versionLen; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 248 | DWORD index; |
| 249 | TCHAR *keyBuf = NULL; |
| 250 | TCHAR *keyBufPtr; |
| 251 | TCHAR **ppPaths = NULL; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 253 | /* Tried to use sysget("winver") but here is too early :-( */ |
| 254 | versionLen = _tcslen(PyWin_DLLVersionString); |
| 255 | /* Space for all the chars, plus one \0 */ |
| 256 | keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) + |
| 257 | sizeof(TCHAR)*(versionLen-1) + |
| 258 | sizeof(keySuffix)); |
| 259 | if (keyBuf==NULL) goto done; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 261 | memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR)); |
| 262 | keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1; |
| 263 | memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR)); |
| 264 | keyBufPtr += versionLen; |
| 265 | /* NULL comes with this one! */ |
| 266 | memcpy(keyBufPtr, keySuffix, sizeof(keySuffix)); |
| 267 | /* Open the root Python key */ |
| 268 | rc=RegOpenKeyEx(keyBase, |
| 269 | keyBuf, /* subkey */ |
| 270 | 0, /* reserved */ |
| 271 | KEY_READ, |
| 272 | &newKey); |
| 273 | if (rc!=ERROR_SUCCESS) goto done; |
| 274 | /* Find out how big our core buffer is, and how many subkeys we have */ |
| 275 | rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL, |
| 276 | NULL, NULL, &dataSize, NULL, NULL); |
| 277 | if (rc!=ERROR_SUCCESS) goto done; |
| 278 | if (skipcore) dataSize = 0; /* Only count core ones if we want them! */ |
| 279 | /* Allocate a temp array of char buffers, so we only need to loop |
| 280 | reading the registry once |
| 281 | */ |
| 282 | ppPaths = malloc( sizeof(TCHAR *) * numKeys ); |
| 283 | if (ppPaths==NULL) goto done; |
| 284 | memset(ppPaths, 0, sizeof(TCHAR *) * numKeys); |
| 285 | /* Loop over all subkeys, allocating a temp sub-buffer. */ |
| 286 | for(index=0;index<numKeys;index++) { |
| 287 | TCHAR keyBuf[MAX_PATH+1]; |
| 288 | HKEY subKey = 0; |
| 289 | DWORD reqdSize = MAX_PATH+1; |
| 290 | /* Get the sub-key name */ |
| 291 | DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize, |
| 292 | NULL, NULL, NULL, NULL ); |
| 293 | if (rc!=ERROR_SUCCESS) goto done; |
| 294 | /* Open the sub-key */ |
| 295 | rc=RegOpenKeyEx(newKey, |
| 296 | keyBuf, /* subkey */ |
| 297 | 0, /* reserved */ |
| 298 | KEY_READ, |
| 299 | &subKey); |
| 300 | if (rc!=ERROR_SUCCESS) goto done; |
| 301 | /* Find the value of the buffer size, malloc, then read it */ |
| 302 | RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize); |
| 303 | if (reqdSize) { |
| 304 | ppPaths[index] = malloc(reqdSize); |
| 305 | if (ppPaths[index]) { |
| 306 | RegQueryValueEx(subKey, NULL, 0, NULL, (LPBYTE)ppPaths[index], &reqdSize); |
| 307 | dataSize += reqdSize + 1; /* 1 for the ";" */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 310 | RegCloseKey(subKey); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 311 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 312 | dataBuf = malloc((dataSize+1) * sizeof(TCHAR)); |
| 313 | if (dataBuf) { |
| 314 | TCHAR *szCur = dataBuf; |
| 315 | DWORD reqdSize = dataSize; |
| 316 | /* Copy our collected strings */ |
| 317 | for (index=0;index<numKeys;index++) { |
| 318 | int len; |
| 319 | if (index > 0) { |
| 320 | *(szCur++) = _T(';'); |
| 321 | dataSize--; |
| 322 | } |
| 323 | len = _tcslen(ppPaths[index]); |
| 324 | _tcsncpy(szCur, ppPaths[index], len); |
| 325 | szCur += len; |
| 326 | dataSize -= len; |
| 327 | } |
| 328 | if (skipcore) |
| 329 | *szCur = '\0'; |
| 330 | else { |
| 331 | *(szCur++) = _T(';'); |
| 332 | dataSize--; |
| 333 | /* Now append the core path entries - this will include the NULL */ |
| 334 | rc = RegQueryValueEx(newKey, NULL, 0, NULL, (LPBYTE)szCur, &dataSize); |
| 335 | } |
| 336 | /* And set the result - caller must free |
| 337 | If MBCS, it is fine as is. If Unicode, allocate new |
| 338 | buffer and convert. |
| 339 | */ |
| 340 | #ifdef UNICODE |
| 341 | retval = (char *)malloc(reqdSize+1); |
| 342 | if (retval) |
| 343 | WideCharToMultiByte(CP_ACP, 0, |
| 344 | dataBuf, -1, /* source */ |
| 345 | retval, dataSize+1, /* dest */ |
| 346 | NULL, NULL); |
| 347 | free(dataBuf); |
| 348 | #else |
| 349 | retval = dataBuf; |
| 350 | #endif |
| 351 | } |
| 352 | done: |
| 353 | /* Loop freeing my temp buffers */ |
| 354 | if (ppPaths) { |
| 355 | for(index=0;index<numKeys;index++) |
| 356 | if (ppPaths[index]) free(ppPaths[index]); |
| 357 | free(ppPaths); |
| 358 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 359 | if (newKey) |
| 360 | RegCloseKey(newKey); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 361 | if (keyBuf) |
| 362 | free(keyBuf); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 363 | return retval; |
| 364 | } |
| 365 | #endif /* MS_WIN32 */ |
| 366 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 367 | static void |
| 368 | get_progpath() |
| 369 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 370 | extern char *Py_GetProgramName(); |
| 371 | char *path = getenv("PATH"); |
| 372 | char *prog = Py_GetProgramName(); |
| 373 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 374 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 375 | #ifdef UNICODE |
| 376 | WCHAR wprogpath[MAXPATHLEN+1]; |
| 377 | if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) { |
| 378 | WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL); |
| 379 | return; |
| 380 | } |
| 381 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 382 | if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) |
| 383 | return; |
| 384 | #endif |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 385 | #endif |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 386 | if (prog == NULL || *prog == '\0') |
| 387 | prog = "python"; |
| 388 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 389 | /* If there is no slash in the argv0 path, then we have to |
| 390 | * assume python is on the user's $PATH, since there's no |
| 391 | * other way to find a directory to start the search from. If |
| 392 | * $PATH isn't exported, you lose. |
| 393 | */ |
| 394 | #ifdef ALTSEP |
| 395 | if (strchr(prog, SEP) || strchr(prog, ALTSEP)) |
| 396 | #else |
| 397 | if (strchr(prog, SEP)) |
| 398 | #endif |
| 399 | strcpy(progpath, prog); |
| 400 | else if (path) { |
| 401 | while (1) { |
| 402 | char *delim = strchr(path, DELIM); |
| 403 | |
| 404 | if (delim) { |
| 405 | int len = delim - path; |
| 406 | strncpy(progpath, path, len); |
| 407 | *(progpath + len) = '\0'; |
| 408 | } |
| 409 | else |
| 410 | strcpy(progpath, path); |
| 411 | |
| 412 | join(progpath, prog); |
| 413 | if (exists(progpath)) |
| 414 | break; |
| 415 | |
| 416 | if (!delim) { |
| 417 | progpath[0] = '\0'; |
| 418 | break; |
| 419 | } |
| 420 | path = delim + 1; |
| 421 | } |
| 422 | } |
| 423 | else |
| 424 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void |
| 428 | calculate_path() |
| 429 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 430 | char argv0_path[MAXPATHLEN+1]; |
| 431 | char *buf; |
| 432 | int bufsz; |
Guido van Rossum | 8b2b3ce | 1998-07-27 13:48:07 +0000 | [diff] [blame] | 433 | char *pythonhome = Py_GetPythonHome(); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 434 | char *envpath = getenv("PYTHONPATH"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 436 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 437 | int skiphome, skipdefault; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 438 | char *machinepath = NULL; |
| 439 | char *userpath = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 440 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 441 | |
| 442 | get_progpath(); |
| 443 | strcpy(argv0_path, progpath); |
| 444 | reduce(argv0_path); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 445 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 446 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 447 | pythonhome = prefix; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 448 | else |
| 449 | pythonhome = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 450 | } |
| 451 | else |
| 452 | strcpy(prefix, pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 453 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 454 | if (envpath && *envpath == '\0') |
| 455 | envpath = NULL; |
| 456 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 457 | |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 458 | #ifdef MS_WIN32 |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 459 | skiphome = pythonhome==NULL ? 0 : 1; |
| 460 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 461 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
| 462 | /* We only use the default relative PYTHONPATH if we havent |
| 463 | anything better to use! */ |
| 464 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 465 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 466 | #endif |
| 467 | |
| 468 | /* We need to construct a path from the following parts. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 469 | (1) the PYTHONPATH environment variable, if set; |
| 470 | (2) for Win32, the machinepath and userpath, if set; |
| 471 | (3) the PYTHONPATH config macro, with the leading "." |
| 472 | of each component replaced with pythonhome, if set; |
| 473 | (4) the directory containing the executable (argv0_path). |
| 474 | The length calculation calculates #3 first. |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 475 | Extra rules: |
| 476 | - If PYTHONHOME is set (in any way) item (2) is ignored. |
| 477 | - If registry values are used, (3) and (4) are ignored. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 478 | */ |
| 479 | |
| 480 | /* Calculate size of return buffer */ |
| 481 | if (pythonhome != NULL) { |
| 482 | char *p; |
| 483 | bufsz = 1; |
| 484 | for (p = PYTHONPATH; *p; p++) { |
| 485 | if (*p == DELIM) |
| 486 | bufsz++; /* number of DELIM plus one */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 487 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 488 | bufsz *= strlen(pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 489 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 490 | else |
| 491 | bufsz = 0; |
Guido van Rossum | 691d2ad | 1997-12-11 02:32:43 +0000 | [diff] [blame] | 492 | bufsz += strlen(PYTHONPATH) + 1; |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 493 | bufsz += strlen(argv0_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 494 | #ifdef MS_WIN32 |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 495 | if (userpath) |
| 496 | bufsz += strlen(userpath) + 1; |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 497 | if (machinepath) |
| 498 | bufsz += strlen(machinepath) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 499 | #endif |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 500 | if (envpath != NULL) |
| 501 | bufsz += strlen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 502 | |
| 503 | module_search_path = buf = malloc(bufsz); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 504 | if (buf == NULL) { |
| 505 | /* We can't exit, so print a warning and limp along */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 506 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 507 | if (envpath) { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 508 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 509 | module_search_path = envpath; |
| 510 | } |
| 511 | else { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 512 | fprintf(stderr, "Using default static path.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 513 | module_search_path = PYTHONPATH; |
| 514 | } |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 515 | #ifdef MS_WIN32 |
| 516 | if (machinepath) |
| 517 | free(machinepath); |
| 518 | if (userpath) |
| 519 | free(userpath); |
| 520 | #endif /* MS_WIN32 */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 521 | return; |
| 522 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 523 | |
| 524 | if (envpath) { |
| 525 | strcpy(buf, envpath); |
| 526 | buf = strchr(buf, '\0'); |
| 527 | *buf++ = DELIM; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 528 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 529 | #ifdef MS_WIN32 |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 530 | if (userpath) { |
| 531 | strcpy(buf, userpath); |
| 532 | buf = strchr(buf, '\0'); |
| 533 | *buf++ = DELIM; |
| 534 | free(userpath); |
| 535 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 536 | if (machinepath) { |
| 537 | strcpy(buf, machinepath); |
| 538 | buf = strchr(buf, '\0'); |
| 539 | *buf++ = DELIM; |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 540 | free(machinepath); |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 541 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 542 | if (pythonhome == NULL) { |
| 543 | if (!skipdefault) { |
| 544 | strcpy(buf, PYTHONPATH); |
| 545 | buf = strchr(buf, '\0'); |
| 546 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 547 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 548 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 549 | if (pythonhome == NULL) { |
| 550 | strcpy(buf, PYTHONPATH); |
| 551 | buf = strchr(buf, '\0'); |
| 552 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 553 | #endif /* MS_WIN32 */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 554 | else { |
| 555 | char *p = PYTHONPATH; |
| 556 | char *q; |
| 557 | int n; |
| 558 | for (;;) { |
| 559 | q = strchr(p, DELIM); |
| 560 | if (q == NULL) |
| 561 | n = strlen(p); |
| 562 | else |
| 563 | n = q-p; |
| 564 | if (p[0] == '.' && is_sep(p[1])) { |
| 565 | strcpy(buf, pythonhome); |
| 566 | buf = strchr(buf, '\0'); |
| 567 | p++; |
| 568 | n--; |
| 569 | } |
| 570 | strncpy(buf, p, n); |
| 571 | buf += n; |
| 572 | if (q == NULL) |
| 573 | break; |
| 574 | *buf++ = DELIM; |
| 575 | p = q+1; |
| 576 | } |
| 577 | } |
| 578 | if (argv0_path) { |
| 579 | *buf++ = DELIM; |
| 580 | strcpy(buf, argv0_path); |
| 581 | buf = strchr(buf, '\0'); |
| 582 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 583 | *buf = '\0'; |
| 584 | } |
| 585 | |
| 586 | |
| 587 | /* External interface */ |
| 588 | |
| 589 | char * |
| 590 | Py_GetPath() |
| 591 | { |
| 592 | if (!module_search_path) |
| 593 | calculate_path(); |
| 594 | return module_search_path; |
| 595 | } |
| 596 | |
| 597 | char * |
| 598 | Py_GetPrefix() |
| 599 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 600 | if (!module_search_path) |
| 601 | calculate_path(); |
| 602 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | char * |
| 606 | Py_GetExecPrefix() |
| 607 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 608 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | char * |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 612 | Py_GetProgramFullPath() |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 613 | { |
| 614 | if (!module_search_path) |
| 615 | calculate_path(); |
| 616 | return progpath; |
| 617 | } |