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