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