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