blob: 2123b2a76c6febe3c763c1de5048d5c137c970d2 [file] [log] [blame]
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
16
17While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
29
30******************************************************************/
31
32/* Return the initial module search path. */
Guido van Rossumc4995721998-08-08 20:05:31 +000033/* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000034
Guido van Rossum88716bb2000-03-30 19:45:39 +000035/* ----------------------------------------------------------------
36 PATH RULES FOR WINDOWS:
37 This describes how sys.path is formed on Windows. It describes the
38 functionality, not the implementation (ie, the order in which these
39 are actually fetched is different)
40
41 * Python always adds an empty entry at the start, which corresponds
42 to the current directory.
43
44 * If the PYTHONPATH env. var. exists, it's entries are added next.
45
46 * We look in the registry for "application paths" - that is, sub-keys
47 under the main PythonPath registry key. These are added next (the
48 order of sub-key processing is undefined).
49 HKEY_CURRENT_USER is searched and added first.
50 HKEY_LOCAL_MACHINE is searched and added next.
51 (Note that all known installers only use HKLM, so HKCU is typically
52 empty)
53
54 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
55 is set, we believe it. Otherwise, we use the path of our host .EXE's
56 to try and locate our "landmark" (lib\\string.py) and deduce our home.
57 - If we DO have a Python Home: The relevant sub-directories (Lib,
58 plat-win, lib-tk, etc) are based on the Python Home
59 - If we DO NOT have a Python Home, the core Python Path is
60 loaded from the registry. This is the main PythonPath key,
61 and both HKLM and HKCU are combined to form the path)
62
63 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
64 specified, and can't locate any Registry entries (ie, we have _nothing_
65 we can assume is a good path), a default path with relative entries is
66 used (eg. .\Lib;.\plat-win, etc)
67
68
69 The end result of all this is:
70 * When running python.exe, or any other .exe in the main Python directory
71 (either an installed version, or directly from the PCbuild directory),
72 the core path is deduced, and the core paths in the registry are
73 ignored. Other "application paths" in the registry are always read.
74
75 * When Python is hosted in another exe (different directory, embedded via
76 COM, etc), the Python Home will not be deduced, so the core path from
77 the registry is used. Other "application paths "in the registry are
78 always read.
79
80 * If Python can't find its home and there is no registry (eg, frozen
81 exe, some very strange installation setup) you get a path with
82 some default, but relative, paths.
83
84 ---------------------------------------------------------------- */
85
86
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000087#include "Python.h"
88#include "osdefs.h"
89
Guido van Rossum8f1b6511997-08-13 19:55:43 +000090#ifdef MS_WIN32
91#include <windows.h>
Guido van Rossum88716bb2000-03-30 19:45:39 +000092#include <tchar.h>
Guido van Rossum8f1b6511997-08-13 19:55:43 +000093#endif
94
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000095#include <sys/types.h>
96#include <sys/stat.h>
97#include <string.h>
98
99#if HAVE_UNISTD_H
100#include <unistd.h>
101#endif /* HAVE_UNISTD_H */
102
103/* Search in some common locations for the associated Python libraries.
104 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000105 * Py_GetPath() tries to return a sensible Python module search path.
106 *
Guido van Rossum42a97441998-02-19 21:00:45 +0000107 * The approach is an adaptation for Windows of the strategy used in
108 * ../Modules/getpath.c; it uses the Windows Registry as one of its
109 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000110 */
111
112#ifndef LANDMARK
Guido van Rossumeea14491997-08-13 21:30:44 +0000113#define LANDMARK "lib\\string.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000114#endif
115
116static char prefix[MAXPATHLEN+1];
117static char progpath[MAXPATHLEN+1];
118static char *module_search_path = NULL;
119
Guido van Rossumeea14491997-08-13 21:30:44 +0000120
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000121static int
122is_sep(ch) /* determine if "ch" is a separator character */
123 char ch;
124{
125#ifdef ALTSEP
126 return ch == SEP || ch == ALTSEP;
127#else
128 return ch == SEP;
129#endif
130}
131
Guido van Rossumeea14491997-08-13 21:30:44 +0000132
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000133static void
134reduce(dir)
135 char *dir;
136{
137 int i = strlen(dir);
138 while (i > 0 && !is_sep(dir[i]))
139 --i;
140 dir[i] = '\0';
141}
142
143
144static int
145exists(filename)
146 char *filename;
147{
148 struct stat buf;
149 return stat(filename, &buf) == 0;
150}
151
152
Guido van Rossum43ff1141998-08-08 23:40:40 +0000153static int
154ismodule(filename) /* Is module -- check for .pyc/.pyo too */
155 char *filename;
156{
157 if (exists(filename))
158 return 1;
159
160 /* Check for the compiled version of prefix. */
161 if (strlen(filename) < MAXPATHLEN) {
162 strcat(filename, Py_OptimizeFlag ? "o" : "c");
163 if (exists(filename))
164 return 1;
165 }
166 return 0;
167}
168
169
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000170static void
171join(buffer, stuff)
172 char *buffer;
173 char *stuff;
174{
175 int n, k;
176 if (is_sep(stuff[0]))
177 n = 0;
178 else {
179 n = strlen(buffer);
180 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
181 buffer[n++] = SEP;
182 }
183 k = strlen(stuff);
184 if (n + k > MAXPATHLEN)
185 k = MAXPATHLEN - n;
186 strncpy(buffer+n, stuff, k);
187 buffer[n+k] = '\0';
188}
189
190
191static int
Guido van Rossume02e48b2000-03-29 01:49:47 +0000192gotlandmark(landmark)
193 char *landmark;
194{
195 int n, ok;
196
197 n = strlen(prefix);
198 join(prefix, landmark);
199 ok = ismodule(prefix);
200 prefix[n] = '\0';
201 return ok;
202}
203
204
205static int
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000206search_for_prefix(argv0_path, landmark)
207 char *argv0_path;
208 char *landmark;
209{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000210
Guido van Rossume02e48b2000-03-29 01:49:47 +0000211 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000212 strcpy(prefix, argv0_path);
213 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000214 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000215 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000216 reduce(prefix);
217 } while (prefix[0]);
218 return 0;
219}
220
Guido van Rossumeea14491997-08-13 21:30:44 +0000221#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000222
Guido van Rossum88716bb2000-03-30 19:45:39 +0000223/* a string loaded from the DLL at startup.*/
224extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000225
Guido van Rossumeea14491997-08-13 21:30:44 +0000226
227/* Load a PYTHONPATH value from the registry.
228 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
229
Guido van Rossum88716bb2000-03-30 19:45:39 +0000230 Works in both Unicode and 8bit environments. Only uses the
231 Ex family of functions so it also works with Windows CE.
232
Guido van Rossumeea14491997-08-13 21:30:44 +0000233 Returns NULL, or a pointer that should be freed.
234*/
235
236static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000237getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000238{
239 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000240 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000241 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000242 LONG rc;
243 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000244 TCHAR *dataBuf = NULL;
245 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
246 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum271f9771997-09-29 23:39:31 +0000247 int versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000248 DWORD index;
249 TCHAR *keyBuf = NULL;
250 TCHAR *keyBufPtr;
251 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000252
Guido van Rossum88716bb2000-03-30 19:45:39 +0000253 /* Tried to use sysget("winver") but here is too early :-( */
254 versionLen = _tcslen(PyWin_DLLVersionString);
255 /* Space for all the chars, plus one \0 */
256 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
257 sizeof(TCHAR)*(versionLen-1) +
258 sizeof(keySuffix));
259 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000260
Guido van Rossum88716bb2000-03-30 19:45:39 +0000261 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
262 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
263 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
264 keyBufPtr += versionLen;
265 /* NULL comes with this one! */
266 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
267 /* Open the root Python key */
268 rc=RegOpenKeyEx(keyBase,
269 keyBuf, /* subkey */
270 0, /* reserved */
271 KEY_READ,
272 &newKey);
273 if (rc!=ERROR_SUCCESS) goto done;
274 /* Find out how big our core buffer is, and how many subkeys we have */
275 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
276 NULL, NULL, &dataSize, NULL, NULL);
277 if (rc!=ERROR_SUCCESS) goto done;
278 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
279 /* Allocate a temp array of char buffers, so we only need to loop
280 reading the registry once
281 */
282 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
283 if (ppPaths==NULL) goto done;
284 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
285 /* Loop over all subkeys, allocating a temp sub-buffer. */
286 for(index=0;index<numKeys;index++) {
287 TCHAR keyBuf[MAX_PATH+1];
288 HKEY subKey = 0;
289 DWORD reqdSize = MAX_PATH+1;
290 /* Get the sub-key name */
291 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
292 NULL, NULL, NULL, NULL );
293 if (rc!=ERROR_SUCCESS) goto done;
294 /* Open the sub-key */
295 rc=RegOpenKeyEx(newKey,
296 keyBuf, /* subkey */
297 0, /* reserved */
298 KEY_READ,
299 &subKey);
300 if (rc!=ERROR_SUCCESS) goto done;
301 /* Find the value of the buffer size, malloc, then read it */
302 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
303 if (reqdSize) {
304 ppPaths[index] = malloc(reqdSize);
305 if (ppPaths[index]) {
306 RegQueryValueEx(subKey, NULL, 0, NULL, (LPBYTE)ppPaths[index], &reqdSize);
307 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000308 }
309 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000310 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000311 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000312 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
313 if (dataBuf) {
314 TCHAR *szCur = dataBuf;
315 DWORD reqdSize = dataSize;
316 /* Copy our collected strings */
317 for (index=0;index<numKeys;index++) {
318 int len;
319 if (index > 0) {
320 *(szCur++) = _T(';');
321 dataSize--;
322 }
323 len = _tcslen(ppPaths[index]);
324 _tcsncpy(szCur, ppPaths[index], len);
325 szCur += len;
326 dataSize -= len;
327 }
328 if (skipcore)
329 *szCur = '\0';
330 else {
331 *(szCur++) = _T(';');
332 dataSize--;
333 /* Now append the core path entries - this will include the NULL */
334 rc = RegQueryValueEx(newKey, NULL, 0, NULL, (LPBYTE)szCur, &dataSize);
335 }
336 /* And set the result - caller must free
337 If MBCS, it is fine as is. If Unicode, allocate new
338 buffer and convert.
339 */
340#ifdef UNICODE
341 retval = (char *)malloc(reqdSize+1);
342 if (retval)
343 WideCharToMultiByte(CP_ACP, 0,
344 dataBuf, -1, /* source */
345 retval, dataSize+1, /* dest */
346 NULL, NULL);
347 free(dataBuf);
348#else
349 retval = dataBuf;
350#endif
351 }
352done:
353 /* Loop freeing my temp buffers */
354 if (ppPaths) {
355 for(index=0;index<numKeys;index++)
356 if (ppPaths[index]) free(ppPaths[index]);
357 free(ppPaths);
358 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000359 if (newKey)
360 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000361 if (keyBuf)
362 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000363 return retval;
364}
365#endif /* MS_WIN32 */
366
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000367static void
368get_progpath()
369{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000370 extern char *Py_GetProgramName();
371 char *path = getenv("PATH");
372 char *prog = Py_GetProgramName();
373
Guido van Rossumeea14491997-08-13 21:30:44 +0000374#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000375#ifdef UNICODE
376 WCHAR wprogpath[MAXPATHLEN+1];
377 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
378 WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL);
379 return;
380 }
381#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000382 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
383 return;
384#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000385#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000386 if (prog == NULL || *prog == '\0')
387 prog = "python";
388
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000389 /* If there is no slash in the argv0 path, then we have to
390 * assume python is on the user's $PATH, since there's no
391 * other way to find a directory to start the search from. If
392 * $PATH isn't exported, you lose.
393 */
394#ifdef ALTSEP
395 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
396#else
397 if (strchr(prog, SEP))
398#endif
399 strcpy(progpath, prog);
400 else if (path) {
401 while (1) {
402 char *delim = strchr(path, DELIM);
403
404 if (delim) {
405 int len = delim - path;
406 strncpy(progpath, path, len);
407 *(progpath + len) = '\0';
408 }
409 else
410 strcpy(progpath, path);
411
412 join(progpath, prog);
413 if (exists(progpath))
414 break;
415
416 if (!delim) {
417 progpath[0] = '\0';
418 break;
419 }
420 path = delim + 1;
421 }
422 }
423 else
424 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000425}
426
427static void
428calculate_path()
429{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000430 char argv0_path[MAXPATHLEN+1];
431 char *buf;
432 int bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000433 char *pythonhome = Py_GetPythonHome();
Guido van Rossumeea14491997-08-13 21:30:44 +0000434 char *envpath = getenv("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000435
Guido van Rossum43ff1141998-08-08 23:40:40 +0000436#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000437 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000438 char *machinepath = NULL;
439 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000440#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000441
442 get_progpath();
443 strcpy(argv0_path, progpath);
444 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000445 if (pythonhome == NULL || *pythonhome == '\0') {
446 if (search_for_prefix(argv0_path, LANDMARK))
447 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000448 else
449 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000450 }
451 else
452 strcpy(prefix, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000453
Guido van Rossumeea14491997-08-13 21:30:44 +0000454 if (envpath && *envpath == '\0')
455 envpath = NULL;
456
Guido van Rossume02e48b2000-03-29 01:49:47 +0000457
Guido van Rossum43ff1141998-08-08 23:40:40 +0000458#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000459 skiphome = pythonhome==NULL ? 0 : 1;
460 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
461 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
462 /* We only use the default relative PYTHONPATH if we havent
463 anything better to use! */
464 skipdefault = envpath!=NULL || pythonhome!=NULL || \
465 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000466#endif
467
468 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000469 (1) the PYTHONPATH environment variable, if set;
470 (2) for Win32, the machinepath and userpath, if set;
471 (3) the PYTHONPATH config macro, with the leading "."
472 of each component replaced with pythonhome, if set;
473 (4) the directory containing the executable (argv0_path).
474 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000475 Extra rules:
476 - If PYTHONHOME is set (in any way) item (2) is ignored.
477 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000478 */
479
480 /* Calculate size of return buffer */
481 if (pythonhome != NULL) {
482 char *p;
483 bufsz = 1;
484 for (p = PYTHONPATH; *p; p++) {
485 if (*p == DELIM)
486 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000487 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000488 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000489 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000490 else
491 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000492 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000493 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000494#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000495 if (userpath)
496 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000497 if (machinepath)
498 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000499#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000500 if (envpath != NULL)
501 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000502
503 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000504 if (buf == NULL) {
505 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000506 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
507 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000508 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000509 module_search_path = envpath;
510 }
511 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000512 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000513 module_search_path = PYTHONPATH;
514 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000515#ifdef MS_WIN32
516 if (machinepath)
517 free(machinepath);
518 if (userpath)
519 free(userpath);
520#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000521 return;
522 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000523
524 if (envpath) {
525 strcpy(buf, envpath);
526 buf = strchr(buf, '\0');
527 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000528 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000529#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000530 if (userpath) {
531 strcpy(buf, userpath);
532 buf = strchr(buf, '\0');
533 *buf++ = DELIM;
534 free(userpath);
535 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000536 if (machinepath) {
537 strcpy(buf, machinepath);
538 buf = strchr(buf, '\0');
539 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000540 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000541 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000542 if (pythonhome == NULL) {
543 if (!skipdefault) {
544 strcpy(buf, PYTHONPATH);
545 buf = strchr(buf, '\0');
546 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000547 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000548#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000549 if (pythonhome == NULL) {
550 strcpy(buf, PYTHONPATH);
551 buf = strchr(buf, '\0');
552 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000553#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000554 else {
555 char *p = PYTHONPATH;
556 char *q;
557 int n;
558 for (;;) {
559 q = strchr(p, DELIM);
560 if (q == NULL)
561 n = strlen(p);
562 else
563 n = q-p;
564 if (p[0] == '.' && is_sep(p[1])) {
565 strcpy(buf, pythonhome);
566 buf = strchr(buf, '\0');
567 p++;
568 n--;
569 }
570 strncpy(buf, p, n);
571 buf += n;
572 if (q == NULL)
573 break;
574 *buf++ = DELIM;
575 p = q+1;
576 }
577 }
578 if (argv0_path) {
579 *buf++ = DELIM;
580 strcpy(buf, argv0_path);
581 buf = strchr(buf, '\0');
582 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000583 *buf = '\0';
584}
585
586
587/* External interface */
588
589char *
590Py_GetPath()
591{
592 if (!module_search_path)
593 calculate_path();
594 return module_search_path;
595}
596
597char *
598Py_GetPrefix()
599{
Guido van Rossumeea14491997-08-13 21:30:44 +0000600 if (!module_search_path)
601 calculate_path();
602 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000603}
604
605char *
606Py_GetExecPrefix()
607{
Guido van Rossumeea14491997-08-13 21:30:44 +0000608 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000609}
610
611char *
Guido van Rossumeea14491997-08-13 21:30:44 +0000612Py_GetProgramFullPath()
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000613{
614 if (!module_search_path)
615 calculate_path();
616 return progpath;
617}