blob: 5bfcadc3b1a3c7de93fe92dca6b2970da095f00d [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
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007Copyright (c) 2000, BeOpen.com.
8Copyright (c) 1995-2000, Corporation for National Research Initiatives.
9Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
10All rights reserved.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000011
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000012See the file "Misc/COPYRIGHT" for information on usage and
13redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000014
15******************************************************************/
16
17/* Return the initial module search path. */
Guido van Rossumc4995721998-08-08 20:05:31 +000018/* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000019
Guido van Rossum88716bb2000-03-30 19:45:39 +000020/* ----------------------------------------------------------------
21 PATH RULES FOR WINDOWS:
22 This describes how sys.path is formed on Windows. It describes the
23 functionality, not the implementation (ie, the order in which these
24 are actually fetched is different)
25
26 * Python always adds an empty entry at the start, which corresponds
27 to the current directory.
28
29 * If the PYTHONPATH env. var. exists, it's entries are added next.
30
31 * We look in the registry for "application paths" - that is, sub-keys
32 under the main PythonPath registry key. These are added next (the
33 order of sub-key processing is undefined).
34 HKEY_CURRENT_USER is searched and added first.
35 HKEY_LOCAL_MACHINE is searched and added next.
36 (Note that all known installers only use HKLM, so HKCU is typically
37 empty)
38
39 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
40 is set, we believe it. Otherwise, we use the path of our host .EXE's
Jeremy Hylton847a9962000-05-26 21:49:07 +000041 to try and locate our "landmark" (lib\\os.py) and deduce our home.
Guido van Rossum88716bb2000-03-30 19:45:39 +000042 - If we DO have a Python Home: The relevant sub-directories (Lib,
43 plat-win, lib-tk, etc) are based on the Python Home
44 - If we DO NOT have a Python Home, the core Python Path is
45 loaded from the registry. This is the main PythonPath key,
46 and both HKLM and HKCU are combined to form the path)
47
48 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
49 specified, and can't locate any Registry entries (ie, we have _nothing_
50 we can assume is a good path), a default path with relative entries is
51 used (eg. .\Lib;.\plat-win, etc)
52
53
54 The end result of all this is:
55 * When running python.exe, or any other .exe in the main Python directory
56 (either an installed version, or directly from the PCbuild directory),
57 the core path is deduced, and the core paths in the registry are
58 ignored. Other "application paths" in the registry are always read.
59
60 * When Python is hosted in another exe (different directory, embedded via
61 COM, etc), the Python Home will not be deduced, so the core path from
62 the registry is used. Other "application paths "in the registry are
63 always read.
64
65 * If Python can't find its home and there is no registry (eg, frozen
66 exe, some very strange installation setup) you get a path with
67 some default, but relative, paths.
68
69 ---------------------------------------------------------------- */
70
71
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000072#include "Python.h"
73#include "osdefs.h"
74
Guido van Rossum8f1b6511997-08-13 19:55:43 +000075#ifdef MS_WIN32
76#include <windows.h>
Guido van Rossum88716bb2000-03-30 19:45:39 +000077#include <tchar.h>
Guido van Rossum8f1b6511997-08-13 19:55:43 +000078#endif
79
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000080#include <sys/types.h>
81#include <sys/stat.h>
82#include <string.h>
83
84#if HAVE_UNISTD_H
85#include <unistd.h>
86#endif /* HAVE_UNISTD_H */
87
88/* Search in some common locations for the associated Python libraries.
89 *
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000090 * Py_GetPath() tries to return a sensible Python module search path.
91 *
Guido van Rossum42a97441998-02-19 21:00:45 +000092 * The approach is an adaptation for Windows of the strategy used in
93 * ../Modules/getpath.c; it uses the Windows Registry as one of its
94 * information sources.
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000095 */
96
97#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +000098#define LANDMARK "lib\\os.py"
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +000099#endif
100
101static char prefix[MAXPATHLEN+1];
102static char progpath[MAXPATHLEN+1];
103static char *module_search_path = NULL;
104
Guido van Rossumeea14491997-08-13 21:30:44 +0000105
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000106static int
107is_sep(ch) /* determine if "ch" is a separator character */
108 char ch;
109{
110#ifdef ALTSEP
111 return ch == SEP || ch == ALTSEP;
112#else
113 return ch == SEP;
114#endif
115}
116
Guido van Rossumeea14491997-08-13 21:30:44 +0000117
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000118static void
119reduce(dir)
120 char *dir;
121{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000122 size_t i = strlen(dir);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000123 while (i > 0 && !is_sep(dir[i]))
124 --i;
125 dir[i] = '\0';
126}
127
128
129static int
130exists(filename)
131 char *filename;
132{
133 struct stat buf;
134 return stat(filename, &buf) == 0;
135}
136
137
Guido van Rossum43ff1141998-08-08 23:40:40 +0000138static int
139ismodule(filename) /* Is module -- check for .pyc/.pyo too */
140 char *filename;
141{
142 if (exists(filename))
143 return 1;
144
145 /* Check for the compiled version of prefix. */
146 if (strlen(filename) < MAXPATHLEN) {
147 strcat(filename, Py_OptimizeFlag ? "o" : "c");
148 if (exists(filename))
149 return 1;
150 }
151 return 0;
152}
153
154
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000155static void
156join(buffer, stuff)
157 char *buffer;
158 char *stuff;
159{
Guido van Rossum1c44e282000-06-28 22:20:06 +0000160 size_t n, k;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000161 if (is_sep(stuff[0]))
162 n = 0;
163 else {
164 n = strlen(buffer);
165 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
166 buffer[n++] = SEP;
167 }
168 k = strlen(stuff);
169 if (n + k > MAXPATHLEN)
170 k = MAXPATHLEN - n;
171 strncpy(buffer+n, stuff, k);
172 buffer[n+k] = '\0';
173}
174
175
176static int
Guido van Rossume02e48b2000-03-29 01:49:47 +0000177gotlandmark(landmark)
178 char *landmark;
179{
180 int n, ok;
181
182 n = strlen(prefix);
183 join(prefix, landmark);
184 ok = ismodule(prefix);
185 prefix[n] = '\0';
186 return ok;
187}
188
189
190static int
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000191search_for_prefix(argv0_path, landmark)
192 char *argv0_path;
193 char *landmark;
194{
Guido van Rossume02e48b2000-03-29 01:49:47 +0000195 /* Search from argv0_path, until landmark is found */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000196 strcpy(prefix, argv0_path);
197 do {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000198 if (gotlandmark(landmark))
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000199 return 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000200 reduce(prefix);
201 } while (prefix[0]);
202 return 0;
203}
204
Guido van Rossumeea14491997-08-13 21:30:44 +0000205#ifdef MS_WIN32
Guido van Rossum43ff1141998-08-08 23:40:40 +0000206
Guido van Rossum88716bb2000-03-30 19:45:39 +0000207/* a string loaded from the DLL at startup.*/
208extern const char *PyWin_DLLVersionString;
Guido van Rossum271f9771997-09-29 23:39:31 +0000209
Guido van Rossumeea14491997-08-13 21:30:44 +0000210
211/* Load a PYTHONPATH value from the registry.
212 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
213
Guido van Rossum88716bb2000-03-30 19:45:39 +0000214 Works in both Unicode and 8bit environments. Only uses the
215 Ex family of functions so it also works with Windows CE.
216
Guido van Rossumeea14491997-08-13 21:30:44 +0000217 Returns NULL, or a pointer that should be freed.
218*/
219
220static char *
Guido van Rossum88716bb2000-03-30 19:45:39 +0000221getpythonregpath(HKEY keyBase, int skipcore)
Guido van Rossumeea14491997-08-13 21:30:44 +0000222{
223 HKEY newKey = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000224 DWORD dataSize = 0;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000225 DWORD numKeys = 0;
Guido van Rossumeea14491997-08-13 21:30:44 +0000226 LONG rc;
227 char *retval = NULL;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000228 TCHAR *dataBuf = NULL;
229 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
230 static const TCHAR keySuffix[] = _T("\\PythonPath");
Guido van Rossum1c44e282000-06-28 22:20:06 +0000231 size_t versionLen;
Guido van Rossum88716bb2000-03-30 19:45:39 +0000232 DWORD index;
233 TCHAR *keyBuf = NULL;
234 TCHAR *keyBufPtr;
235 TCHAR **ppPaths = NULL;
Guido van Rossum271f9771997-09-29 23:39:31 +0000236
Guido van Rossum88716bb2000-03-30 19:45:39 +0000237 /* Tried to use sysget("winver") but here is too early :-( */
238 versionLen = _tcslen(PyWin_DLLVersionString);
239 /* Space for all the chars, plus one \0 */
240 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
241 sizeof(TCHAR)*(versionLen-1) +
242 sizeof(keySuffix));
243 if (keyBuf==NULL) goto done;
Guido van Rossum271f9771997-09-29 23:39:31 +0000244
Guido van Rossum88716bb2000-03-30 19:45:39 +0000245 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
246 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
247 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
248 keyBufPtr += versionLen;
249 /* NULL comes with this one! */
250 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
251 /* Open the root Python key */
252 rc=RegOpenKeyEx(keyBase,
253 keyBuf, /* subkey */
254 0, /* reserved */
255 KEY_READ,
256 &newKey);
257 if (rc!=ERROR_SUCCESS) goto done;
258 /* Find out how big our core buffer is, and how many subkeys we have */
259 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
260 NULL, NULL, &dataSize, NULL, NULL);
261 if (rc!=ERROR_SUCCESS) goto done;
262 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
263 /* Allocate a temp array of char buffers, so we only need to loop
264 reading the registry once
265 */
266 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
267 if (ppPaths==NULL) goto done;
268 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
269 /* Loop over all subkeys, allocating a temp sub-buffer. */
270 for(index=0;index<numKeys;index++) {
271 TCHAR keyBuf[MAX_PATH+1];
272 HKEY subKey = 0;
273 DWORD reqdSize = MAX_PATH+1;
274 /* Get the sub-key name */
275 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
276 NULL, NULL, NULL, NULL );
277 if (rc!=ERROR_SUCCESS) goto done;
278 /* Open the sub-key */
279 rc=RegOpenKeyEx(newKey,
280 keyBuf, /* subkey */
281 0, /* reserved */
282 KEY_READ,
283 &subKey);
284 if (rc!=ERROR_SUCCESS) goto done;
285 /* Find the value of the buffer size, malloc, then read it */
286 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
287 if (reqdSize) {
288 ppPaths[index] = malloc(reqdSize);
289 if (ppPaths[index]) {
290 RegQueryValueEx(subKey, NULL, 0, NULL, (LPBYTE)ppPaths[index], &reqdSize);
291 dataSize += reqdSize + 1; /* 1 for the ";" */
Guido van Rossumeea14491997-08-13 21:30:44 +0000292 }
293 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000294 RegCloseKey(subKey);
Guido van Rossumeea14491997-08-13 21:30:44 +0000295 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000296 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
297 if (dataBuf) {
298 TCHAR *szCur = dataBuf;
299 DWORD reqdSize = dataSize;
300 /* Copy our collected strings */
301 for (index=0;index<numKeys;index++) {
302 int len;
303 if (index > 0) {
304 *(szCur++) = _T(';');
305 dataSize--;
306 }
307 len = _tcslen(ppPaths[index]);
308 _tcsncpy(szCur, ppPaths[index], len);
309 szCur += len;
310 dataSize -= len;
311 }
312 if (skipcore)
313 *szCur = '\0';
314 else {
315 *(szCur++) = _T(';');
316 dataSize--;
317 /* Now append the core path entries - this will include the NULL */
318 rc = RegQueryValueEx(newKey, NULL, 0, NULL, (LPBYTE)szCur, &dataSize);
319 }
320 /* And set the result - caller must free
321 If MBCS, it is fine as is. If Unicode, allocate new
322 buffer and convert.
323 */
324#ifdef UNICODE
325 retval = (char *)malloc(reqdSize+1);
326 if (retval)
327 WideCharToMultiByte(CP_ACP, 0,
328 dataBuf, -1, /* source */
329 retval, dataSize+1, /* dest */
330 NULL, NULL);
331 free(dataBuf);
332#else
333 retval = dataBuf;
334#endif
335 }
336done:
337 /* Loop freeing my temp buffers */
338 if (ppPaths) {
339 for(index=0;index<numKeys;index++)
340 if (ppPaths[index]) free(ppPaths[index]);
341 free(ppPaths);
342 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000343 if (newKey)
344 RegCloseKey(newKey);
Guido van Rossum88716bb2000-03-30 19:45:39 +0000345 if (keyBuf)
346 free(keyBuf);
Guido van Rossumeea14491997-08-13 21:30:44 +0000347 return retval;
348}
349#endif /* MS_WIN32 */
350
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000351static void
352get_progpath()
353{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000354 extern char *Py_GetProgramName();
355 char *path = getenv("PATH");
356 char *prog = Py_GetProgramName();
357
Guido van Rossumeea14491997-08-13 21:30:44 +0000358#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000359#ifdef UNICODE
360 WCHAR wprogpath[MAXPATHLEN+1];
361 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
362 WideCharToMultiByte(CP_ACP, 0, wprogpath, -1, progpath, MAXPATHLEN+1, NULL, NULL);
363 return;
364 }
365#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000366 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
367 return;
368#endif
Guido van Rossum88716bb2000-03-30 19:45:39 +0000369#endif
Guido van Rossumeea14491997-08-13 21:30:44 +0000370 if (prog == NULL || *prog == '\0')
371 prog = "python";
372
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000373 /* If there is no slash in the argv0 path, then we have to
374 * assume python is on the user's $PATH, since there's no
375 * other way to find a directory to start the search from. If
376 * $PATH isn't exported, you lose.
377 */
378#ifdef ALTSEP
379 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
380#else
381 if (strchr(prog, SEP))
382#endif
383 strcpy(progpath, prog);
384 else if (path) {
385 while (1) {
386 char *delim = strchr(path, DELIM);
387
388 if (delim) {
Guido van Rossum1c44e282000-06-28 22:20:06 +0000389 size_t len = delim - path;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000390 strncpy(progpath, path, len);
391 *(progpath + len) = '\0';
392 }
393 else
394 strcpy(progpath, path);
395
396 join(progpath, prog);
397 if (exists(progpath))
398 break;
399
400 if (!delim) {
401 progpath[0] = '\0';
402 break;
403 }
404 path = delim + 1;
405 }
406 }
407 else
408 progpath[0] = '\0';
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000409}
410
411static void
412calculate_path()
413{
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000414 char argv0_path[MAXPATHLEN+1];
415 char *buf;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000416 size_t bufsz;
Guido van Rossum8b2b3ce1998-07-27 13:48:07 +0000417 char *pythonhome = Py_GetPythonHome();
Guido van Rossumeea14491997-08-13 21:30:44 +0000418 char *envpath = getenv("PYTHONPATH");
Guido van Rossumeea14491997-08-13 21:30:44 +0000419
Guido van Rossum43ff1141998-08-08 23:40:40 +0000420#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000421 int skiphome, skipdefault;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000422 char *machinepath = NULL;
423 char *userpath = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000424#endif
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000425
426 get_progpath();
427 strcpy(argv0_path, progpath);
428 reduce(argv0_path);
Guido van Rossumeea14491997-08-13 21:30:44 +0000429 if (pythonhome == NULL || *pythonhome == '\0') {
430 if (search_for_prefix(argv0_path, LANDMARK))
431 pythonhome = prefix;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000432 else
433 pythonhome = NULL;
Guido van Rossumeea14491997-08-13 21:30:44 +0000434 }
435 else
436 strcpy(prefix, pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000437
Guido van Rossumeea14491997-08-13 21:30:44 +0000438 if (envpath && *envpath == '\0')
439 envpath = NULL;
440
Guido van Rossume02e48b2000-03-29 01:49:47 +0000441
Guido van Rossum43ff1141998-08-08 23:40:40 +0000442#ifdef MS_WIN32
Guido van Rossum88716bb2000-03-30 19:45:39 +0000443 skiphome = pythonhome==NULL ? 0 : 1;
444 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
445 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
446 /* We only use the default relative PYTHONPATH if we havent
447 anything better to use! */
448 skipdefault = envpath!=NULL || pythonhome!=NULL || \
449 machinepath!=NULL || userpath!=NULL;
Guido van Rossum43ff1141998-08-08 23:40:40 +0000450#endif
451
452 /* We need to construct a path from the following parts.
Guido van Rossumeea14491997-08-13 21:30:44 +0000453 (1) the PYTHONPATH environment variable, if set;
454 (2) for Win32, the machinepath and userpath, if set;
455 (3) the PYTHONPATH config macro, with the leading "."
456 of each component replaced with pythonhome, if set;
457 (4) the directory containing the executable (argv0_path).
458 The length calculation calculates #3 first.
Guido van Rossum43ff1141998-08-08 23:40:40 +0000459 Extra rules:
460 - If PYTHONHOME is set (in any way) item (2) is ignored.
461 - If registry values are used, (3) and (4) are ignored.
Guido van Rossumeea14491997-08-13 21:30:44 +0000462 */
463
464 /* Calculate size of return buffer */
465 if (pythonhome != NULL) {
466 char *p;
467 bufsz = 1;
468 for (p = PYTHONPATH; *p; p++) {
469 if (*p == DELIM)
470 bufsz++; /* number of DELIM plus one */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000471 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000472 bufsz *= strlen(pythonhome);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000473 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000474 else
475 bufsz = 0;
Guido van Rossum691d2ad1997-12-11 02:32:43 +0000476 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum8f1b6511997-08-13 19:55:43 +0000477 bufsz += strlen(argv0_path) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000478#ifdef MS_WIN32
Guido van Rossumeea14491997-08-13 21:30:44 +0000479 if (userpath)
480 bufsz += strlen(userpath) + 1;
Guido van Rossum67ab6721998-08-08 19:58:59 +0000481 if (machinepath)
482 bufsz += strlen(machinepath) + 1;
Guido van Rossumeea14491997-08-13 21:30:44 +0000483#endif
Guido van Rossum67ab6721998-08-08 19:58:59 +0000484 if (envpath != NULL)
485 bufsz += strlen(envpath) + 1;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000486
487 module_search_path = buf = malloc(bufsz);
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000488 if (buf == NULL) {
489 /* We can't exit, so print a warning and limp along */
Guido van Rossumeea14491997-08-13 21:30:44 +0000490 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
491 if (envpath) {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000492 fprintf(stderr, "Using environment $PYTHONPATH.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000493 module_search_path = envpath;
494 }
495 else {
Guido van Rossume02e48b2000-03-29 01:49:47 +0000496 fprintf(stderr, "Using default static path.\n");
Guido van Rossumeea14491997-08-13 21:30:44 +0000497 module_search_path = PYTHONPATH;
498 }
Guido van Rossum42a97441998-02-19 21:00:45 +0000499#ifdef MS_WIN32
500 if (machinepath)
501 free(machinepath);
502 if (userpath)
503 free(userpath);
504#endif /* MS_WIN32 */
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000505 return;
506 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000507
508 if (envpath) {
509 strcpy(buf, envpath);
510 buf = strchr(buf, '\0');
511 *buf++ = DELIM;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000512 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000513#ifdef MS_WIN32
Guido van Rossum67ab6721998-08-08 19:58:59 +0000514 if (userpath) {
515 strcpy(buf, userpath);
516 buf = strchr(buf, '\0');
517 *buf++ = DELIM;
518 free(userpath);
519 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000520 if (machinepath) {
521 strcpy(buf, machinepath);
522 buf = strchr(buf, '\0');
523 *buf++ = DELIM;
Guido van Rossum42a97441998-02-19 21:00:45 +0000524 free(machinepath);
Guido van Rossume02e48b2000-03-29 01:49:47 +0000525 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000526 if (pythonhome == NULL) {
527 if (!skipdefault) {
528 strcpy(buf, PYTHONPATH);
529 buf = strchr(buf, '\0');
530 }
Guido van Rossumeea14491997-08-13 21:30:44 +0000531 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000532#else
Guido van Rossumeea14491997-08-13 21:30:44 +0000533 if (pythonhome == NULL) {
534 strcpy(buf, PYTHONPATH);
535 buf = strchr(buf, '\0');
536 }
Guido van Rossum88716bb2000-03-30 19:45:39 +0000537#endif /* MS_WIN32 */
Guido van Rossumeea14491997-08-13 21:30:44 +0000538 else {
539 char *p = PYTHONPATH;
540 char *q;
Guido van Rossum1c44e282000-06-28 22:20:06 +0000541 size_t n;
Guido van Rossumeea14491997-08-13 21:30:44 +0000542 for (;;) {
543 q = strchr(p, DELIM);
544 if (q == NULL)
545 n = strlen(p);
546 else
547 n = q-p;
548 if (p[0] == '.' && is_sep(p[1])) {
549 strcpy(buf, pythonhome);
550 buf = strchr(buf, '\0');
551 p++;
552 n--;
553 }
554 strncpy(buf, p, n);
555 buf += n;
556 if (q == NULL)
557 break;
558 *buf++ = DELIM;
559 p = q+1;
560 }
561 }
562 if (argv0_path) {
563 *buf++ = DELIM;
564 strcpy(buf, argv0_path);
565 buf = strchr(buf, '\0');
566 }
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000567 *buf = '\0';
568}
569
570
571/* External interface */
572
573char *
574Py_GetPath()
575{
576 if (!module_search_path)
577 calculate_path();
578 return module_search_path;
579}
580
581char *
582Py_GetPrefix()
583{
Guido van Rossumeea14491997-08-13 21:30:44 +0000584 if (!module_search_path)
585 calculate_path();
586 return prefix;
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000587}
588
589char *
590Py_GetExecPrefix()
591{
Guido van Rossumeea14491997-08-13 21:30:44 +0000592 return Py_GetPrefix();
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000593}
594
595char *
Guido van Rossumeea14491997-08-13 21:30:44 +0000596Py_GetProgramFullPath()
Guido van Rossum1aa7e3a1997-05-19 14:16:21 +0000597{
598 if (!module_search_path)
599 calculate_path();
600 return progpath;
601}