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