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 | |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 14 | * If the PYTHONPATH env. var. exists, its entries are added next. |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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 |
Mark Hammond | 19fdbfb | 2001-09-07 14:08:01 +0000 | [diff] [blame] | 47 | the registry is used. Other "application paths" in the registry are |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 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 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 60 | #ifdef MS_WINDOWS |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 69 | /* Search in some common locations for the associated Python libraries. |
| 70 | * |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 71 | * Py_GetPath() tries to return a sensible Python module search path. |
| 72 | * |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 73 | * The approach is an adaptation for Windows of the strategy used in |
| 74 | * ../Modules/getpath.c; it uses the Windows Registry as one of its |
| 75 | * information sources. |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 76 | */ |
| 77 | |
| 78 | #ifndef LANDMARK |
Jeremy Hylton | 847a996 | 2000-05-26 21:49:07 +0000 | [diff] [blame] | 79 | #define LANDMARK "lib\\os.py" |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | static char prefix[MAXPATHLEN+1]; |
| 83 | static char progpath[MAXPATHLEN+1]; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 84 | static char dllpath[MAXPATHLEN+1]; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 85 | static char *module_search_path = NULL; |
| 86 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 88 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 89 | is_sep(char ch) /* determine if "ch" is a separator character */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 90 | { |
| 91 | #ifdef ALTSEP |
| 92 | return ch == SEP || ch == ALTSEP; |
| 93 | #else |
| 94 | return ch == SEP; |
| 95 | #endif |
| 96 | } |
| 97 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 98 | /* assumes 'dir' null terminated in bounds. Never writes |
| 99 | beyond existing terminator. |
| 100 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 101 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 102 | reduce(char *dir) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 103 | { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 104 | size_t i = strlen(dir); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 105 | while (i > 0 && !is_sep(dir[i])) |
| 106 | --i; |
| 107 | dir[i] = '\0'; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 112 | exists(char *filename) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 113 | { |
| 114 | struct stat buf; |
| 115 | return stat(filename, &buf) == 0; |
| 116 | } |
| 117 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 118 | /* Assumes 'filename' MAXPATHLEN+1 bytes long - |
| 119 | may extend 'filename' by one character. |
| 120 | */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 121 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 122 | ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */ |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 123 | { |
| 124 | if (exists(filename)) |
| 125 | return 1; |
| 126 | |
| 127 | /* Check for the compiled version of prefix. */ |
| 128 | if (strlen(filename) < MAXPATHLEN) { |
| 129 | strcat(filename, Py_OptimizeFlag ? "o" : "c"); |
| 130 | if (exists(filename)) |
| 131 | return 1; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
Tim Peters | 8484fbf | 2004-08-07 19:12:27 +0000 | [diff] [blame] | 136 | /* Add a path component, by appending stuff to buffer. |
| 137 | buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a |
| 138 | NUL-terminated string with no more than MAXPATHLEN characters (not counting |
| 139 | the trailing NUL). It's a fatal error if it contains a string longer than |
| 140 | that (callers must be careful!). If these requirements are met, it's |
| 141 | guaranteed that buffer will still be a NUL-terminated string with no more |
| 142 | than MAXPATHLEN characters at exit. If stuff is too long, only as much of |
| 143 | stuff as fits will be appended. |
| 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 | } |
Tim Peters | 8484fbf | 2004-08-07 19:12:27 +0000 | [diff] [blame] | 156 | if (n > MAXPATHLEN) |
| 157 | Py_FatalError("buffer overflow in getpathp.c's joinpath()"); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 158 | k = strlen(stuff); |
| 159 | if (n + k > MAXPATHLEN) |
| 160 | k = MAXPATHLEN - n; |
| 161 | strncpy(buffer+n, stuff, k); |
| 162 | buffer[n+k] = '\0'; |
| 163 | } |
| 164 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 165 | /* gotlandmark only called by search_for_prefix, which ensures |
| 166 | 'prefix' is null terminated in bounds. join() ensures |
| 167 | 'landmark' can not overflow prefix if too long. |
| 168 | */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 169 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 170 | gotlandmark(char *landmark) |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 171 | { |
| 172 | int n, ok; |
| 173 | |
| 174 | n = strlen(prefix); |
| 175 | join(prefix, landmark); |
| 176 | ok = ismodule(prefix); |
| 177 | prefix[n] = '\0'; |
| 178 | return ok; |
| 179 | } |
| 180 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 181 | /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd. |
| 182 | assumption provided by only caller, calculate_path() */ |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 183 | static int |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 184 | search_for_prefix(char *argv0_path, char *landmark) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 185 | { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 186 | /* Search from argv0_path, until landmark is found */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 187 | strcpy(prefix, argv0_path); |
| 188 | do { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 189 | if (gotlandmark(landmark)) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 190 | return 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 191 | reduce(prefix); |
| 192 | } while (prefix[0]); |
| 193 | return 0; |
| 194 | } |
| 195 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 196 | #ifdef MS_WINDOWS |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 198 | /* a string loaded from the DLL at startup.*/ |
| 199 | extern const char *PyWin_DLLVersionString; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 200 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 201 | |
| 202 | /* Load a PYTHONPATH value from the registry. |
| 203 | Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. |
| 204 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 205 | Works in both Unicode and 8bit environments. Only uses the |
| 206 | Ex family of functions so it also works with Windows CE. |
| 207 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 208 | Returns NULL, or a pointer that should be freed. |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 209 | |
| 210 | XXX - this code is pretty strange, as it used to also |
| 211 | work on Win16, where the buffer sizes werent available |
| 212 | in advance. It could be simplied now Win16/Win32s is dead! |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 213 | */ |
| 214 | |
| 215 | static char * |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 216 | getpythonregpath(HKEY keyBase, int skipcore) |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 217 | { |
| 218 | HKEY newKey = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 219 | DWORD dataSize = 0; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 220 | DWORD numKeys = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 221 | LONG rc; |
| 222 | char *retval = NULL; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 223 | TCHAR *dataBuf = NULL; |
| 224 | static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\"); |
| 225 | static const TCHAR keySuffix[] = _T("\\PythonPath"); |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 226 | size_t versionLen; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 227 | DWORD index; |
| 228 | TCHAR *keyBuf = NULL; |
| 229 | TCHAR *keyBufPtr; |
| 230 | TCHAR **ppPaths = NULL; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 232 | /* Tried to use sysget("winver") but here is too early :-( */ |
| 233 | versionLen = _tcslen(PyWin_DLLVersionString); |
| 234 | /* Space for all the chars, plus one \0 */ |
| 235 | keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) + |
| 236 | sizeof(TCHAR)*(versionLen-1) + |
| 237 | sizeof(keySuffix)); |
| 238 | if (keyBuf==NULL) goto done; |
Guido van Rossum | 271f977 | 1997-09-29 23:39:31 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 240 | memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR)); |
| 241 | keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1; |
| 242 | memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR)); |
| 243 | keyBufPtr += versionLen; |
| 244 | /* NULL comes with this one! */ |
| 245 | memcpy(keyBufPtr, keySuffix, sizeof(keySuffix)); |
| 246 | /* Open the root Python key */ |
| 247 | rc=RegOpenKeyEx(keyBase, |
| 248 | keyBuf, /* subkey */ |
| 249 | 0, /* reserved */ |
| 250 | KEY_READ, |
| 251 | &newKey); |
| 252 | if (rc!=ERROR_SUCCESS) goto done; |
| 253 | /* Find out how big our core buffer is, and how many subkeys we have */ |
| 254 | rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL, |
| 255 | NULL, NULL, &dataSize, NULL, NULL); |
| 256 | if (rc!=ERROR_SUCCESS) goto done; |
| 257 | if (skipcore) dataSize = 0; /* Only count core ones if we want them! */ |
| 258 | /* Allocate a temp array of char buffers, so we only need to loop |
| 259 | reading the registry once |
| 260 | */ |
| 261 | ppPaths = malloc( sizeof(TCHAR *) * numKeys ); |
| 262 | if (ppPaths==NULL) goto done; |
| 263 | memset(ppPaths, 0, sizeof(TCHAR *) * numKeys); |
| 264 | /* Loop over all subkeys, allocating a temp sub-buffer. */ |
| 265 | for(index=0;index<numKeys;index++) { |
| 266 | TCHAR keyBuf[MAX_PATH+1]; |
| 267 | HKEY subKey = 0; |
| 268 | DWORD reqdSize = MAX_PATH+1; |
| 269 | /* Get the sub-key name */ |
| 270 | DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize, |
| 271 | NULL, NULL, NULL, NULL ); |
| 272 | if (rc!=ERROR_SUCCESS) goto done; |
| 273 | /* Open the sub-key */ |
| 274 | rc=RegOpenKeyEx(newKey, |
| 275 | keyBuf, /* subkey */ |
| 276 | 0, /* reserved */ |
| 277 | KEY_READ, |
| 278 | &subKey); |
| 279 | if (rc!=ERROR_SUCCESS) goto done; |
| 280 | /* Find the value of the buffer size, malloc, then read it */ |
| 281 | RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize); |
| 282 | if (reqdSize) { |
| 283 | ppPaths[index] = malloc(reqdSize); |
| 284 | if (ppPaths[index]) { |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 285 | RegQueryValueEx(subKey, NULL, 0, NULL, |
| 286 | (LPBYTE)ppPaths[index], |
| 287 | &reqdSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 288 | dataSize += reqdSize + 1; /* 1 for the ";" */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 291 | RegCloseKey(subKey); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 292 | } |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 293 | /* original datasize from RegQueryInfo doesn't include the \0 */ |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 294 | dataBuf = malloc((dataSize+1) * sizeof(TCHAR)); |
| 295 | if (dataBuf) { |
| 296 | TCHAR *szCur = dataBuf; |
| 297 | DWORD reqdSize = dataSize; |
| 298 | /* Copy our collected strings */ |
| 299 | for (index=0;index<numKeys;index++) { |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 300 | if (index > 0) { |
| 301 | *(szCur++) = _T(';'); |
| 302 | dataSize--; |
| 303 | } |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 304 | if (ppPaths[index]) { |
| 305 | int len = _tcslen(ppPaths[index]); |
| 306 | _tcsncpy(szCur, ppPaths[index], len); |
| 307 | szCur += len; |
| 308 | dataSize -= len; |
| 309 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 310 | } |
| 311 | if (skipcore) |
| 312 | *szCur = '\0'; |
| 313 | else { |
Mark Hammond | 5edc627 | 2001-02-23 11:38:38 +0000 | [diff] [blame] | 314 | /* If we have no values, we dont need a ';' */ |
| 315 | if (numKeys) { |
| 316 | *(szCur++) = _T(';'); |
| 317 | dataSize--; |
| 318 | } |
Mark Hammond | e61aca7 | 2000-09-10 09:14:53 +0000 | [diff] [blame] | 319 | /* Now append the core path entries - |
| 320 | this will include the NULL |
| 321 | */ |
| 322 | rc = RegQueryValueEx(newKey, NULL, 0, NULL, |
| 323 | (LPBYTE)szCur, &dataSize); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 324 | } |
| 325 | /* And set the result - caller must free |
| 326 | If MBCS, it is fine as is. If Unicode, allocate new |
| 327 | buffer and convert. |
| 328 | */ |
| 329 | #ifdef UNICODE |
| 330 | retval = (char *)malloc(reqdSize+1); |
| 331 | if (retval) |
| 332 | WideCharToMultiByte(CP_ACP, 0, |
| 333 | dataBuf, -1, /* source */ |
Mark Hammond | 61bb35f | 2003-01-29 22:38:29 +0000 | [diff] [blame] | 334 | retval, reqdSize+1, /* dest */ |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 335 | NULL, NULL); |
| 336 | free(dataBuf); |
| 337 | #else |
| 338 | retval = dataBuf; |
| 339 | #endif |
| 340 | } |
| 341 | done: |
| 342 | /* Loop freeing my temp buffers */ |
| 343 | if (ppPaths) { |
| 344 | for(index=0;index<numKeys;index++) |
| 345 | if (ppPaths[index]) free(ppPaths[index]); |
| 346 | free(ppPaths); |
| 347 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 348 | if (newKey) |
| 349 | RegCloseKey(newKey); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 350 | if (keyBuf) |
| 351 | free(keyBuf); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 352 | return retval; |
| 353 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 354 | #endif /* MS_WINDOWS */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 355 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 356 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 357 | get_progpath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 358 | { |
Thomas Wouters | a534594 | 2000-07-22 23:59:33 +0000 | [diff] [blame] | 359 | extern char *Py_GetProgramName(void); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 360 | char *path = getenv("PATH"); |
| 361 | char *prog = Py_GetProgramName(); |
| 362 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 363 | #ifdef MS_WINDOWS |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 364 | extern HANDLE PyWin_DLLhModule; |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 365 | #ifdef UNICODE |
| 366 | WCHAR wprogpath[MAXPATHLEN+1]; |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 367 | /* Windows documents that GetModuleFileName() will "truncate", |
| 368 | but makes no mention of the null terminator. Play it safe. |
| 369 | PLUS Windows itself defines MAX_PATH as the same, but anyway... |
| 370 | */ |
Mark Hammond | 2795dae | 2002-07-22 13:28:21 +0000 | [diff] [blame] | 371 | wprogpath[MAXPATHLEN]=_T('\0'); |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 372 | if (PyWin_DLLhModule && |
| 373 | GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) { |
| 374 | WideCharToMultiByte(CP_ACP, 0, |
| 375 | wprogpath, -1, |
| 376 | dllpath, MAXPATHLEN+1, |
| 377 | NULL, NULL); |
| 378 | } |
Thomas Heller | 6019f9a | 2003-08-18 17:53:33 +0000 | [diff] [blame] | 379 | wprogpath[MAXPATHLEN]=_T('\0'); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 380 | if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) { |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 381 | WideCharToMultiByte(CP_ACP, 0, |
| 382 | wprogpath, -1, |
| 383 | progpath, MAXPATHLEN+1, |
| 384 | NULL, NULL); |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | #else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 388 | /* static init of progpath ensures final char remains \0 */ |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 389 | if (PyWin_DLLhModule) |
| 390 | if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN)) |
| 391 | dllpath[0] = 0; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 392 | if (GetModuleFileName(NULL, progpath, MAXPATHLEN)) |
| 393 | return; |
| 394 | #endif |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 395 | #endif |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 396 | if (prog == NULL || *prog == '\0') |
| 397 | prog = "python"; |
| 398 | |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 399 | /* If there is no slash in the argv0 path, then we have to |
| 400 | * assume python is on the user's $PATH, since there's no |
| 401 | * other way to find a directory to start the search from. If |
| 402 | * $PATH isn't exported, you lose. |
| 403 | */ |
| 404 | #ifdef ALTSEP |
| 405 | if (strchr(prog, SEP) || strchr(prog, ALTSEP)) |
| 406 | #else |
| 407 | if (strchr(prog, SEP)) |
| 408 | #endif |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 409 | strncpy(progpath, prog, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 410 | else if (path) { |
| 411 | while (1) { |
| 412 | char *delim = strchr(path, DELIM); |
| 413 | |
| 414 | if (delim) { |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 415 | size_t len = delim - path; |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 416 | /* ensure we can't overwrite buffer */ |
| 417 | len = min(MAXPATHLEN,len); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 418 | strncpy(progpath, path, len); |
| 419 | *(progpath + len) = '\0'; |
| 420 | } |
| 421 | else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 422 | strncpy(progpath, path, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 423 | |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 424 | /* join() is safe for MAXPATHLEN+1 size buffer */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 425 | join(progpath, prog); |
| 426 | if (exists(progpath)) |
| 427 | break; |
| 428 | |
| 429 | if (!delim) { |
| 430 | progpath[0] = '\0'; |
| 431 | break; |
| 432 | } |
| 433 | path = delim + 1; |
| 434 | } |
| 435 | } |
| 436 | else |
| 437 | progpath[0] = '\0'; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | static void |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 441 | calculate_path(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 442 | { |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 443 | char argv0_path[MAXPATHLEN+1]; |
| 444 | char *buf; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 445 | size_t bufsz; |
Guido van Rossum | 8b2b3ce | 1998-07-27 13:48:07 +0000 | [diff] [blame] | 446 | char *pythonhome = Py_GetPythonHome(); |
Neil Schemenauer | 7d4bb9f | 2001-07-23 16:30:27 +0000 | [diff] [blame] | 447 | char *envpath = Py_GETENV("PYTHONPATH"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 448 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 449 | #ifdef MS_WINDOWS |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 450 | int skiphome, skipdefault; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 451 | char *machinepath = NULL; |
| 452 | char *userpath = NULL; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 453 | char zip_path[MAXPATHLEN+1]; |
| 454 | size_t len; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 455 | #endif |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 456 | |
| 457 | get_progpath(); |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 458 | /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 459 | strcpy(argv0_path, progpath); |
| 460 | reduce(argv0_path); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 461 | if (pythonhome == NULL || *pythonhome == '\0') { |
| 462 | if (search_for_prefix(argv0_path, LANDMARK)) |
| 463 | pythonhome = prefix; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 464 | else |
| 465 | pythonhome = NULL; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 466 | } |
| 467 | else |
Mark Hammond | 8bf9e3b | 2000-10-07 11:10:50 +0000 | [diff] [blame] | 468 | strncpy(prefix, pythonhome, MAXPATHLEN); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 470 | if (envpath && *envpath == '\0') |
| 471 | envpath = NULL; |
| 472 | |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 473 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 474 | #ifdef MS_WINDOWS |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 475 | /* Calculate zip archive path */ |
| 476 | if (dllpath[0]) /* use name of python DLL */ |
| 477 | strncpy(zip_path, dllpath, MAXPATHLEN); |
| 478 | else /* use name of executable program */ |
| 479 | strncpy(zip_path, progpath, MAXPATHLEN); |
Neal Norwitz | a8aed02 | 2002-12-31 12:35:41 +0000 | [diff] [blame] | 480 | zip_path[MAXPATHLEN] = '\0'; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 481 | len = strlen(zip_path); |
| 482 | if (len > 4) { |
| 483 | zip_path[len-3] = 'z'; /* change ending to "zip" */ |
| 484 | zip_path[len-2] = 'i'; |
| 485 | zip_path[len-1] = 'p'; |
| 486 | } |
| 487 | else { |
| 488 | zip_path[0] = 0; |
| 489 | } |
| 490 | |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 491 | skiphome = pythonhome==NULL ? 0 : 1; |
| 492 | machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome); |
| 493 | userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome); |
| 494 | /* We only use the default relative PYTHONPATH if we havent |
| 495 | anything better to use! */ |
| 496 | skipdefault = envpath!=NULL || pythonhome!=NULL || \ |
| 497 | machinepath!=NULL || userpath!=NULL; |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 498 | #endif |
| 499 | |
| 500 | /* We need to construct a path from the following parts. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 501 | (1) the PYTHONPATH environment variable, if set; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 502 | (2) for Win32, the zip archive file path; |
| 503 | (3) for Win32, the machinepath and userpath, if set; |
| 504 | (4) the PYTHONPATH config macro, with the leading "." |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 505 | of each component replaced with pythonhome, if set; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 506 | (5) the directory containing the executable (argv0_path). |
| 507 | The length calculation calculates #4 first. |
Guido van Rossum | 43ff114 | 1998-08-08 23:40:40 +0000 | [diff] [blame] | 508 | Extra rules: |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 509 | - If PYTHONHOME is set (in any way) item (3) is ignored. |
| 510 | - If registry values are used, (4) and (5) are ignored. |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 511 | */ |
| 512 | |
| 513 | /* Calculate size of return buffer */ |
| 514 | if (pythonhome != NULL) { |
| 515 | char *p; |
| 516 | bufsz = 1; |
| 517 | for (p = PYTHONPATH; *p; p++) { |
| 518 | if (*p == DELIM) |
| 519 | bufsz++; /* number of DELIM plus one */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 520 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 521 | bufsz *= strlen(pythonhome); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 522 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 523 | else |
| 524 | bufsz = 0; |
Guido van Rossum | 691d2ad | 1997-12-11 02:32:43 +0000 | [diff] [blame] | 525 | bufsz += strlen(PYTHONPATH) + 1; |
Guido van Rossum | 8f1b651 | 1997-08-13 19:55:43 +0000 | [diff] [blame] | 526 | bufsz += strlen(argv0_path) + 1; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 527 | #ifdef MS_WINDOWS |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 528 | if (userpath) |
| 529 | bufsz += strlen(userpath) + 1; |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 530 | if (machinepath) |
| 531 | bufsz += strlen(machinepath) + 1; |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 532 | bufsz += strlen(zip_path) + 1; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 533 | #endif |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 534 | if (envpath != NULL) |
| 535 | bufsz += strlen(envpath) + 1; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 536 | |
| 537 | module_search_path = buf = malloc(bufsz); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 538 | if (buf == NULL) { |
| 539 | /* We can't exit, so print a warning and limp along */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 540 | fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n"); |
| 541 | if (envpath) { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 542 | fprintf(stderr, "Using environment $PYTHONPATH.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 543 | module_search_path = envpath; |
| 544 | } |
| 545 | else { |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 546 | fprintf(stderr, "Using default static path.\n"); |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 547 | module_search_path = PYTHONPATH; |
| 548 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 549 | #ifdef MS_WINDOWS |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 550 | if (machinepath) |
| 551 | free(machinepath); |
| 552 | if (userpath) |
| 553 | free(userpath); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 554 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 555 | return; |
| 556 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 557 | |
| 558 | if (envpath) { |
| 559 | strcpy(buf, envpath); |
| 560 | buf = strchr(buf, '\0'); |
| 561 | *buf++ = DELIM; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 562 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 563 | #ifdef MS_WINDOWS |
Just van Rossum | 52e14d6 | 2002-12-30 22:08:05 +0000 | [diff] [blame] | 564 | if (zip_path[0]) { |
| 565 | strcpy(buf, zip_path); |
| 566 | buf = strchr(buf, '\0'); |
| 567 | *buf++ = DELIM; |
| 568 | } |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 569 | if (userpath) { |
| 570 | strcpy(buf, userpath); |
| 571 | buf = strchr(buf, '\0'); |
| 572 | *buf++ = DELIM; |
| 573 | free(userpath); |
| 574 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 575 | if (machinepath) { |
| 576 | strcpy(buf, machinepath); |
| 577 | buf = strchr(buf, '\0'); |
| 578 | *buf++ = DELIM; |
Guido van Rossum | 42a9744 | 1998-02-19 21:00:45 +0000 | [diff] [blame] | 579 | free(machinepath); |
Guido van Rossum | e02e48b | 2000-03-29 01:49:47 +0000 | [diff] [blame] | 580 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 581 | if (pythonhome == NULL) { |
| 582 | if (!skipdefault) { |
| 583 | strcpy(buf, PYTHONPATH); |
| 584 | buf = strchr(buf, '\0'); |
| 585 | } |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 586 | } |
Guido van Rossum | 88716bb | 2000-03-30 19:45:39 +0000 | [diff] [blame] | 587 | #else |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 588 | if (pythonhome == NULL) { |
| 589 | strcpy(buf, PYTHONPATH); |
| 590 | buf = strchr(buf, '\0'); |
| 591 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 592 | #endif /* MS_WINDOWS */ |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 593 | else { |
| 594 | char *p = PYTHONPATH; |
| 595 | char *q; |
Guido van Rossum | 1c44e28 | 2000-06-28 22:20:06 +0000 | [diff] [blame] | 596 | size_t n; |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 597 | for (;;) { |
| 598 | q = strchr(p, DELIM); |
| 599 | if (q == NULL) |
| 600 | n = strlen(p); |
| 601 | else |
| 602 | n = q-p; |
| 603 | if (p[0] == '.' && is_sep(p[1])) { |
| 604 | strcpy(buf, pythonhome); |
| 605 | buf = strchr(buf, '\0'); |
| 606 | p++; |
| 607 | n--; |
| 608 | } |
| 609 | strncpy(buf, p, n); |
| 610 | buf += n; |
| 611 | if (q == NULL) |
| 612 | break; |
| 613 | *buf++ = DELIM; |
| 614 | p = q+1; |
| 615 | } |
| 616 | } |
| 617 | if (argv0_path) { |
| 618 | *buf++ = DELIM; |
| 619 | strcpy(buf, argv0_path); |
| 620 | buf = strchr(buf, '\0'); |
| 621 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 622 | *buf = '\0'; |
Mark Hammond | 19fdbfb | 2001-09-07 14:08:01 +0000 | [diff] [blame] | 623 | /* Now to pull one last hack/trick. If sys.prefix is |
| 624 | empty, then try and find it somewhere on the paths |
| 625 | we calculated. We scan backwards, as our general policy |
| 626 | is that Python core directories are at the *end* of |
| 627 | sys.path. We assume that our "lib" directory is |
| 628 | on the path, and that our 'prefix' directory is |
| 629 | the parent of that. |
| 630 | */ |
| 631 | if (*prefix=='\0') { |
| 632 | char lookBuf[MAXPATHLEN+1]; |
| 633 | char *look = buf - 1; /* 'buf' is at the end of the buffer */ |
| 634 | while (1) { |
| 635 | int nchars; |
| 636 | char *lookEnd = look; |
| 637 | /* 'look' will end up one character before the |
| 638 | start of the path in question - even if this |
| 639 | is one character before the start of the buffer |
| 640 | */ |
| 641 | while (*look != DELIM && look >= module_search_path) |
| 642 | look--; |
| 643 | nchars = lookEnd-look; |
| 644 | strncpy(lookBuf, look+1, nchars); |
| 645 | lookBuf[nchars] = '\0'; |
| 646 | /* Up one level to the parent */ |
| 647 | reduce(lookBuf); |
| 648 | if (search_for_prefix(lookBuf, LANDMARK)) { |
| 649 | break; |
| 650 | } |
| 651 | /* If we are out of paths to search - give up */ |
| 652 | if (look < module_search_path) |
| 653 | break; |
| 654 | look--; |
| 655 | } |
| 656 | } |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | /* External interface */ |
| 661 | |
| 662 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 663 | Py_GetPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 664 | { |
| 665 | if (!module_search_path) |
| 666 | calculate_path(); |
| 667 | return module_search_path; |
| 668 | } |
| 669 | |
| 670 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 671 | Py_GetPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 672 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 673 | if (!module_search_path) |
| 674 | calculate_path(); |
| 675 | return prefix; |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 679 | Py_GetExecPrefix(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 680 | { |
Guido van Rossum | eea1449 | 1997-08-13 21:30:44 +0000 | [diff] [blame] | 681 | return Py_GetPrefix(); |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | char * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 685 | Py_GetProgramFullPath(void) |
Guido van Rossum | 1aa7e3a | 1997-05-19 14:16:21 +0000 | [diff] [blame] | 686 | { |
| 687 | if (!module_search_path) |
| 688 | calculate_path(); |
| 689 | return progpath; |
| 690 | } |