Bump version to 0.9.1.
[python/dscho.git] / PC / os2vacpp / getpathp.c
blob533689c63f53b8d3f27308e640ab62599061766a
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Return the initial module search path. */
12 /* Used by DOS, OS/2, Windows 3.1. Works on NT too. */
14 #include "Python.h"
15 #include "osdefs.h"
17 #ifdef MS_WIN32
18 #include <windows.h>
19 extern BOOL PyWin_IsWin32s(void);
20 #endif
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <string.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif /* HAVE_UNISTD_H */
30 /* Search in some common locations for the associated Python libraries.
32 * 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.
38 * Py_GetPath() tries to return a sensible Python module search path.
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.
48 * Otherwise, if there is a PYTHONPATH environment variable, we return that.
50 * Otherwise we try to find $progpath/lib/os.py, and if found, then
51 * root is $progpath/lib, and we return Python path as compiled PYTHONPATH
52 * with all "./lib" replaced by $root (as above).
56 #ifndef LANDMARK
57 #define LANDMARK "lib\\os.py"
58 #endif
60 static char prefix[MAXPATHLEN+1];
61 static char exec_prefix[MAXPATHLEN+1];
62 static char progpath[MAXPATHLEN+1];
63 static char *module_search_path = NULL;
66 static int
67 is_sep(char ch) /* determine if "ch" is a separator character */
69 #ifdef ALTSEP
70 return ch == SEP || ch == ALTSEP;
71 #else
72 return ch == SEP;
73 #endif
77 static void
78 reduce(char *dir)
80 int i = strlen(dir);
81 while (i > 0 && !is_sep(dir[i]))
82 --i;
83 dir[i] = '\0';
87 static int
88 exists(char *filename)
90 struct stat buf;
91 return stat(filename, &buf) == 0;
95 static void
96 join(char *buffer, char *stuff)
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;
106 k = strlen(stuff);
107 if (n + k > MAXPATHLEN)
108 k = MAXPATHLEN - n;
109 strncpy(buffer+n, stuff, k);
110 buffer[n+k] = '\0';
114 static int
115 search_for_prefix(char *argv0_path, char *landmark)
117 int n;
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;
128 prefix[n] = '\0';
129 reduce(prefix);
130 } while (prefix[0]);
131 return 0;
134 #ifdef MS_WIN32
135 #include "malloc.h" // for alloca - see comments below!
136 extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
139 /* Load a PYTHONPATH value from the registry.
140 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
142 Returns NULL, or a pointer that should be freed.
145 static char *
146 getpythonregpath(HKEY keyBase, BOOL bWin32s)
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;
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!
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);
177 if (bWin32s && numEntries==0 && dataSize==0) {
178 /* must hardcode for Win32s */
179 numEntries = 1;
180 dataSize = 511;
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 ";" */
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--;
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 */
232 else
233 free(dataBuf);
236 if (newKey)
237 RegCloseKey(newKey);
238 return retval;
240 #endif /* MS_WIN32 */
242 static void
243 get_progpath(void)
245 extern char *Py_GetProgramName(void);
246 char *path = getenv("PATH");
247 char *prog = Py_GetProgramName();
249 #ifdef MS_WIN32
250 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
251 return;
252 #endif
253 if (prog == NULL || *prog == '\0')
254 prog = "python";
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.
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);
271 if (delim) {
272 int len = delim - path;
273 strncpy(progpath, path, len);
274 *(progpath + len) = '\0';
276 else
277 strcpy(progpath, path);
279 join(progpath, prog);
280 if (exists(progpath))
281 break;
283 if (!delim) {
284 progpath[0] = '\0';
285 break;
287 path = delim + 1;
290 else
291 progpath[0] = '\0';
294 static void
295 calculate_path(void)
297 char argv0_path[MAXPATHLEN+1];
298 char *buf;
299 int bufsz;
300 char *pythonhome = Py_GetPythonHome();
301 char *envpath = getenv("PYTHONPATH");
302 #ifdef MS_WIN32
303 char *machinepath, *userpath;
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);
314 #endif
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;
325 else {
326 char *delim;
328 strcpy(prefix, pythonhome);
330 /* 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);
340 if (envpath && *envpath == '\0')
341 envpath = NULL;
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.
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 */
360 bufsz *= strlen(pythonhome);
362 else
363 bufsz = 0;
364 bufsz += strlen(PYTHONPATH) + 1;
365 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
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;
383 else {
384 fprintf(stderr, "Using environment $PYTHONPATH.\n");
385 module_search_path = PYTHONPATH;
387 return;
390 if (envpath) {
391 strcpy(buf, envpath);
392 buf = strchr(buf, '\0');
393 *buf++ = DELIM;
395 #ifdef MS_WIN32
396 if (machinepath) {
397 strcpy(buf, machinepath);
398 buf = strchr(buf, '\0');
399 *buf++ = DELIM;
401 if (userpath) {
402 strcpy(buf, userpath);
403 buf = strchr(buf, '\0');
404 *buf++ = DELIM;
406 #endif
407 if (pythonhome == NULL) {
408 strcpy(buf, PYTHONPATH);
409 buf = strchr(buf, '\0');
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--;
427 strncpy(buf, p, n);
428 buf += n;
429 if (q == NULL)
430 break;
431 *buf++ = DELIM;
432 p = q+1;
435 if (argv0_path) {
436 *buf++ = DELIM;
437 strcpy(buf, argv0_path);
438 buf = strchr(buf, '\0');
440 *buf = '\0';
444 /* External interface */
446 char *
447 Py_GetPath(void)
449 if (!module_search_path)
450 calculate_path();
452 return module_search_path;
455 char *
456 Py_GetPrefix(void)
458 if (!module_search_path)
459 calculate_path();
461 return prefix;
464 char *
465 Py_GetExecPrefix(void)
467 if (!module_search_path)
468 calculate_path();
470 return exec_prefix;
473 char *
474 Py_GetProgramFullPath(void)
476 if (!module_search_path)
477 calculate_path();
479 return progpath;