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