blob: 533689c63f53b8d3f27308e640ab62599061766a [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>
Thomas Woutersa5345942000-07-22 23:59:33 +000019extern BOOL PyWin_IsWin32s(void);
Guido van Rossum50d4cc21997-11-22 21:59:45 +000020#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
Thomas Wouters78890102000-07-22 19:25:51 +000067is_sep(char ch) /* determine if "ch" is a separator character */
Guido van Rossum50d4cc21997-11-22 21:59:45 +000068{
69#ifdef ALTSEP
70 return ch == SEP || ch == ALTSEP;
71#else
72 return ch == SEP;
73#endif
74}
75
76
77static void
Thomas Wouters78890102000-07-22 19:25:51 +000078reduce(char *dir)
Guido van Rossum50d4cc21997-11-22 21:59:45 +000079{
80 int i = strlen(dir);
81 while (i > 0 && !is_sep(dir[i]))
82 --i;
83 dir[i] = '\0';
84}
85
86
87static int
Thomas Wouters78890102000-07-22 19:25:51 +000088exists(char *filename)
Guido van Rossum50d4cc21997-11-22 21:59:45 +000089{
90 struct stat buf;
91 return stat(filename, &buf) == 0;
92}
93
94
95static void
Thomas Wouters78890102000-07-22 19:25:51 +000096join(char *buffer, char *stuff)
Guido van Rossum50d4cc21997-11-22 21:59:45 +000097{
98 int n, k;
99 if (is_sep(stuff[0]))
100 n = 0;
101 else {
102 n = strlen(buffer);
103 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
104 buffer[n++] = SEP;
105 }
106 k = strlen(stuff);
107 if (n + k > MAXPATHLEN)
108 k = MAXPATHLEN - n;
109 strncpy(buffer+n, stuff, k);
110 buffer[n+k] = '\0';
111}
112
113
114static int
Thomas Wouters78890102000-07-22 19:25:51 +0000115search_for_prefix(char *argv0_path, char *landmark)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000116{
117 int n;
118
119 /* Search from argv0_path, until root is found */
120 strcpy(prefix, argv0_path);
121 do {
122 n = strlen(prefix);
123 join(prefix, landmark);
124 if (exists(prefix)) {
125 prefix[n] = '\0';
126 return 1;
127 }
128 prefix[n] = '\0';
129 reduce(prefix);
130 } while (prefix[0]);
131 return 0;
132}
133
134#ifdef MS_WIN32
135#include "malloc.h" // for alloca - see comments below!
136extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
137
138
139/* Load a PYTHONPATH value from the registry.
140 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
141
142 Returns NULL, or a pointer that should be freed.
143*/
144
145static char *
146getpythonregpath(HKEY keyBase, BOOL bWin32s)
147{
148 HKEY newKey = 0;
149 DWORD nameSize = 0;
150 DWORD dataSize = 0;
151 DWORD numEntries = 0;
152 LONG rc;
153 char *retval = NULL;
154 char *dataBuf;
155 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
156 const char keySuffix[] = "\\PythonPath";
157 int versionLen;
158 char *keyBuf;
159
160 // Tried to use sysget("winver") but here is too early :-(
161 versionLen = strlen(PyWin_DLLVersionString);
162 // alloca == no free required, but memory only local to fn.
163 // also no heap fragmentation! Am I being silly?
164 keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
165 // lots of constants here for the compiler to optimize away :-)
166 memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
167 memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
168 memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!
169
170 rc=RegOpenKey(keyBase,
171 keyBuf,
172 &newKey);
173 if (rc==ERROR_SUCCESS) {
174 RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
175 &numEntries, &nameSize, &dataSize, NULL, NULL);
176 }
177 if (bWin32s && numEntries==0 && dataSize==0) {
178 /* must hardcode for Win32s */
179 numEntries = 1;
180 dataSize = 511;
181 }
182 if (numEntries) {
183 /* Loop over all subkeys. */
184 /* Win32s doesnt know how many subkeys, so we do
185 it twice */
186 char keyBuf[MAX_PATH+1];
187 int index = 0;
188 int off = 0;
189 for(index=0;;index++) {
190 long reqdSize = 0;
191 DWORD rc = RegEnumKey(newKey,
192 index, keyBuf, MAX_PATH+1);
193 if (rc) break;
194 rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
195 if (rc) break;
196 if (bWin32s && reqdSize==0) reqdSize = 512;
197 dataSize += reqdSize + 1; /* 1 for the ";" */
198 }
199 dataBuf = malloc(dataSize+1);
200 if (dataBuf==NULL)
201 return NULL; /* pretty serious? Raise error? */
202 /* Now loop over, grabbing the paths.
203 Subkeys before main library */
204 for(index=0;;index++) {
205 int adjust;
206 long reqdSize = dataSize;
207 DWORD rc = RegEnumKey(newKey,
208 index, keyBuf,MAX_PATH+1);
209 if (rc) break;
210 rc = RegQueryValue(newKey,
211 keyBuf, dataBuf+off, &reqdSize);
212 if (rc) break;
213 if (reqdSize>1) {
214 /* If Nothing, or only '\0' copied. */
215 adjust = strlen(dataBuf+off);
216 dataSize -= adjust;
217 off += adjust;
218 dataBuf[off++] = ';';
219 dataBuf[off] = '\0';
220 dataSize--;
221 }
222 }
223 /* Additionally, win32s doesnt work as expected, so
224 the specific strlen() is required for 3.1. */
225 rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
226 if (rc==ERROR_SUCCESS) {
227 if (strlen(dataBuf)==0)
228 free(dataBuf);
229 else
230 retval = dataBuf; /* caller will free */
231 }
232 else
233 free(dataBuf);
234 }
235
236 if (newKey)
237 RegCloseKey(newKey);
238 return retval;
239}
240#endif /* MS_WIN32 */
241
242static void
Thomas Wouters78890102000-07-22 19:25:51 +0000243get_progpath(void)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000244{
Thomas Woutersa5345942000-07-22 23:59:33 +0000245 extern char *Py_GetProgramName(void);
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000246 char *path = getenv("PATH");
247 char *prog = Py_GetProgramName();
248
249#ifdef MS_WIN32
250 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
251 return;
252#endif
253 if (prog == NULL || *prog == '\0')
254 prog = "python";
255
256 /* If there is no slash in the argv0 path, then we have to
257 * assume python is on the user's $PATH, since there's no
258 * other way to find a directory to start the search from. If
259 * $PATH isn't exported, you lose.
260 */
261#ifdef ALTSEP
262 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
263#else
264 if (strchr(prog, SEP))
265#endif
266 strcpy(progpath, prog);
267 else if (path) {
268 while (1) {
269 char *delim = strchr(path, DELIM);
270
271 if (delim) {
272 int len = delim - path;
273 strncpy(progpath, path, len);
274 *(progpath + len) = '\0';
275 }
276 else
277 strcpy(progpath, path);
278
279 join(progpath, prog);
280 if (exists(progpath))
281 break;
282
283 if (!delim) {
284 progpath[0] = '\0';
285 break;
286 }
287 path = delim + 1;
288 }
289 }
290 else
291 progpath[0] = '\0';
292}
293
294static void
Thomas Wouters78890102000-07-22 19:25:51 +0000295calculate_path(void)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000296{
297 char argv0_path[MAXPATHLEN+1];
298 char *buf;
299 int bufsz;
Guido van Rossum28700c41998-07-27 13:49:04 +0000300 char *pythonhome = Py_GetPythonHome();
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000301 char *envpath = getenv("PYTHONPATH");
302#ifdef MS_WIN32
303 char *machinepath, *userpath;
304
305 /* Are we running under Windows 3.1(1) Win32s? */
306 if (PyWin_IsWin32s()) {
307 /* Only CLASSES_ROOT is supported */
308 machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
309 userpath = NULL;
310 } else {
311 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
312 userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
313 }
314#endif
315
316 get_progpath();
317 strcpy(argv0_path, progpath);
318 reduce(argv0_path);
319 if (pythonhome == NULL || *pythonhome == '\0') {
320 if (search_for_prefix(argv0_path, LANDMARK))
321 pythonhome = prefix;
322 else
323 pythonhome = NULL;
324 }
Guido van Rossuma34c3131997-12-05 22:07:14 +0000325 else {
326 char *delim;
327
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000328 strcpy(prefix, pythonhome);
329
Guido van Rossuma34c3131997-12-05 22:07:14 +0000330 /* Extract Any Optional Trailing EXEC_PREFIX */
331 /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
332 delim = strchr(prefix, DELIM);
333 if (delim) {
334 *delim = '\0';
335 strcpy(exec_prefix, delim+1);
336 } else
337 strcpy(exec_prefix, EXEC_PREFIX);
338 }
339
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000340 if (envpath && *envpath == '\0')
341 envpath = NULL;
342
343 /* We need to construct a path from the following parts:
344 (1) the PYTHONPATH environment variable, if set;
345 (2) for Win32, the machinepath and userpath, if set;
346 (3) the PYTHONPATH config macro, with the leading "."
347 of each component replaced with pythonhome, if set;
348 (4) the directory containing the executable (argv0_path).
349 The length calculation calculates #3 first.
350 */
351
352 /* Calculate size of return buffer */
353 if (pythonhome != NULL) {
354 char *p;
355 bufsz = 1;
356 for (p = PYTHONPATH; *p; p++) {
357 if (*p == DELIM)
358 bufsz++; /* number of DELIM plus one */
359 }
360 bufsz *= strlen(pythonhome);
361 }
362 else
363 bufsz = 0;
Guido van Rossumd0ec7611997-12-11 15:21:33 +0000364 bufsz += strlen(PYTHONPATH) + 1;
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000365 if (envpath != NULL)
366 bufsz += strlen(envpath) + 1;
367 bufsz += strlen(argv0_path) + 1;
368#ifdef MS_WIN32
369 if (machinepath)
370 bufsz += strlen(machinepath) + 1;
371 if (userpath)
372 bufsz += strlen(userpath) + 1;
373#endif
374
375 module_search_path = buf = malloc(bufsz);
376 if (buf == NULL) {
377 /* We can't exit, so print a warning and limp along */
378 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
379 if (envpath) {
380 fprintf(stderr, "Using default static $PYTHONPATH.\n");
381 module_search_path = envpath;
382 }
383 else {
384 fprintf(stderr, "Using environment $PYTHONPATH.\n");
385 module_search_path = PYTHONPATH;
386 }
387 return;
388 }
389
390 if (envpath) {
391 strcpy(buf, envpath);
392 buf = strchr(buf, '\0');
393 *buf++ = DELIM;
394 }
395#ifdef MS_WIN32
396 if (machinepath) {
397 strcpy(buf, machinepath);
398 buf = strchr(buf, '\0');
399 *buf++ = DELIM;
400 }
401 if (userpath) {
402 strcpy(buf, userpath);
403 buf = strchr(buf, '\0');
404 *buf++ = DELIM;
405 }
406#endif
407 if (pythonhome == NULL) {
408 strcpy(buf, PYTHONPATH);
409 buf = strchr(buf, '\0');
410 }
411 else {
412 char *p = PYTHONPATH;
413 char *q;
414 int n;
415 for (;;) {
416 q = strchr(p, DELIM);
417 if (q == NULL)
418 n = strlen(p);
419 else
420 n = q-p;
421 if (p[0] == '.' && is_sep(p[1])) {
422 strcpy(buf, pythonhome);
423 buf = strchr(buf, '\0');
424 p++;
425 n--;
426 }
427 strncpy(buf, p, n);
428 buf += n;
429 if (q == NULL)
430 break;
431 *buf++ = DELIM;
432 p = q+1;
433 }
434 }
435 if (argv0_path) {
436 *buf++ = DELIM;
437 strcpy(buf, argv0_path);
438 buf = strchr(buf, '\0');
439 }
440 *buf = '\0';
441}
442
443
444/* External interface */
445
446char *
Thomas Wouters78890102000-07-22 19:25:51 +0000447Py_GetPath(void)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000448{
449 if (!module_search_path)
450 calculate_path();
451
452 return module_search_path;
453}
454
455char *
Thomas Wouters78890102000-07-22 19:25:51 +0000456Py_GetPrefix(void)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000457{
458 if (!module_search_path)
459 calculate_path();
460
461 return prefix;
462}
463
464char *
Thomas Wouters78890102000-07-22 19:25:51 +0000465Py_GetExecPrefix(void)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000466{
Guido van Rossuma34c3131997-12-05 22:07:14 +0000467 if (!module_search_path)
468 calculate_path();
469
470 return exec_prefix;
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000471}
472
473char *
Thomas Wouters78890102000-07-22 19:25:51 +0000474Py_GetProgramFullPath(void)
Guido van Rossum50d4cc21997-11-22 21:59:45 +0000475{
476 if (!module_search_path)
477 calculate_path();
478
479 return progpath;
480}