annotate Modules/getpath.c @ 6719:faebc1940432 legacy-trunk

Understand -Xlinker option.
author Guido van Rossum <guido@python.org>
date Tue, 02 Dec 1997 20:38:11 +0000
parents 4981fd2b3211
children 663a49e4bed3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3621
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
1 /***********************************************************
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
3 The Netherlands.
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
4
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
5 All Rights Reserved
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
8 documentation for any purpose and without fee is hereby granted,
3621
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
10 both that copyright notice and this permission notice appear in
3621
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
15 permission.
3621
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 3878
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
3621
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
29
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
30 ******************************************************************/
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
31
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
32 /* Return the initial module search path. */
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
33
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "Python.h"
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #include "osdefs.h"
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
37 #include <sys/types.h>
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
38 #include <sys/stat.h>
5698
ba860dc0a585 Use string.h, not strings.h
Guido van Rossum <guido@python.org>
parents: 5568
diff changeset
39 #include <string.h>
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
41 #if HAVE_UNISTD_H
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
42 #include <unistd.h>
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
43 #endif /* HAVE_UNISTD_H */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
44
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
45 /* Search in some common locations for the associated Python libraries.
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
46 *
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
47 * Two directories must be found, the platform independent directory
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
48 * (prefix), containing the common .py and .pyc files, and the platform
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
49 * dependent directory (exec_prefix), containing the shared library
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
50 * modules. Note that prefix and exec_prefix can be the same directory,
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
51 * but for some installations, they are different.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
52 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
53 * Py_GetPath() carries out separate searches for prefix and exec_prefix.
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
54 * Each search tries a number of different locations until a ``landmark''
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
55 * file or directory is found. If no prefix or exec_prefix is found, a
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
56 * warning message is issued and the preprocessor defined PREFIX and
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
57 * EXEC_PREFIX are used (even though they will not work); python carries on
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
58 * as best as is possible, but most imports will fail.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
59 *
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
60 * Before any searches are done, the location of the executable is
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
61 * determined. If argv[0] has one or more slashs in it, it is used
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
62 * unchanged. Otherwise, it must have been invoked from the shell's path,
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
63 * so we search $PATH for the named executable and use that. If the
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
64 * executable was not found on $PATH (or there was no $PATH environment
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
65 * variable), the original argv[0] string is used.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
66 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
67 * Next, the executable location is examined to see if it is a symbolic
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
68 * link. If so, the link is chased (correctly interpreting a relative
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
69 * pathname if one is found) and the directory of the link target is used.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
70 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
71 * Finally, argv0_path is set to the directory containing the executable
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
72 * (i.e. the last component is stripped).
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
73 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
74 * With argv0_path in hand, we perform a number of steps. The same steps
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
75 * are performed for prefix and for exec_prefix, but with a different
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
76 * landmark.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
77 *
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
78 * Step 1. Are we running python out of the build directory? This is
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
79 * checked by looking for a different kind of landmark relative to
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
80 * argv0_path. For prefix, the landmark's path is derived from the VPATH
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
81 * preprocessor variable (taking into account that its value is almost, but
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
82 * not quite, what we need). For exec_prefix, the landmark is
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
83 * Modules/Setup. If the landmark is found, we're done.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
84 *
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
85 * For the remaining steps, the prefix landmark will always be
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
86 * lib/python$VERSION/string.py and the exec_prefix will always be
6498
4981fd2b3211 Change sharedmodules to lib-dynload.
Guido van Rossum <guido@python.org>
parents: 6121
diff changeset
87 * lib/python$VERSION/lib-dynload, where $VERSION is Python's version
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
88 * number as supplied by the Makefile. Note that this means that no more
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
89 * build directory checking is performed; if the first step did not find
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
90 * the landmarks, the assumption is that python is running from an
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
91 * installed setup.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
92 *
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
93 * Step 2. See if the $PYTHONHOME environment variable points to the
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
94 * installed location of the Python libraries. If $PYTHONHOME is set, then
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
95 * it points to prefix and exec_prefix. $PYTHONHOME can be a single
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
96 * directory, which is used for both, or the prefix and exec_prefix
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
97 * directories separated by a colon.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
98 *
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
99 * Step 3. Try to find prefix and exec_prefix relative to argv0_path,
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
100 * backtracking up the path until it is exhausted. This is the most common
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
101 * step to succeed. Note that if prefix and exec_prefix are different,
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
102 * exec_prefix is more likely to be found; however if exec_prefix is a
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
103 * subdirectory of prefix, both will be found.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
104 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
105 * Step 4. Search the directories pointed to by the preprocessor variables
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
106 * PREFIX and EXEC_PREFIX. These are supplied by the Makefile but can be
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
107 * passed in as options to the configure script.
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
108 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
109 * That's it!
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
110 *
5316
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
111 * Well, almost. Once we have determined prefix and exec_prefix, the
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
112 * preprocesor variable PYTHONPATH is used to construct a path. Each
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
113 * relative path on PYTHONPATH is prefixed with prefix. Then the directory
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
114 * containing the shared library modules is appended. The environment
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
115 * variable $PYTHONPATH is inserted in front of it all. Finally, the
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
116 * prefix and exec_prefix globals are tweaked so they reflect the values
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
117 * expected by other code, by stripping the "lib/python$VERSION/..." stuff
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
118 * off. If either points to the build directory, the globals are reset to
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
119 * the corresponding preprocessor variables (so sys.prefix will reflect the
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
120 * installation location, even though sys.path points into the build
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
121 * directory). This seems to make more sense given that currently the only
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
122 * known use of sys.prefix and sys.exec_prefix is for the ILU installation
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
123 * process to find the installed Python tree.
c8e24c4b8bfb Removed two unused static function: string_addsep() and
Barry Warsaw <barry@python.org>
parents: 5300
diff changeset
124 */
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
125
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
126 #ifndef VERSION
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
127 #define VERSION "1.5"
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
128 #endif
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
129
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
130 #ifndef VPATH
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
131 #define VPATH "."
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 #endif
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133
3674
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
134 #ifndef PREFIX
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
135 #define PREFIX "/usr/local"
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
136 #endif
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
137
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
138 #ifndef EXEC_PREFIX
3878
4286910080a9 Don't have "." in the default path.
Guido van Rossum <guido@python.org>
parents: 3674
diff changeset
139 #define EXEC_PREFIX PREFIX
3674
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
140 #endif
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
141
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
142 #ifndef PYTHONPATH
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
143 /* I know this isn't K&R C, but the Makefile specifies it anyway */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
144 #define PYTHONPATH PREFIX "/lib/python" VERSION ":" \
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
145 PREFIX "/lib/python" VERSION "/test" ":" \
6498
4981fd2b3211 Change sharedmodules to lib-dynload.
Guido van Rossum <guido@python.org>
parents: 6121
diff changeset
146 EXEC_PREFIX "/lib/python" VERSION "/lib-dynload"
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
147 #endif
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
149 #ifndef LANDMARK
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
150 #define LANDMARK "string.py"
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
151 #endif
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
152
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
153 static char prefix[MAXPATHLEN+1];
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
154 static char exec_prefix[MAXPATHLEN+1];
5568
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
155 static char progpath[MAXPATHLEN+1];
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
156 static char *module_search_path = NULL;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
157 static char lib_python[20]; /* Dynamically set to "lib/python" VERSION */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
158
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
159 static void
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
160 reduce(dir)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
161 char *dir;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
162 {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
163 int i = strlen(dir);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
164 while (i > 0 && dir[i] != SEP)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
165 --i;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
166 dir[i] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
167 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
168
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
169
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
170 static int
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
171 exists(filename)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
172 char *filename;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
173 {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
174 struct stat buf;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
175 return stat(filename, &buf) == 0;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
176 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
177
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
178
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
179 static void
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
180 joinpath(buffer, stuff)
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
181 char *buffer;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
182 char *stuff;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
183 {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
184 int n, k;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
185 if (stuff[0] == SEP)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
186 n = 0;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
187 else {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
188 n = strlen(buffer);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
189 if (n > 0 && buffer[n-1] != SEP && n < MAXPATHLEN)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
190 buffer[n++] = SEP;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
191 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
192 k = strlen(stuff);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
193 if (n + k > MAXPATHLEN)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
194 k = MAXPATHLEN - n;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
195 strncpy(buffer+n, stuff, k);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
196 buffer[n+k] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
197 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
198
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
199
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
200 static int
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
201 search_for_prefix(argv0_path, home)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
202 char *argv0_path;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
203 char *home;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
204 {
5500
821912fd965a Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 5483
diff changeset
205 int n;
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
206 char *vpath;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
207
5482
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
208 /* Check to see if argv[0] is in the build directory */
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
209 strcpy(prefix, argv0_path);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
210 joinpath(prefix, "Modules/Setup");
5482
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
211 if (exists(prefix)) {
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
212 /* Check VPATH to see if argv0_path is in the build directory.
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
213 * Complication: the VPATH passed in is relative to the
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
214 * Modules build directory and points to the Modules source
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
215 * directory; we need it relative to the build tree and
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
216 * pointing to the source tree. Solution: chop off a leading
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
217 * ".." (but only if it's there -- it could be an absolute
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
218 * path) and chop off the final component (assuming it's
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
219 * "Modules").
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
220 */
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
221 vpath = VPATH;
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
222 if (vpath[0] == '.' && vpath[1] == '.' && vpath[2] == '/')
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
223 vpath += 3;
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
224 strcpy(prefix, argv0_path);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
225 joinpath(prefix, vpath);
5482
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
226 reduce(prefix);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
227 joinpath(prefix, "Lib");
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
228 joinpath(prefix, LANDMARK);
5482
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
229 if (exists(prefix))
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
230 return -1;
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
231 }
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
232
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
233 if (home) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
234 /* Check $PYTHONHOME */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
235 char *delim;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
236 strcpy(prefix, home);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
237 delim = strchr(prefix, DELIM);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
238 if (delim)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
239 *delim = '\0';
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
240 joinpath(prefix, lib_python);
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
241 joinpath(prefix, LANDMARK);
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
242 if (exists(prefix))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
243 return 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
244 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
245
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
246 /* Search from argv0_path, until root is found */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
247 strcpy(prefix, argv0_path);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
248 do {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
249 n = strlen(prefix);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
250 joinpath(prefix, lib_python);
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
251 joinpath(prefix, LANDMARK);
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
252 if (exists(prefix))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
253 return 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
254 prefix[n] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
255 reduce(prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
256 } while (prefix[0]);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
257
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
258 /* Look at configure's PREFIX */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
259 strcpy(prefix, PREFIX);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
260 joinpath(prefix, lib_python);
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
261 joinpath(prefix, LANDMARK);
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
262 if (exists(prefix))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
263 return 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
264
5483
73509efa724b Another change suggested by Donn Cave -- forget "step 5", which
Guido van Rossum <guido@python.org>
parents: 5482
diff changeset
265 /* Fail */
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
266 return 0;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
267 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
268
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
269
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
270 static int
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
271 search_for_exec_prefix(argv0_path, home)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
272 char *argv0_path;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
273 char *home;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
274 {
5500
821912fd965a Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 5483
diff changeset
275 int n;
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
276
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
277 /* Check to see if argv[0] is in the build directory */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
278 strcpy(exec_prefix, argv0_path);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
279 joinpath(exec_prefix, "Modules/Setup");
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
280 if (exists(exec_prefix)) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
281 reduce(exec_prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
282 return -1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
283 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
284
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
285 if (home) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
286 /* Check $PYTHONHOME */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
287 char *delim;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
288 delim = strchr(home, DELIM);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
289 if (delim)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
290 strcpy(exec_prefix, delim+1);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
291 else
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
292 strcpy(exec_prefix, home);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
293 joinpath(exec_prefix, lib_python);
6498
4981fd2b3211 Change sharedmodules to lib-dynload.
Guido van Rossum <guido@python.org>
parents: 6121
diff changeset
294 joinpath(exec_prefix, "lib-dynload");
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
295 if (exists(exec_prefix))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
296 return 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
297 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
298
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
299 /* Search from argv0_path, until root is found */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
300 strcpy(exec_prefix, argv0_path);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
301 do {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
302 n = strlen(exec_prefix);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
303 joinpath(exec_prefix, lib_python);
6498
4981fd2b3211 Change sharedmodules to lib-dynload.
Guido van Rossum <guido@python.org>
parents: 6121
diff changeset
304 joinpath(exec_prefix, "lib-dynload");
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
305 if (exists(exec_prefix))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
306 return 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
307 exec_prefix[n] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
308 reduce(exec_prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
309 } while (exec_prefix[0]);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
310
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
311 /* Look at configure's EXEC_PREFIX */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
312 strcpy(exec_prefix, EXEC_PREFIX);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
313 joinpath(exec_prefix, lib_python);
6498
4981fd2b3211 Change sharedmodules to lib-dynload.
Guido van Rossum <guido@python.org>
parents: 6121
diff changeset
314 joinpath(exec_prefix, "lib-dynload");
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
315 if (exists(exec_prefix))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
316 return 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
317
5483
73509efa724b Another change suggested by Donn Cave -- forget "step 5", which
Guido van Rossum <guido@python.org>
parents: 5482
diff changeset
318 /* Fail */
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
319 return 0;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
320 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
321
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
322
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
323 static void
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
324 calculate_path()
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
325 {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
326 extern char *Py_GetProgramName();
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
327
5568
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
328 static char delimiter[2] = {DELIM, '\0'};
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
329 static char separator[2] = {SEP, '\0'};
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
330 char *pythonpath = PYTHONPATH;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
331 char *rtpypath = getenv("PYTHONPATH");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
332 char *home = getenv("PYTHONHOME");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
333 char *path = getenv("PATH");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
334 char *prog = Py_GetProgramName();
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
335 char argv0_path[MAXPATHLEN+1];
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
336 int pfound, efound; /* 1 if found; -1 if found build directory */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
337 char *buf;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
338 int bufsz;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
339 int prefixsz;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
340 char *defpath = pythonpath;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
341
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
342 /* Initialize this dynamically for K&R C */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
343 sprintf(lib_python, "lib/python%s", VERSION);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
344
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
345 /* If there is no slash in the argv0 path, then we have to
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
346 * assume python is on the user's $PATH, since there's no
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
347 * other way to find a directory to start the search from. If
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
348 * $PATH isn't exported, you lose.
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
349 */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
350 if (strchr(prog, SEP))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
351 strcpy(progpath, prog);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
352 else if (path) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
353 while (1) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
354 char *delim = strchr(path, DELIM);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
355
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
356 if (delim) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
357 int len = delim - path;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
358 strncpy(progpath, path, len);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
359 *(progpath + len) = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
360 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
361 else
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
362 strcpy(progpath, path);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
363
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
364 joinpath(progpath, prog);
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
365 if (exists(progpath))
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
366 break;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
367
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
368 if (!delim) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
369 progpath[0] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
370 break;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
371 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
372 path = delim + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
373 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
374 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
375 else
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
376 progpath[0] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
377
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
378 strcpy(argv0_path, progpath);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
379
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
380 #if HAVE_READLINK
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
381 {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
382 char tmpbuffer[MAXPATHLEN+1];
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
383 int linklen = readlink(progpath, tmpbuffer, MAXPATHLEN);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
384 if (linklen != -1) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
385 /* It's not null terminated! */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
386 tmpbuffer[linklen] = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
387 if (tmpbuffer[0] == SEP)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
388 strcpy(argv0_path, tmpbuffer);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
389 else {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
390 /* Interpret relative to progpath */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
391 reduce(argv0_path);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
392 joinpath(argv0_path, tmpbuffer);
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
393 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
394 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
395 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
396 #endif /* HAVE_READLINK */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
397
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
398 reduce(argv0_path);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
399
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
400 if (!(pfound = search_for_prefix(argv0_path, home))) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
401 fprintf(stderr,
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
402 "Could not find platform independent libraries <prefix>\n");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
403 strcpy(prefix, PREFIX);
6121
df028bc54e6f Globally renamed join() to joinpath() to avoid compilation error on Cray.
Guido van Rossum <guido@python.org>
parents: 5698
diff changeset
404 joinpath(prefix, lib_python);
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
405 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
406 else
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
407 reduce(prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
408
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
409 if (!(efound = search_for_exec_prefix(argv0_path, home))) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
410 fprintf(stderr,
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
411 "Could not find platform dependent libraries <exec_prefix>\n");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
412 strcpy(exec_prefix, EXEC_PREFIX);
6498
4981fd2b3211 Change sharedmodules to lib-dynload.
Guido van Rossum <guido@python.org>
parents: 6121
diff changeset
413 joinpath(exec_prefix, "lib/lib-dynload");
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
414 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
415 /* If we found EXEC_PREFIX do *not* reduce it! (Yet.) */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
416
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
417 if (!pfound || !efound)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
418 fprintf(stderr,
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
419 "Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]\n");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
420
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
421 /* Calculate size of return buffer.
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
422 */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
423 bufsz = 0;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
424
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
425 if (rtpypath)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
426 bufsz += strlen(rtpypath) + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
427
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
428 prefixsz = strlen(prefix) + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
429
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
430 while (1) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
431 char *delim = strchr(defpath, DELIM);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
432
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
433 if (defpath[0] != SEP)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
434 /* Paths are relative to prefix */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
435 bufsz += prefixsz;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
436
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
437 if (delim)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
438 bufsz += delim - defpath + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
439 else {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
440 bufsz += strlen(defpath) + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
441 break;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
442 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
443 defpath = delim + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
444 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
445
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
446 bufsz += strlen(exec_prefix) + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
447
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
448 /* This is the only malloc call in this file */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
449 buf = malloc(bufsz);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
450
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
451 if (buf == NULL) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
452 /* We can't exit, so print a warning and limp along */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
453 fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
454 fprintf(stderr, "Using default static PYTHONPATH.\n");
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
455 module_search_path = PYTHONPATH;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
456 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
457 else {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
458 /* Run-time value of $PYTHONPATH goes first */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
459 if (rtpypath) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
460 strcpy(buf, rtpypath);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
461 strcat(buf, delimiter);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
462 }
5482
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
463 else
6fa29756ec6a Fix problem reported by Donn Cave: if VPATH is an absolute path, it
Guido van Rossum <guido@python.org>
parents: 5316
diff changeset
464 buf[0] = '\0';
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
465
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
466 /* Next goes merge of compile-time $PYTHONPATH with
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
467 * dynamically located prefix.
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
468 */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
469 defpath = pythonpath;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
470 while (1) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
471 char *delim = strchr(defpath, DELIM);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
472
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
473 if (defpath[0] != SEP) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
474 strcat(buf, prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
475 strcat(buf, separator);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
476 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
477
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
478 if (delim) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
479 int len = delim - defpath + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
480 int end = strlen(buf) + len;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
481 strncat(buf, defpath, len);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
482 *(buf + end) = '\0';
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
483 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
484 else {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
485 strcat(buf, defpath);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
486 break;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
487 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
488 defpath = delim + 1;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
489 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
490 strcat(buf, delimiter);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
491
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
492 /* Finally, on goes the directory for dynamic-load modules */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
493 strcat(buf, exec_prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
494
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
495 /* And publish the results */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
496 module_search_path = buf;
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
497 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
498
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
499 /* Reduce prefix and exec_prefix to their essence,
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
500 * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
501 * If we're loading relative to the build directory,
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
502 * return the compiled-in defaults instead.
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
503 */
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
504 if (pfound > 0) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
505 reduce(prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
506 reduce(prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
507 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
508 else
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
509 strcpy(prefix, PREFIX);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
510
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
511 if (efound > 0) {
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
512 reduce(exec_prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
513 reduce(exec_prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
514 reduce(exec_prefix);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
515 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
516 else
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
517 strcpy(exec_prefix, EXEC_PREFIX);
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
518 }
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
519
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
520
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
521 /* External interface */
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 char *
3621
f158edccc72c grand renaming; added copyright to some files
Guido van Rossum <guido@python.org>
parents: 3416
diff changeset
524 Py_GetPath()
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 {
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
526 if (!module_search_path)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
527 calculate_path();
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
528 return module_search_path;
2896
f0d9f800a4aa Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 }
3674
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
530
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
531 char *
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
532 Py_GetPrefix()
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
533 {
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
534 if (!module_search_path)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
535 calculate_path();
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
536 return prefix;
3674
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
537 }
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
538
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
539 char *
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
540 Py_GetExecPrefix()
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
541 {
5300
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
542 if (!module_search_path)
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
543 calculate_path();
e77942562dd3 Completely revamped the way the default path is constructed.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
544 return exec_prefix;
3674
3eaf2c673a11 provide access to $(exec_)prefix
Guido van Rossum <guido@python.org>
parents: 3621
diff changeset
545 }
5568
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
546
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
547 char *
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
548 Py_GetProgramFullPath()
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
549 {
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
550 if (!module_search_path)
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
551 calculate_path();
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
552 return progpath;
139ab21d2629 Make delimiter and separator static for K&R C.
Guido van Rossum <guido@python.org>
parents: 5500
diff changeset
553 }