getpathp.c 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9

/* Return the initial module search path. */
/* Used by DOS, OS/2, Windows 3.1.  Works on NT too. */

#include "Python.h"
#include "osdefs.h"

#ifdef MS_WIN32
#include <windows.h>
10
extern BOOL PyWin_IsWin32s(void);
11 12 13 14 15 16 17 18 19 20 21 22
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>

#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

/* Search in some common locations for the associated Python libraries.
 *
23 24 25 26 27
 * Two directories must be found, the platform independent directory
 * (prefix), containing the common .py and .pyc files, and the platform
 * dependent directory (exec_prefix), containing the shared library
 * modules.  Note that prefix and exec_prefix can be the same directory,
 * but for some installations, they are different.
28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * Py_GetPath() tries to return a sensible Python module search path.
 *
 * First, we look to see if the executable is in a subdirectory of
 * the Python build directory.  We calculate the full path of the
 * directory containing the executable as progpath.  We work backwards
 * along progpath and look for $dir/Modules/Setup.in, a distinctive
 * landmark.  If found, we use $dir/Lib as $root.  The returned
 * Python path is the compiled #define PYTHONPATH with all the initial
 * "./lib" replaced by $root.
 *
 * Otherwise, if there is a PYTHONPATH environment variable, we return that.
 *
41
 * Otherwise we try to find $progpath/lib/os.py, and if found, then
42 43 44 45 46 47
 * root is $progpath/lib, and we return Python path as compiled PYTHONPATH
 * with all "./lib" replaced by $root (as above).
 *
 */

#ifndef LANDMARK
48
#define LANDMARK "lib\\os.py"
49 50 51
#endif

static char prefix[MAXPATHLEN+1];
52
static char exec_prefix[MAXPATHLEN+1];
53 54 55 56 57
static char progpath[MAXPATHLEN+1];
static char *module_search_path = NULL;


static int
58
is_sep(char ch)	/* determine if "ch" is a separator character */
59 60 61 62 63 64 65 66 67 68
{
#ifdef ALTSEP
	return ch == SEP || ch == ALTSEP;
#else
	return ch == SEP;
#endif
}


static void
69
reduce(char *dir)
70 71 72 73 74 75 76 77 78
{
	int i = strlen(dir);
	while (i > 0 && !is_sep(dir[i]))
		--i;
	dir[i] = '\0';
}
	

static int
79
exists(char *filename)
80 81 82 83 84 85
{
	struct stat buf;
	return stat(filename, &buf) == 0;
}


86 87 88 89 90 91 92 93 94
/* Add a path component, by appending stuff to buffer.
   buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
   NUL-terminated string with no more than MAXPATHLEN characters (not counting
   the trailing NUL).  It's a fatal error if it contains a string longer than
   that (callers must be careful!).  If these requirements are met, it's
   guaranteed that buffer will still be a NUL-terminated string with no more
   than MAXPATHLEN characters at exit.  If stuff is too long, only as much of
   stuff as fits will be appended.
*/
95
static void
96
join(char *buffer, char *stuff)
97 98 99 100 101 102 103 104 105
{
	int n, k;
	if (is_sep(stuff[0]))
		n = 0;
	else {
		n = strlen(buffer);
		if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
			buffer[n++] = SEP;
	}
106 107
	if (n > MAXPATHLEN)
		Py_FatalError("buffer overflow in getpathp.c's joinpath()");
108 109 110 111 112 113 114 115 116
	k = strlen(stuff);
	if (n + k > MAXPATHLEN)
		k = MAXPATHLEN - n;
	strncpy(buffer+n, stuff, k);
	buffer[n+k] = '\0';
}


static int
117
search_for_prefix(char *argv0_path, char *landmark)
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
{
	int n;

	/* Search from argv0_path, until root is found */
	strcpy(prefix, argv0_path);
	do {
		n = strlen(prefix);
		join(prefix, landmark);
		if (exists(prefix)) {
			prefix[n] = '\0';
			return 1;
		}
		prefix[n] = '\0';
		reduce(prefix);
	} while (prefix[0]);
	return 0;
}

#ifdef MS_WIN32
#include "malloc.h" // for alloca - see comments below!
extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.


/* Load a PYTHONPATH value from the registry.
   Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.

   Returns NULL, or a pointer that should be freed.
*/

static char *
getpythonregpath(HKEY keyBase, BOOL bWin32s)
{
	HKEY newKey = 0;
	DWORD nameSize = 0;
	DWORD dataSize = 0;
	DWORD numEntries = 0;
	LONG rc;
	char *retval = NULL;
	char *dataBuf;
	const char keyPrefix[] = "Software\\Python\\PythonCore\\";
	const char keySuffix[] = "\\PythonPath";
	int versionLen;
	char *keyBuf;

	// Tried to use sysget("winver") but here is too early :-(
	versionLen = strlen(PyWin_DLLVersionString);
	// alloca == no free required, but memory only local to fn.
	// also no heap fragmentation!  Am I being silly?
	keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
	// lots of constants here for the compiler to optimize away :-)
	memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
	memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
	memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!

	rc=RegOpenKey(keyBase,
		      keyBuf,
		      &newKey);
	if (rc==ERROR_SUCCESS) {
		RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL, 
		                &numEntries, &nameSize, &dataSize, NULL, NULL);
	}
	if (bWin32s && numEntries==0 && dataSize==0) {
		/* must hardcode for Win32s */
		numEntries = 1;
		dataSize = 511;
	}
	if (numEntries) {
		/* Loop over all subkeys. */
		/* Win32s doesnt know how many subkeys, so we do
		   it twice */
		char keyBuf[MAX_PATH+1];
		int index = 0;
		int off = 0;
		for(index=0;;index++) {
			long reqdSize = 0;
			DWORD rc = RegEnumKey(newKey,
					      index, keyBuf, MAX_PATH+1);
			if (rc) break;
			rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
			if (rc) break;
			if (bWin32s && reqdSize==0) reqdSize = 512;
			dataSize += reqdSize + 1; /* 1 for the ";" */
		}
		dataBuf = malloc(dataSize+1);
		if (dataBuf==NULL)
			return NULL; /* pretty serious?  Raise error? */
		/* Now loop over, grabbing the paths.
		   Subkeys before main library */
		for(index=0;;index++) {
			int adjust;
			long reqdSize = dataSize;
			DWORD rc = RegEnumKey(newKey,
					      index, keyBuf,MAX_PATH+1);
			if (rc) break;
			rc = RegQueryValue(newKey,
					   keyBuf, dataBuf+off, &reqdSize);
			if (rc) break;
			if (reqdSize>1) {
				/* If Nothing, or only '\0' copied. */
				adjust = strlen(dataBuf+off);
				dataSize -= adjust;
				off += adjust;
				dataBuf[off++] = ';';
				dataBuf[off] = '\0';
				dataSize--;
			}
		}
		/* Additionally, win32s doesnt work as expected, so
		   the specific strlen() is required for 3.1. */
		rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
		if (rc==ERROR_SUCCESS) {
			if (strlen(dataBuf)==0)
				free(dataBuf);
			else
				retval = dataBuf; /* caller will free */
		}
		else
			free(dataBuf);
	}

	if (newKey)
		RegCloseKey(newKey);
	return retval;
}
#endif /* MS_WIN32 */

static void
245
get_progpath(void)
246
{
247
	extern char *Py_GetProgramName(void);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	char *path = getenv("PATH");
	char *prog = Py_GetProgramName();

#ifdef MS_WIN32
	if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
		return;
#endif
	if (prog == NULL || *prog == '\0')
		prog = "python";

	/* If there is no slash in the argv0 path, then we have to
	 * assume python is on the user's $PATH, since there's no
	 * other way to find a directory to start the search from.  If
	 * $PATH isn't exported, you lose.
	 */
#ifdef ALTSEP
	if (strchr(prog, SEP) || strchr(prog, ALTSEP))
#else
	if (strchr(prog, SEP))
#endif
		strcpy(progpath, prog);
	else if (path) {
		while (1) {
			char *delim = strchr(path, DELIM);

			if (delim) {
				int len = delim - path;
				strncpy(progpath, path, len);
				*(progpath + len) = '\0';
			}
			else
				strcpy(progpath, path);

			join(progpath, prog);
			if (exists(progpath))
				break;

			if (!delim) {
				progpath[0] = '\0';
				break;
			}
			path = delim + 1;
		}
	}
	else
		progpath[0] = '\0';
}

static void
297
calculate_path(void)
298 299 300 301
{
	char argv0_path[MAXPATHLEN+1];
	char *buf;
	int bufsz;
302
	char *pythonhome = Py_GetPythonHome();
303
	char *envpath = Py_GETENV("PYTHONPATH");
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
#ifdef MS_WIN32
	char *machinepath, *userpath;

	/* Are we running under Windows 3.1(1) Win32s? */
	if (PyWin_IsWin32s()) {
		/* Only CLASSES_ROOT is supported */
		machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE); 
		userpath = NULL;
	} else {
		machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
		userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
	}
#endif

	get_progpath();
	strcpy(argv0_path, progpath);
	reduce(argv0_path);
	if (pythonhome == NULL || *pythonhome == '\0') {
		if (search_for_prefix(argv0_path, LANDMARK))
			pythonhome = prefix;
		else
			pythonhome = NULL;
	}
327 328 329
	else {
        char *delim;

330 331
		strcpy(prefix, pythonhome);

332 333 334 335 336 337 338 339 340 341
        /* Extract Any Optional Trailing EXEC_PREFIX */
        /* e.g. PYTHONHOME=<prefix>:<exec_prefix>   */
        delim = strchr(prefix, DELIM);
        if (delim) {
            *delim = '\0';
            strcpy(exec_prefix, delim+1);
        } else
            strcpy(exec_prefix, EXEC_PREFIX);
    }

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	if (envpath && *envpath == '\0')
		envpath = NULL;

	/* We need to construct a path from the following parts:
	   (1) the PYTHONPATH environment variable, if set;
	   (2) for Win32, the machinepath and userpath, if set;
	   (3) the PYTHONPATH config macro, with the leading "."
	       of each component replaced with pythonhome, if set;
	   (4) the directory containing the executable (argv0_path).
	   The length calculation calculates #3 first.
	*/

	/* Calculate size of return buffer */
	if (pythonhome != NULL) {
		char *p;
		bufsz = 1;	
		for (p = PYTHONPATH; *p; p++) {
			if (*p == DELIM)
				bufsz++; /* number of DELIM plus one */
		}
		bufsz *= strlen(pythonhome);
	}
	else
		bufsz = 0;
366
	bufsz += strlen(PYTHONPATH) + 1;
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
	if (envpath != NULL)
		bufsz += strlen(envpath) + 1;
	bufsz += strlen(argv0_path) + 1;
#ifdef MS_WIN32
	if (machinepath)
		bufsz += strlen(machinepath) + 1;
	if (userpath)
		bufsz += strlen(userpath) + 1;
#endif

	module_search_path = buf = malloc(bufsz);
	if (buf == NULL) {
		/* We can't exit, so print a warning and limp along */
		fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
		if (envpath) {
			fprintf(stderr, "Using default static $PYTHONPATH.\n");
			module_search_path = envpath;
		}
		else {
			fprintf(stderr, "Using environment $PYTHONPATH.\n");
			module_search_path = PYTHONPATH;
		}
		return;
	}

	if (envpath) {
		strcpy(buf, envpath);
		buf = strchr(buf, '\0');
		*buf++ = DELIM;
	}
#ifdef MS_WIN32
	if (machinepath) {
		strcpy(buf, machinepath);
		buf = strchr(buf, '\0');
		*buf++ = DELIM;
	}
	if (userpath) {
		strcpy(buf, userpath);
		buf = strchr(buf, '\0');
		*buf++ = DELIM;
	}
#endif
	if (pythonhome == NULL) {
		strcpy(buf, PYTHONPATH);
		buf = strchr(buf, '\0');
	}
	else {
		char *p = PYTHONPATH;
		char *q;
		int n;
		for (;;) {
			q = strchr(p, DELIM);
			if (q == NULL)
				n = strlen(p);
			else
				n = q-p;
			if (p[0] == '.' && is_sep(p[1])) {
				strcpy(buf, pythonhome);
				buf = strchr(buf, '\0');
				p++;
				n--;
			}
			strncpy(buf, p, n);
			buf += n;
			if (q == NULL)
				break;
			*buf++ = DELIM;
			p = q+1;
		}
	}
	if (argv0_path) {
		*buf++ = DELIM;
		strcpy(buf, argv0_path);
		buf = strchr(buf, '\0');
	}
	*buf = '\0';
}


/* External interface */

char *
449
Py_GetPath(void)
450 451 452 453 454 455 456 457
{
	if (!module_search_path)
		calculate_path();

	return module_search_path;
}

char *
458
Py_GetPrefix(void)
459 460 461 462 463 464 465 466
{
	if (!module_search_path)
		calculate_path();

	return prefix;
}

char *
467
Py_GetExecPrefix(void)
468
{
469 470 471 472
	if (!module_search_path)
		calculate_path();

	return exec_prefix;
473 474 475
}

char *
476
Py_GetProgramFullPath(void)
477 478 479 480 481 482
{
	if (!module_search_path)
		calculate_path();

	return progpath;
}