blob: 78b4915fa5ed6a07428b2dc0148b081a3e1bfdb6 [file] [log] [blame]
Guido van Rossum582646a1996-05-28 22:30:17 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum582646a1996-05-28 22:30:17 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum582646a1996-05-28 22:30:17 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum 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.
Guido van Rossum582646a1996-05-28 22:30:17 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While 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.
Guido van Rossum582646a1996-05-28 22:30:17 +000029
30******************************************************************/
31
32/* Return the initial module search path. */
33
Guido van Rossum667d7041995-08-04 04:20:48 +000034#include "Python.h"
35#include "osdefs.h"
36
Guido van Rossum305e5d01997-04-11 17:18:45 +000037#include <sys/types.h>
38#include <sys/stat.h>
Guido van Rossum21f84971997-06-02 22:18:31 +000039#include <string.h>
Guido van Rossum667d7041995-08-04 04:20:48 +000040
Guido van Rossum305e5d01997-04-11 17:18:45 +000041#if HAVE_UNISTD_H
42#include <unistd.h>
43#endif /* HAVE_UNISTD_H */
44
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000045#ifdef WITH_NEXT_FRAMEWORK
46#include <mach-o/dyld.h>
47#endif
48
Guido van Rossum305e5d01997-04-11 17:18:45 +000049/* Search in some common locations for the associated Python libraries.
50 *
51 * Two directories must be found, the platform independent directory
Barry Warsaw90126031997-04-11 20:27:03 +000052 * (prefix), containing the common .py and .pyc files, and the platform
53 * dependent directory (exec_prefix), containing the shared library
54 * modules. Note that prefix and exec_prefix can be the same directory,
55 * but for some installations, they are different.
Guido van Rossum305e5d01997-04-11 17:18:45 +000056 *
Barry Warsaw90126031997-04-11 20:27:03 +000057 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
58 * Each search tries a number of different locations until a ``landmark''
59 * file or directory is found. If no prefix or exec_prefix is found, a
60 * warning message is issued and the preprocessor defined PREFIX and
61 * EXEC_PREFIX are used (even though they will not work); python carries on
62 * as best as is possible, but most imports will fail.
Guido van Rossum305e5d01997-04-11 17:18:45 +000063 *
64 * Before any searches are done, the location of the executable is
Barry Warsaw90126031997-04-11 20:27:03 +000065 * determined. If argv[0] has one or more slashs in it, it is used
66 * unchanged. Otherwise, it must have been invoked from the shell's path,
67 * so we search $PATH for the named executable and use that. If the
68 * executable was not found on $PATH (or there was no $PATH environment
69 * variable), the original argv[0] string is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000070 *
Barry Warsaw90126031997-04-11 20:27:03 +000071 * Next, the executable location is examined to see if it is a symbolic
72 * link. If so, the link is chased (correctly interpreting a relative
73 * pathname if one is found) and the directory of the link target is used.
Guido van Rossum305e5d01997-04-11 17:18:45 +000074 *
Barry Warsaw90126031997-04-11 20:27:03 +000075 * Finally, argv0_path is set to the directory containing the executable
76 * (i.e. the last component is stripped).
Guido van Rossum305e5d01997-04-11 17:18:45 +000077 *
Barry Warsaw90126031997-04-11 20:27:03 +000078 * With argv0_path in hand, we perform a number of steps. The same steps
79 * are performed for prefix and for exec_prefix, but with a different
80 * landmark.
Guido van Rossum305e5d01997-04-11 17:18:45 +000081 *
82 * Step 1. Are we running python out of the build directory? This is
83 * checked by looking for a different kind of landmark relative to
Barry Warsaw90126031997-04-11 20:27:03 +000084 * argv0_path. For prefix, the landmark's path is derived from the VPATH
85 * preprocessor variable (taking into account that its value is almost, but
86 * not quite, what we need). For exec_prefix, the landmark is
87 * Modules/Setup. If the landmark is found, we're done.
Guido van Rossum305e5d01997-04-11 17:18:45 +000088 *
89 * For the remaining steps, the prefix landmark will always be
90 * lib/python$VERSION/string.py and the exec_prefix will always be
Guido van Rossum266033e1997-10-20 23:20:32 +000091 * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
Barry Warsaw90126031997-04-11 20:27:03 +000092 * number as supplied by the Makefile. Note that this means that no more
93 * build directory checking is performed; if the first step did not find
94 * the landmarks, the assumption is that python is running from an
95 * installed setup.
Guido van Rossum305e5d01997-04-11 17:18:45 +000096 *
97 * Step 2. See if the $PYTHONHOME environment variable points to the
Barry Warsaw90126031997-04-11 20:27:03 +000098 * installed location of the Python libraries. If $PYTHONHOME is set, then
99 * it points to prefix and exec_prefix. $PYTHONHOME can be a single
100 * directory, which is used for both, or the prefix and exec_prefix
101 * directories separated by a colon.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000102 *
103 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
Barry Warsaw90126031997-04-11 20:27:03 +0000104 * backtracking up the path until it is exhausted. This is the most common
105 * step to succeed. Note that if prefix and exec_prefix are different,
106 * exec_prefix is more likely to be found; however if exec_prefix is a
107 * subdirectory of prefix, both will be found.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000108 *
Barry Warsaw90126031997-04-11 20:27:03 +0000109 * Step 4. Search the directories pointed to by the preprocessor variables
110 * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be
111 * passed in as options to the configure script.
Guido van Rossum305e5d01997-04-11 17:18:45 +0000112 *
Barry Warsaw90126031997-04-11 20:27:03 +0000113 * That's it!
114 *
115 * Well, almost. Once we have determined prefix and exec_prefix, the
116 * preprocesor variable PYTHONPATH is used to construct a path. Each
117 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
118 * containing the shared library modules is appended. The environment
119 * variable $PYTHONPATH is inserted in front of it all. Finally, the
120 * prefix and exec_prefix globals are tweaked so they reflect the values
121 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
122 * off. If either points to the build directory, the globals are reset to
123 * the corresponding preprocessor variables (so sys.prefix will reflect the
124 * installation location, even though sys.path points into the build
125 * directory). This seems to make more sense given that currently the only
126 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
127 * process to find the installed Python tree.
128 */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000129
130#ifndef VERSION
131#define VERSION "1.5"
132#endif
133
134#ifndef VPATH
135#define VPATH "."
Guido van Rossum667d7041995-08-04 04:20:48 +0000136#endif
137
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000138#ifndef PREFIX
139#define PREFIX "/usr/local"
140#endif
141
142#ifndef EXEC_PREFIX
Guido van Rossum6e12d561996-07-30 20:36:12 +0000143#define EXEC_PREFIX PREFIX
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000144#endif
145
Guido van Rossum305e5d01997-04-11 17:18:45 +0000146#ifndef PYTHONPATH
147/* I know this isn't K&R C, but the Makefile specifies it anyway */
148#define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
Guido van Rossum266033e1997-10-20 23:20:32 +0000149 EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
Guido van Rossum305e5d01997-04-11 17:18:45 +0000150#endif
Guido van Rossum667d7041995-08-04 04:20:48 +0000151
Guido van Rossum305e5d01997-04-11 17:18:45 +0000152#ifndef LANDMARK
153#define LANDMARK "string.py"
154#endif
155
Guido van Rossum305e5d01997-04-11 17:18:45 +0000156static char prefix[MAXPATHLEN+1];
157static char exec_prefix[MAXPATHLEN+1];
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000158static char progpath[MAXPATHLEN+1];
Guido van Rossum305e5d01997-04-11 17:18:45 +0000159static char *module_search_path = NULL;
160static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
161
162static void
163reduce(dir)
164 char *dir;
165{
166 int i = strlen(dir);
167 while (i > 0 && dir[i] != SEP)
168 --i;
169 dir[i] = '\0';
170}
Guido van Rossumd29806c1998-01-19 22:06:22 +0000171
172
173#ifndef S_ISREG
174#define S_ISREG(x) (((x) & S_IFMT) == S_IFREG)
175#endif
176
177#ifndef S_ISDIR
178#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
179#endif
Guido van Rossum305e5d01997-04-11 17:18:45 +0000180
181static int
Guido van Rossumd29806c1998-01-19 22:06:22 +0000182isfile(filename) /* Is file, not directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000183 char *filename;
184{
185 struct stat buf;
Guido van Rossumd29806c1998-01-19 22:06:22 +0000186 if (stat(filename, &buf) != 0)
187 return 0;
188 if (!S_ISREG(buf.st_mode))
189 return 0;
190 return 1;
191}
192
193
194static int
195ismodule(filename) /* Is module -- check for .pyc/.pyo too */
196 char *filename;
197{
198 if (isfile(filename))
199 return 1;
200
201 /* Check for the compiled version of prefix. */
202 if (strlen(filename) < MAXPATHLEN) {
203 strcat(filename, Py_OptimizeFlag ? "o" : "c");
204 if (isfile(filename))
205 return 1;
206 }
207 return 0;
208}
209
210
211static int
212isxfile(filename) /* Is executable file */
213 char *filename;
214{
215 struct stat buf;
216 if (stat(filename, &buf) != 0)
217 return 0;
218 if (!S_ISREG(buf.st_mode))
219 return 0;
220 if ((buf.st_mode & 0111) == 0)
221 return 0;
222 return 1;
223}
224
225
226static int
227isdir(filename) /* Is directory */
228 char *filename;
229{
230 struct stat buf;
231 if (stat(filename, &buf) != 0)
232 return 0;
233 if (!S_ISDIR(buf.st_mode))
234 return 0;
235 return 1;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000236}
237
238
239static void
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000240joinpath(buffer, stuff)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000241 char *buffer;
242 char *stuff;
243{
244 int n, k;
245 if (stuff[0] == SEP)
246 n = 0;
247 else {
248 n = strlen(buffer);
249 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
250 buffer[n++] = SEP;
251 }
252 k = strlen(stuff);
253 if (n + k > MAXPATHLEN)
254 k = MAXPATHLEN - n;
255 strncpy(buffer+n, stuff, k);
256 buffer[n+k] = '\0';
257}
258
259
Guido van Rossum305e5d01997-04-11 17:18:45 +0000260static int
261search_for_prefix(argv0_path, home)
262 char *argv0_path;
263 char *home;
264{
Guido van Rossum7d3246d1997-05-13 19:19:41 +0000265 int n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000266 char *vpath;
267
Guido van Rossum573a24a1997-05-12 20:49:39 +0000268 /* Check to see if argv[0] is in the build directory */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000269 strcpy(prefix, argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000270 joinpath(prefix, "Modules/Setup");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000271 if (isfile(prefix)) {
Guido van Rossum573a24a1997-05-12 20:49:39 +0000272 /* Check VPATH to see if argv0_path is in the build directory.
273 * Complication: the VPATH passed in is relative to the
274 * Modules build directory and points to the Modules source
275 * directory; we need it relative to the build tree and
276 * pointing to the source tree. Solution: chop off a leading
277 * ".." (but only if it's there -- it could be an absolute
278 * path) and chop off the final component (assuming it's
279 * "Modules").
280 */
281 vpath = VPATH;
282 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
283 vpath += 3;
284 strcpy(prefix, argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000285 joinpath(prefix, vpath);
Guido van Rossum573a24a1997-05-12 20:49:39 +0000286 reduce(prefix);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000287 joinpath(prefix, "Lib");
288 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000289 if (ismodule(prefix))
Guido van Rossum573a24a1997-05-12 20:49:39 +0000290 return -1;
291 }
Guido van Rossum305e5d01997-04-11 17:18:45 +0000292
293 if (home) {
294 /* Check $PYTHONHOME */
295 char *delim;
296 strcpy(prefix, home);
297 delim = strchr(prefix, DELIM);
298 if (delim)
299 *delim = '\0';
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000300 joinpath(prefix, lib_python);
301 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000302 if (ismodule(prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000303 return 1;
304 }
305
306 /* Search from argv0_path, until root is found */
307 strcpy(prefix, argv0_path);
308 do {
309 n = strlen(prefix);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000310 joinpath(prefix, lib_python);
311 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000312 if (ismodule(prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000313 return 1;
314 prefix[n] = '\0';
315 reduce(prefix);
316 } while (prefix[0]);
317
318 /* Look at configure's PREFIX */
319 strcpy(prefix, PREFIX);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000320 joinpath(prefix, lib_python);
321 joinpath(prefix, LANDMARK);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000322 if (ismodule(prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000323 return 1;
324
Guido van Rossumd7763621997-05-12 20:53:23 +0000325 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000326 return 0;
327}
328
329
330static int
331search_for_exec_prefix(argv0_path, home)
332 char *argv0_path;
333 char *home;
334{
Guido van Rossum7d3246d1997-05-13 19:19:41 +0000335 int n;
Guido van Rossum305e5d01997-04-11 17:18:45 +0000336
337 /* Check to see if argv[0] is in the build directory */
338 strcpy(exec_prefix, argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000339 joinpath(exec_prefix, "Modules/Setup");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000340 if (isfile(exec_prefix)) {
Guido van Rossum305e5d01997-04-11 17:18:45 +0000341 reduce(exec_prefix);
342 return -1;
343 }
344
345 if (home) {
346 /* Check $PYTHONHOME */
347 char *delim;
348 delim = strchr(home, DELIM);
349 if (delim)
350 strcpy(exec_prefix, delim+1);
351 else
352 strcpy(exec_prefix, home);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000353 joinpath(exec_prefix, lib_python);
Guido van Rossum266033e1997-10-20 23:20:32 +0000354 joinpath(exec_prefix, "lib-dynload");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000355 if (isdir(exec_prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000356 return 1;
357 }
358
359 /* Search from argv0_path, until root is found */
360 strcpy(exec_prefix, argv0_path);
361 do {
362 n = strlen(exec_prefix);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000363 joinpath(exec_prefix, lib_python);
Guido van Rossum266033e1997-10-20 23:20:32 +0000364 joinpath(exec_prefix, "lib-dynload");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000365 if (isdir(exec_prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000366 return 1;
367 exec_prefix[n] = '\0';
368 reduce(exec_prefix);
369 } while (exec_prefix[0]);
370
371 /* Look at configure's EXEC_PREFIX */
372 strcpy(exec_prefix, EXEC_PREFIX);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000373 joinpath(exec_prefix, lib_python);
Guido van Rossum266033e1997-10-20 23:20:32 +0000374 joinpath(exec_prefix, "lib-dynload");
Guido van Rossumd29806c1998-01-19 22:06:22 +0000375 if (isdir(exec_prefix))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000376 return 1;
377
Guido van Rossumd7763621997-05-12 20:53:23 +0000378 /* Fail */
Guido van Rossum305e5d01997-04-11 17:18:45 +0000379 return 0;
380}
381
382
383static void
384calculate_path()
385{
386 extern char *Py_GetProgramName();
387
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000388 static char delimiter[2] = {DELIM, '\0'};
389 static char separator[2] = {SEP, '\0'};
Guido van Rossum305e5d01997-04-11 17:18:45 +0000390 char *pythonpath = PYTHONPATH;
391 char *rtpypath = getenv("PYTHONPATH");
Guido van Rossum131c92c1998-02-06 22:29:30 +0000392 char *home = Py_GetPythonHome();
Guido van Rossum305e5d01997-04-11 17:18:45 +0000393 char *path = getenv("PATH");
394 char *prog = Py_GetProgramName();
395 char argv0_path[MAXPATHLEN+1];
Guido van Rossum305e5d01997-04-11 17:18:45 +0000396 int pfound, efound; /* 1 if found; -1 if found build directory */
397 char *buf;
398 int bufsz;
399 int prefixsz;
400 char *defpath = pythonpath;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000401#ifdef WITH_NEXT_FRAMEWORK
402 NSModule pythonModule;
403#endif
404
405#ifdef WITH_NEXT_FRAMEWORK
406 pythonModule = NSModuleForSymbol(NSLookupAndBindSymbol("_Py_Initialize"));
407 /* Use dylib functions to find out where the framework was loaded from */
408 buf = NSLibraryNameForModule(pythonModule);
409 if (buf != NULL) {
410 /* We're in a framework. */
411 strcpy(progpath, buf);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000412
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000413 /* Frameworks have support for versioning */
414 strcpy(lib_python, "lib");
415 } else {
416 /* If we're not in a framework, fall back to the old way (even though NSNameOfModule() probably does the same thing.) */
417#endif
418
Guido van Rossum305e5d01997-04-11 17:18:45 +0000419 /* Initialize this dynamically for K&R C */
420 sprintf(lib_python, "lib/python%s", VERSION);
421
422 /* If there is no slash in the argv0 path, then we have to
423 * assume python is on the user's $PATH, since there's no
424 * other way to find a directory to start the search from. If
425 * $PATH isn't exported, you lose.
426 */
427 if (strchr(prog, SEP))
428 strcpy(progpath, prog);
429 else if (path) {
430 while (1) {
431 char *delim = strchr(path, DELIM);
432
433 if (delim) {
434 int len = delim - path;
435 strncpy(progpath, path, len);
436 *(progpath + len) = '\0';
437 }
438 else
439 strcpy(progpath, path);
440
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000441 joinpath(progpath, prog);
Guido van Rossumd29806c1998-01-19 22:06:22 +0000442 if (isxfile(progpath))
Guido van Rossum305e5d01997-04-11 17:18:45 +0000443 break;
444
445 if (!delim) {
446 progpath[0] = '\0';
447 break;
448 }
449 path = delim + 1;
450 }
451 }
452 else
453 progpath[0] = '\0';
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000454#ifdef WITH_NEXT_FRAMEWORK
455 }
456#endif
Guido van Rossum305e5d01997-04-11 17:18:45 +0000457
458 strcpy(argv0_path, progpath);
459
460#if HAVE_READLINK
461 {
462 char tmpbuffer[MAXPATHLEN+1];
463 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
Guido van Rossum302be441998-04-29 21:07:06 +0000464 while (linklen != -1) {
Guido van Rossum305e5d01997-04-11 17:18:45 +0000465 /* It's not null terminated! */
466 tmpbuffer[linklen] = '\0';
467 if (tmpbuffer[0] == SEP)
468 strcpy(argv0_path, tmpbuffer);
469 else {
470 /* Interpret relative to progpath */
471 reduce(argv0_path);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000472 joinpath(argv0_path, tmpbuffer);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000473 }
Guido van Rossum302be441998-04-29 21:07:06 +0000474 linklen = readlink(argv0_path, tmpbuffer, MAXPATHLEN);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000475 }
476 }
477#endif /* HAVE_READLINK */
478
479 reduce(argv0_path);
480
481 if (!(pfound = search_for_prefix(argv0_path, home))) {
Guido van Rossum131c92c1998-02-06 22:29:30 +0000482 if (!Py_FrozenFlag)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000483 fprintf(stderr,
484 "Could not find platform independent libraries <prefix>\n");
485 strcpy(prefix, PREFIX);
Guido van Rossum6b9fdf51997-08-20 23:48:16 +0000486 joinpath(prefix, lib_python);
Guido van Rossum305e5d01997-04-11 17:18:45 +0000487 }
488 else
489 reduce(prefix);
490
491 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
Guido van Rossum131c92c1998-02-06 22:29:30 +0000492 if (!Py_FrozenFlag)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000493 fprintf(stderr,
494 "Could not find platform dependent libraries <exec_prefix>\n");
495 strcpy(exec_prefix, EXEC_PREFIX);
Guido van Rossum266033e1997-10-20 23:20:32 +0000496 joinpath(exec_prefix, "lib/lib-dynload");
Guido van Rossum305e5d01997-04-11 17:18:45 +0000497 }
498 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
499
Guido van Rossum131c92c1998-02-06 22:29:30 +0000500 if ((!pfound || !efound) && !Py_FrozenFlag)
Guido van Rossum305e5d01997-04-11 17:18:45 +0000501 fprintf(stderr,
502 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
503
504 /* Calculate size of return buffer.
505 */
506 bufsz = 0;
507
508 if (rtpypath)
509 bufsz += strlen(rtpypath) + 1;
510
511 prefixsz = strlen(prefix) + 1;
512
513 while (1) {
514 char *delim = strchr(defpath, DELIM);
515
516 if (defpath[0] != SEP)
517 /* Paths are relative to prefix */
518 bufsz += prefixsz;
519
520 if (delim)
521 bufsz += delim - defpath + 1;
522 else {
523 bufsz += strlen(defpath) + 1;
524 break;
525 }
526 defpath = delim + 1;
527 }
528
529 bufsz += strlen(exec_prefix) + 1;
530
531 /* This is the only malloc call in this file */
532 buf = malloc(bufsz);
533
534 if (buf == NULL) {
535 /* We can't exit, so print a warning and limp along */
536 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
537 fprintf(stderr, "Using default static PYTHONPATH.\n");
538 module_search_path = PYTHONPATH;
539 }
540 else {
541 /* Run-time value of $PYTHONPATH goes first */
542 if (rtpypath) {
543 strcpy(buf, rtpypath);
544 strcat(buf, delimiter);
545 }
Guido van Rossum573a24a1997-05-12 20:49:39 +0000546 else
547 buf[0] = '\0';
Guido van Rossum305e5d01997-04-11 17:18:45 +0000548
549 /* Next goes merge of compile-time $PYTHONPATH with
550 * dynamically located prefix.
551 */
552 defpath = pythonpath;
553 while (1) {
554 char *delim = strchr(defpath, DELIM);
555
556 if (defpath[0] != SEP) {
557 strcat(buf, prefix);
558 strcat(buf, separator);
559 }
560
561 if (delim) {
562 int len = delim - defpath + 1;
563 int end = strlen(buf) + len;
564 strncat(buf, defpath, len);
565 *(buf + end) = '\0';
566 }
567 else {
568 strcat(buf, defpath);
569 break;
570 }
571 defpath = delim + 1;
572 }
573 strcat(buf, delimiter);
574
575 /* Finally, on goes the directory for dynamic-load modules */
576 strcat(buf, exec_prefix);
577
578 /* And publish the results */
579 module_search_path = buf;
580 }
581
582 /* Reduce prefix and exec_prefix to their essence,
583 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
584 * If we're loading relative to the build directory,
585 * return the compiled-in defaults instead.
586 */
587 if (pfound > 0) {
588 reduce(prefix);
589 reduce(prefix);
590 }
591 else
592 strcpy(prefix, PREFIX);
593
594 if (efound > 0) {
595 reduce(exec_prefix);
596 reduce(exec_prefix);
597 reduce(exec_prefix);
598 }
599 else
600 strcpy(exec_prefix, EXEC_PREFIX);
601}
602
603
604/* External interface */
Guido van Rossum667d7041995-08-04 04:20:48 +0000605
606char *
Guido van Rossum582646a1996-05-28 22:30:17 +0000607Py_GetPath()
Guido van Rossum667d7041995-08-04 04:20:48 +0000608{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000609 if (!module_search_path)
610 calculate_path();
611 return module_search_path;
Guido van Rossum667d7041995-08-04 04:20:48 +0000612}
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000613
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000614char *
615Py_GetPrefix()
616{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000617 if (!module_search_path)
618 calculate_path();
619 return prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000620}
621
622char *
623Py_GetExecPrefix()
624{
Guido van Rossum305e5d01997-04-11 17:18:45 +0000625 if (!module_search_path)
626 calculate_path();
627 return exec_prefix;
Guido van Rossumc34c9a51996-06-12 04:20:27 +0000628}
Guido van Rossum7929c6f1997-05-20 22:38:21 +0000629
630char *
631Py_GetProgramFullPath()
632{
633 if (!module_search_path)
634 calculate_path();
635 return progpath;
636}