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