blob: f796851493337e519752afcf0747602dc69350fd [file] [log] [blame]
Guido van Rossum50d4cc21997-11-22 21:59:45 +00001/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00002Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
Guido van Rossum50d4cc21997-11-22 21:59:45 +00006
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00007See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossum50d4cc21997-11-22 21:59:45 +00009******************************************************************/
10
11/* Return the initial module search path. */
12/* Used by DOS, OS/2, Windows 3.1. Works on NT too. */
13
14#include "Python.h"
15#include "osdefs.h"
16
17#ifdef MS_WIN32
18#include <windows.h>
19extern BOOL PyWin_IsWin32s();
20#endif
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <string.h>
25
26#if HAVE_UNISTD_H
27#include <unistd.h>
28#endif /* HAVE_UNISTD_H */
29
30/* Search in some common locations for the associated Python libraries.
31 *
Guido van Rossuma34c3131997-12-05 22:07:14 +000032 * Two directories must be found, the platform independent directory
33 * (prefix), containing the common .py and .pyc files, and the platform
34 * dependent directory (exec_prefix), containing the shared library
35 * modules. Note that prefix and exec_prefix can be the same directory,
36 * but for some installations, they are different.
Guido van Rossum50d4cc21997-11-22 21:59:45 +000037 *
38 * Py_GetPath() tries to return a sensible Python module search path.
39 *
40 * First, we look to see if the executable is in a subdirectory of
41 * the Python build directory. We calculate the full path of the
42 * directory containing the executable as progpath. We work backwards
43 * along progpath and look for $dir/Modules/Setup.in, a distinctive
44 * landmark. If found, we use $dir/Lib as $root. The returned
45 * Python path is the compiled #define PYTHONPATH with all the initial
46 * "./lib" replaced by $root.
47 *
48 * Otherwise, if there is a PYTHONPATH environment variable, we return that.
49 *
Jeremy Hylton847a9962000-05-26 21:49:07 +000050 * Otherwise we try to find $progpath/lib/os.py, and if found, then
Guido van Rossum50d4cc21997-11-22 21:59:45 +000051 * root is $progpath/lib, and we return Python path as compiled PYTHONPATH
52 * with all "./lib" replaced by $root (as above).
53 *
54 */
55
56#ifndef LANDMARK
Jeremy Hylton847a9962000-05-26 21:49:07 +000057#define LANDMARK "lib\\os.py"
Guido van Rossum50d4cc21997-11-22 21:59:45 +000058#endif
59
60static char prefix[MAXPATHLEN+1];
Guido van Rossuma34c3131997-12-05 22:07:14 +000061static char exec_prefix[MAXPATHLEN+1];
Guido van Rossum50d4cc21997-11-22 21:59:45 +000062static char progpath[MAXPATHLEN+1];
63static char *module_search_path = NULL;
64
65
66static int
67is_sep(ch) /* determine if "ch" is a separator character */
68 char ch;
69{
70#ifdef ALTSEP
71 return ch == SEP || ch == ALTSEP;
72#else
73 return ch == SEP;
74#endif
75}
76
77
78static void
79reduce(dir)
80 char *dir;
81{
82 int i = strlen(dir);
83 while (i > 0 && !is_sep(dir[i]))
84 --i;
85 dir[i] = '\0';
86}
87
88
89static int
90exists(filename)
91 char *filename;
92{
93 struct stat buf;
94 return stat(filename, &buf) == 0;
95}
96
97
98static void
99join(buffer, stuff)
100 char *buffer;
101 char *stuff;
102{
103 int n, k;
104 if (is_sep(stuff[0]))
105 n = 0;
106 else {
107 n = strlen(buffer);
108 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
109 buffer[n++] = SEP;
110 }
111 k = strlen(stuff);
112 if (n + k > MAXPATHLEN)
113 k = MAXPATHLEN - n;
114 strncpy(buffer+n, stuff, k);
115 buffer[n+k] = '\0';
116}
117
118
119static int
120search_for_prefix(argv0_path, landmark)
121 char *argv0_path;
122 char *landmark;
123{
124 int n;
125
126 /* Search from argv0_path, until root is found */
127 strcpy(prefix, argv0_path);
128 do {
129 n = strlen(prefix);
130 join(prefix, landmark);
131 if (exists(prefix)) {
132 prefix[n] = '\0';
133 return 1;
134 }
135 prefix[n] = '\0';
136 reduce(prefix);
137 } while (prefix[0]);
138 return 0;
139}
140
141#ifdef MS_WIN32
142#include "malloc.h" // for alloca - see comments below!
143extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
144
145
146/* Load a PYTHONPATH value from the registry.
147 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
148
149 Returns NULL, or a pointer that should be freed.
150*/
151
152static char *
153getpythonregpath(HKEY keyBase, BOOL bWin32s)
154{
155 HKEY newKey = 0;
156 DWORD nameSize = 0;
157 DWORD dataSize = 0;
158 DWORD numEntries = 0;
159 LONG rc;
160 char *retval = NULL;
161 char *dataBuf;
162 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
163 const char keySuffix[] = "\\PythonPath";
164 int versionLen;
165 char *keyBuf;
166
167 // Tried to use sysget("winver") but here is too early :-(
168 versionLen = strlen(PyWin_DLLVersionString);
169 // alloca == no free required, but memory only local to fn.
170 // also no heap fragmentation! Am I being silly?
171 keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
172 // lots of constants here for the compiler to optimize away :-)
173 memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
174 memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
175 memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!
176
177 rc=RegOpenKey(keyBase,
178 keyBuf,
179 &newKey);
180 if (rc==ERROR_SUCCESS) {
181 RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
182 &numEntries, &nameSize, &dataSize, NULL, NULL);
183 }
184 if (bWin32s && numEntries==0 && dataSize==0) {
185 /* must hardcode for Win32s */
186 numEntries = 1;
187 dataSize = 511;
188 }
189 if (numEntries) {
190 /* Loop over all subkeys. */
191 /* Win32s doesnt know how many subkeys, so we do
192 it twice */
193 char keyBuf[MAX_PATH+1];
194 int index = 0;
195 int off = 0;
196 for(index=0;;index++) {
197 long reqdSize = 0;
198 DWORD rc = RegEnumKey(newKey,
199 index, keyBuf, MAX_PATH+1);
200 if (rc) break;
201 rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
202 if (rc) break;
203 if (bWin32s && reqdSize==0) reqdSize = 512;
204 dataSize += reqdSize + 1; /* 1 for the ";" */
205 }
206 dataBuf = malloc(dataSize+1);
207 if (dataBuf==NULL)
208 return NULL; /* pretty serious? Raise error? */
209 /* Now loop over, grabbing the paths.
210 Subkeys before main library */
211 for(index=0;;index++) {
212 int adjust;
213 long reqdSize = dataSize;
214 DWORD rc = RegEnumKey(newKey,
215 index, keyBuf,MAX_PATH+1);
216 if (rc) break;
217 rc = RegQueryValue(newKey,
218 keyBuf, dataBuf+off, &reqdSize);
219 if (rc) break;
220 if (reqdSize>1) {
221 /* If Nothing, or only '\0' copied. */
222 adjust = strlen(dataBuf+off);
223 dataSize -= adjust;
224 off += adjust;
225 dataBuf[off++] = ';';
226 dataBuf[off] = '\0';
227 dataSize--;
228 }
229 }
230 /* Additionally, win32s doesnt work as expected, so
231 the specific strlen() is required for 3.1. */
232 rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
233 if (rc==ERROR_SUCCESS) {
234 if (strlen(dataBuf)==0)
235 free(dataBuf);
236 else
237 retval = dataBuf; /* caller will free */
238 }
239 else
240 free(dataBuf);
241 }
242
243 if (newKey)
244 RegCloseKey(newKey);
245 return retval;
246}
247#endif /* MS_WIN32 */
248
249static void
250get_progpath()
251{
252 extern char *Py_GetProgramName();
253 char *path = getenv("PATH");
254 char *prog = Py_GetProgramName();
255
256#ifdef MS_WIN32
257 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
258 return;
259#endif
260 if (prog == NULL || *prog == '\0')
261 prog = "python";
262
263 /* If there is no slash in the argv0 path, then we have to
264 * assume python is on the user's $PATH, since there's no
265 * other way to find a directory to start the search from. If
266 * $PATH isn't exported, you lose.
267 */
268#ifdef ALTSEP
269 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
270#else
271 if (strchr(prog, SEP))
272#endif
273 strcpy(progpath, prog);
274 else if (path) {
275 while (1) {
276 char *delim = strchr(path, DELIM);
277
278 if (delim) {
279 int len = delim - path;
280 strncpy(progpath, path, len);
281 *(progpath + len) = '\0';
282 }
283 else
284 strcpy(progpath, path);
285
286 join(progpath, prog);
287 if (exists(progpath))
288 break;
289
290 if (!delim) {
291 progpath[0] = '\0';
292 break;
293 }
294 path = delim + 1;
295 }
296 }
297 else
298 progpath[0] = '\0';
299}
300
301static void
302calculate_path()
303{
304 char argv0_path[MAXPATHLEN+1];
305 char *buf;
306 int bufsz;
Guido van Rossum28700c41998-07-27 13:49:04 +0000307 char *pythonhome = Py_GetPythonHome();
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000308 char *envpath = getenv("PYTHONPATH");
309#ifdef MS_WIN32
310 char *machinepath, *userpath;
311
312 /* Are we running under Windows 3.1(1) Win32s? */
313 if (PyWin_IsWin32s()) {
314 /* Only CLASSES_ROOT is supported */
315 machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
316 userpath = NULL;
317 } else {
318 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
319 userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
320 }
321#endif
322
323 get_progpath();
324 strcpy(argv0_path, progpath);
325 reduce(argv0_path);
326 if (pythonhome == NULL || *pythonhome == '\0') {
327 if (search_for_prefix(argv0_path, LANDMARK))
328 pythonhome = prefix;
329 else
330 pythonhome = NULL;
331 }
Guido van Rossuma34c3131997-12-05 22:07:14 +0000332 else {
333 char *delim;
334
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000335 strcpy(prefix, pythonhome);
336
Guido van Rossuma34c3131997-12-05 22:07:14 +0000337 /* Extract Any Optional Trailing EXEC_PREFIX */
338 /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
339 delim = strchr(prefix, DELIM);
340 if (delim) {
341 *delim = '\0';
342 strcpy(exec_prefix, delim+1);
343 } else
344 strcpy(exec_prefix, EXEC_PREFIX);
345 }
346
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000347 if (envpath && *envpath == '\0')
348 envpath = NULL;
349
350 /* We need to construct a path from the following parts:
351 (1) the PYTHONPATH environment variable, if set;
352 (2) for Win32, the machinepath and userpath, if set;
353 (3) the PYTHONPATH config macro, with the leading "."
354 of each component replaced with pythonhome, if set;
355 (4) the directory containing the executable (argv0_path).
356 The length calculation calculates #3 first.
357 */
358
359 /* Calculate size of return buffer */
360 if (pythonhome != NULL) {
361 char *p;
362 bufsz = 1;
363 for (p = PYTHONPATH; *p; p++) {
364 if (*p == DELIM)
365 bufsz++; /* number of DELIM plus one */
366 }
367 bufsz *= strlen(pythonhome);
368 }
369 else
370 bufsz = 0;
Guido van Rossumd0ec7611997-12-11 15:21:33 +0000371 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000372 if (envpath != NULL)
373 bufsz += strlen(envpath) + 1;
374 bufsz += strlen(argv0_path) + 1;
375#ifdef MS_WIN32
376 if (machinepath)
377 bufsz += strlen(machinepath) + 1;
378 if (userpath)
379 bufsz += strlen(userpath) + 1;
380#endif
381
382 module_search_path = buf = malloc(bufsz);
383 if (buf == NULL) {
384 /* We can't exit, so print a warning and limp along */
385 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
386 if (envpath) {
387 fprintf(stderr, "Using default static $PYTHONPATH.\n");
388 module_search_path = envpath;
389 }
390 else {
391 fprintf(stderr, "Using environment $PYTHONPATH.\n");
392 module_search_path = PYTHONPATH;
393 }
394 return;
395 }
396
397 if (envpath) {
398 strcpy(buf, envpath);
399 buf = strchr(buf, '\0');
400 *buf++ = DELIM;
401 }
402#ifdef MS_WIN32
403 if (machinepath) {
404 strcpy(buf, machinepath);
405 buf = strchr(buf, '\0');
406 *buf++ = DELIM;
407 }
408 if (userpath) {
409 strcpy(buf, userpath);
410 buf = strchr(buf, '\0');
411 *buf++ = DELIM;
412 }
413#endif
414 if (pythonhome == NULL) {
415 strcpy(buf, PYTHONPATH);
416 buf = strchr(buf, '\0');
417 }
418 else {
419 char *p = PYTHONPATH;
420 char *q;
421 int n;
422 for (;;) {
423 q = strchr(p, DELIM);
424 if (q == NULL)
425 n = strlen(p);
426 else
427 n = q-p;
428 if (p[0] == '.' && is_sep(p[1])) {
429 strcpy(buf, pythonhome);
430 buf = strchr(buf, '\0');
431 p++;
432 n--;
433 }
434 strncpy(buf, p, n);
435 buf += n;
436 if (q == NULL)
437 break;
438 *buf++ = DELIM;
439 p = q+1;
440 }
441 }
442 if (argv0_path) {
443 *buf++ = DELIM;
444 strcpy(buf, argv0_path);
445 buf = strchr(buf, '\0');
446 }
447 *buf = '\0';
448}
449
450
451/* External interface */
452
453char *
454Py_GetPath()
455{
456 if (!module_search_path)
457 calculate_path();
458
459 return module_search_path;
460}
461
462char *
463Py_GetPrefix()
464{
465 if (!module_search_path)
466 calculate_path();
467
468 return prefix;
469}
470
471char *
472Py_GetExecPrefix()
473{
Guido van Rossuma34c3131997-12-05 22:07:14 +0000474 if (!module_search_path)
475 calculate_path();
476
477 return exec_prefix;
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000478}
479
480char *
481Py_GetProgramFullPath()
482{
483 if (!module_search_path)
484 calculate_path();
485
486 return progpath;
487}