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