annotate PC/getpathp.c @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents d5f47e1465d6
children 94665291ab1b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Return the initial module search path. */
8895
9b4bc3b8b9d4 This file's previous log message is bogus. The real change is that
Guido van Rossum <guido@python.org>
parents: 8892
diff changeset
3 /* Used by DOS, OS/2, Windows 3.1, Windows 95/98, Windows NT. */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
5 /* ----------------------------------------------------------------
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
6 PATH RULES FOR WINDOWS:
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
7 This describes how sys.path is formed on Windows. It describes the
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
8 functionality, not the implementation (ie, the order in which these
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
9 are actually fetched is different)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
10
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
11 * Python always adds an empty entry at the start, which corresponds
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
12 to the current directory.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
13
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
14 * If the PYTHONPATH env. var. exists, it's entries are added next.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
15
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
16 * We look in the registry for "application paths" - that is, sub-keys
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
17 under the main PythonPath registry key. These are added next (the
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
18 order of sub-key processing is undefined).
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
19 HKEY_CURRENT_USER is searched and added first.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
20 HKEY_LOCAL_MACHINE is searched and added next.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
21 (Note that all known installers only use HKLM, so HKCU is typically
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
22 empty)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
23
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
24 * We attempt to locate the "Python Home" - if the PYTHONHOME env var
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
25 is set, we believe it. Otherwise, we use the path of our host .EXE's
12775
8e2b35d386aa Patch from M.-A. Lemburg:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12116
diff changeset
26 to try and locate our "landmark" (lib\\os.py) and deduce our home.
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
27 - If we DO have a Python Home: The relevant sub-directories (Lib,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
28 plat-win, lib-tk, etc) are based on the Python Home
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
29 - If we DO NOT have a Python Home, the core Python Path is
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
30 loaded from the registry. This is the main PythonPath key,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
31 and both HKLM and HKCU are combined to form the path)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
32
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
33 * Iff - we can not locate the Python Home, have not had a PYTHONPATH
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
34 specified, and can't locate any Registry entries (ie, we have _nothing_
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
35 we can assume is a good path), a default path with relative entries is
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
36 used (eg. .\Lib;.\plat-win, etc)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
37
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
38
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
39 The end result of all this is:
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
40 * When running python.exe, or any other .exe in the main Python directory
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
41 (either an installed version, or directly from the PCbuild directory),
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
42 the core path is deduced, and the core paths in the registry are
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
43 ignored. Other "application paths" in the registry are always read.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
44
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
45 * When Python is hosted in another exe (different directory, embedded via
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
46 COM, etc), the Python Home will not be deduced, so the core path from
19795
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
47 the registry is used. Other "application paths" in the registry are
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
48 always read.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
49
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
50 * If Python can't find its home and there is no registry (eg, frozen
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
51 exe, some very strange installation setup) you get a path with
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
52 some default, but relative, paths.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
53
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
54 ---------------------------------------------------------------- */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
55
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
56
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 #include "Python.h"
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 #include "osdefs.h"
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
60 #ifdef MS_WINDOWS
5999
f2d134bf6973 Some changes to make it work on NT; add the directory where the binary
Guido van Rossum <guido@python.org>
parents: 5545
diff changeset
61 #include <windows.h>
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
62 #include <tchar.h>
5999
f2d134bf6973 Some changes to make it work on NT; add the directory where the binary
Guido van Rossum <guido@python.org>
parents: 5545
diff changeset
63 #endif
f2d134bf6973 Some changes to make it work on NT; add the directory where the binary
Guido van Rossum <guido@python.org>
parents: 5545
diff changeset
64
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 #include <sys/types.h>
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 #include <sys/stat.h>
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 #include <string.h>
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 /* Search in some common locations for the associated Python libraries.
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 *
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 * Py_GetPath() tries to return a sensible Python module search path.
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 *
7407
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
73 * The approach is an adaptation for Windows of the strategy used in
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
74 * ../Modules/getpath.c; it uses the Windows Registry as one of its
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
75 * information sources.
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 */
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 #ifndef LANDMARK
12775
8e2b35d386aa Patch from M.-A. Lemburg:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12116
diff changeset
79 #define LANDMARK "lib\\os.py"
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 #endif
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 static char prefix[MAXPATHLEN+1];
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 static char progpath[MAXPATHLEN+1];
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
84 static char dllpath[MAXPATHLEN+1];
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 static char *module_search_path = NULL;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
86
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
87
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
89 is_sep(char ch) /* determine if "ch" is a separator character */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #ifdef ALTSEP
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 return ch == SEP || ch == ALTSEP;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 #else
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 return ch == SEP;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 #endif
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
98 /* assumes 'dir' null terminated in bounds. Never writes
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
99 beyond existing terminator.
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
100 */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
102 reduce(char *dir)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 {
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
104 size_t i = strlen(dir);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 while (i > 0 && !is_sep(dir[i]))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 --i;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 dir[i] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
112 exists(char *filename)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 struct stat buf;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 return stat(filename, &buf) == 0;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
116 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
117
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
118 /* Assumes 'filename' MAXPATHLEN+1 bytes long -
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
119 may extend 'filename' by one character.
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
120 */
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
121 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
122 ismodule(char *filename) /* Is module -- check for .pyc/.pyo too */
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
123 {
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
124 if (exists(filename))
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
125 return 1;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
126
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
127 /* Check for the compiled version of prefix. */
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
128 if (strlen(filename) < MAXPATHLEN) {
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
129 strcat(filename, Py_OptimizeFlag ? "o" : "c");
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
130 if (exists(filename))
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
131 return 1;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
132 }
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
133 return 0;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
134 }
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
135
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
136 /* guarantees buffer will never overflow MAXPATHLEN+1 bytes */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
138 join(char *buffer, char *stuff)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 {
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
140 size_t n, k;
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 if (is_sep(stuff[0]))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 n = 0;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 else {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 n = strlen(buffer);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 buffer[n++] = SEP;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 k = strlen(stuff);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 if (n + k > MAXPATHLEN)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 k = MAXPATHLEN - n;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 strncpy(buffer+n, stuff, k);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 buffer[n+k] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
155 /* gotlandmark only called by search_for_prefix, which ensures
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
156 'prefix' is null terminated in bounds. join() ensures
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
157 'landmark' can not overflow prefix if too long.
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
158 */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
160 gotlandmark(char *landmark)
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
161 {
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
162 int n, ok;
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
163
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
164 n = strlen(prefix);
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
165 join(prefix, landmark);
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
166 ok = ismodule(prefix);
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
167 prefix[n] = '\0';
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
168 return ok;
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
169 }
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
170
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
171 /* assumes argv0_path is MAXPATHLEN+1 bytes long, already \0 term'd.
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
172 assumption provided by only caller, calculate_path() */
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
173 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
174 search_for_prefix(char *argv0_path, char *landmark)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 {
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
176 /* Search from argv0_path, until landmark is found */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 strcpy(prefix, argv0_path);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 do {
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
179 if (gotlandmark(landmark))
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 return 1;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 reduce(prefix);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 } while (prefix[0]);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 return 0;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
186 #ifdef MS_WINDOWS
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
187
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
188 /* a string loaded from the DLL at startup.*/
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
189 extern const char *PyWin_DLLVersionString;
6356
a857d2f87336 Seem to be some changes related to DLL version from string resource,
Guido van Rossum <guido@python.org>
parents: 6001
diff changeset
190
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
191
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
192 /* Load a PYTHONPATH value from the registry.
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
193 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
194
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
195 Works in both Unicode and 8bit environments. Only uses the
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
196 Ex family of functions so it also works with Windows CE.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
197
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
198 Returns NULL, or a pointer that should be freed.
16748
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
199
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
200 XXX - this code is pretty strange, as it used to also
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
201 work on Win16, where the buffer sizes werent available
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
202 in advance. It could be simplied now Win16/Win32s is dead!
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
203 */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
204
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
205 static char *
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
206 getpythonregpath(HKEY keyBase, int skipcore)
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
207 {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
208 HKEY newKey = 0;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
209 DWORD dataSize = 0;
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
210 DWORD numKeys = 0;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
211 LONG rc;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
212 char *retval = NULL;
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
213 TCHAR *dataBuf = NULL;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
214 static const TCHAR keyPrefix[] = _T("Software\\Python\\PythonCore\\");
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
215 static const TCHAR keySuffix[] = _T("\\PythonPath");
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
216 size_t versionLen;
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
217 DWORD index;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
218 TCHAR *keyBuf = NULL;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
219 TCHAR *keyBufPtr;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
220 TCHAR **ppPaths = NULL;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
221
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
222 /* Tried to use sysget("winver") but here is too early :-( */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
223 versionLen = _tcslen(PyWin_DLLVersionString);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
224 /* Space for all the chars, plus one \0 */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
225 keyBuf = keyBufPtr = malloc(sizeof(keyPrefix) +
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
226 sizeof(TCHAR)*(versionLen-1) +
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
227 sizeof(keySuffix));
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
228 if (keyBuf==NULL) goto done;
6356
a857d2f87336 Seem to be some changes related to DLL version from string resource,
Guido van Rossum <guido@python.org>
parents: 6001
diff changeset
229
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
230 memcpy(keyBufPtr, keyPrefix, sizeof(keyPrefix)-sizeof(TCHAR));
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
231 keyBufPtr += sizeof(keyPrefix)/sizeof(TCHAR) - 1;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
232 memcpy(keyBufPtr, PyWin_DLLVersionString, versionLen * sizeof(TCHAR));
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
233 keyBufPtr += versionLen;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
234 /* NULL comes with this one! */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
235 memcpy(keyBufPtr, keySuffix, sizeof(keySuffix));
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
236 /* Open the root Python key */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
237 rc=RegOpenKeyEx(keyBase,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
238 keyBuf, /* subkey */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
239 0, /* reserved */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
240 KEY_READ,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
241 &newKey);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
242 if (rc!=ERROR_SUCCESS) goto done;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
243 /* Find out how big our core buffer is, and how many subkeys we have */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
244 rc = RegQueryInfoKey(newKey, NULL, NULL, NULL, &numKeys, NULL, NULL,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
245 NULL, NULL, &dataSize, NULL, NULL);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
246 if (rc!=ERROR_SUCCESS) goto done;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
247 if (skipcore) dataSize = 0; /* Only count core ones if we want them! */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
248 /* Allocate a temp array of char buffers, so we only need to loop
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
249 reading the registry once
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
250 */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
251 ppPaths = malloc( sizeof(TCHAR *) * numKeys );
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
252 if (ppPaths==NULL) goto done;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
253 memset(ppPaths, 0, sizeof(TCHAR *) * numKeys);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
254 /* Loop over all subkeys, allocating a temp sub-buffer. */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
255 for(index=0;index<numKeys;index++) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
256 TCHAR keyBuf[MAX_PATH+1];
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
257 HKEY subKey = 0;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
258 DWORD reqdSize = MAX_PATH+1;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
259 /* Get the sub-key name */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
260 DWORD rc = RegEnumKeyEx(newKey, index, keyBuf, &reqdSize,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
261 NULL, NULL, NULL, NULL );
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
262 if (rc!=ERROR_SUCCESS) goto done;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
263 /* Open the sub-key */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
264 rc=RegOpenKeyEx(newKey,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
265 keyBuf, /* subkey */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
266 0, /* reserved */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
267 KEY_READ,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
268 &subKey);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
269 if (rc!=ERROR_SUCCESS) goto done;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
270 /* Find the value of the buffer size, malloc, then read it */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
271 RegQueryValueEx(subKey, NULL, 0, NULL, NULL, &reqdSize);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
272 if (reqdSize) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
273 ppPaths[index] = malloc(reqdSize);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
274 if (ppPaths[index]) {
14500
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
275 RegQueryValueEx(subKey, NULL, 0, NULL,
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
276 (LPBYTE)ppPaths[index],
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
277 &reqdSize);
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
278 dataSize += reqdSize + 1; /* 1 for the ";" */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
279 }
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
280 }
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
281 RegCloseKey(subKey);
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
282 }
16748
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
283 /* original datasize from RegQueryInfo doesn't include the \0 */
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
284 dataBuf = malloc((dataSize+1) * sizeof(TCHAR));
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
285 if (dataBuf) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
286 TCHAR *szCur = dataBuf;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
287 DWORD reqdSize = dataSize;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
288 /* Copy our collected strings */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
289 for (index=0;index<numKeys;index++) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
290 if (index > 0) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
291 *(szCur++) = _T(';');
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
292 dataSize--;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
293 }
14500
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
294 if (ppPaths[index]) {
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
295 int len = _tcslen(ppPaths[index]);
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
296 _tcsncpy(szCur, ppPaths[index], len);
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
297 szCur += len;
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
298 dataSize -= len;
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
299 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
300 }
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
301 if (skipcore)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
302 *szCur = '\0';
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
303 else {
16748
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
304 /* If we have no values, we dont need a ';' */
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
305 if (numKeys) {
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
306 *(szCur++) = _T(';');
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
307 dataSize--;
e444a5271181 Checkin updated version of patch #103933 . As Thomas says, fixes the bugs #131064, #129584, #127722. See the discussion in bug #131064
Mark Hammond <mhammond@skippinet.com.au>
parents: 15084
diff changeset
308 }
14500
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
309 /* Now append the core path entries -
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
310 this will include the NULL
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
311 */
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
312 rc = RegQueryValueEx(newKey, NULL, 0, NULL,
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
313 (LPBYTE)szCur, &dataSize);
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
314 }
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
315 /* And set the result - caller must free
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
316 If MBCS, it is fine as is. If Unicode, allocate new
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
317 buffer and convert.
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
318 */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
319 #ifdef UNICODE
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
320 retval = (char *)malloc(reqdSize+1);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
321 if (retval)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
322 WideCharToMultiByte(CP_ACP, 0,
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
323 dataBuf, -1, /* source */
27524
d5f47e1465d6 Fix [ 583477 ] wrong dest size.
Mark Hammond <mhammond@skippinet.com.au>
parents: 26942
diff changeset
324 retval, reqdSize+1, /* dest */
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
325 NULL, NULL);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
326 free(dataBuf);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
327 #else
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
328 retval = dataBuf;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
329 #endif
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
330 }
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
331 done:
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
332 /* Loop freeing my temp buffers */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
333 if (ppPaths) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
334 for(index=0;index<numKeys;index++)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
335 if (ppPaths[index]) free(ppPaths[index]);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
336 free(ppPaths);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
337 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
338 if (newKey)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
339 RegCloseKey(newKey);
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
340 if (keyBuf)
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
341 free(keyBuf);
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
342 return retval;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
343 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
344 #endif /* MS_WINDOWS */
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
345
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
347 get_progpath(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 {
13668
d7d81ea9c82d ANSIfication: remove very-old-varargs code, fix function declarations so
Thomas Wouters <thomas@python.org>
parents: 13660
diff changeset
349 extern char *Py_GetProgramName(void);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 char *path = getenv("PATH");
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 char *prog = Py_GetProgramName();
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
352
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
353 #ifdef MS_WINDOWS
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
354 extern HANDLE PyWin_DLLhModule;
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
355 #ifdef UNICODE
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
356 WCHAR wprogpath[MAXPATHLEN+1];
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
357 /* Windows documents that GetModuleFileName() will "truncate",
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
358 but makes no mention of the null terminator. Play it safe.
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
359 PLUS Windows itself defines MAX_PATH as the same, but anyway...
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
360 */
24353
fda513b671b6 Remove a strange single quote that didn't seem to upset the compilers!
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
361 wprogpath[MAXPATHLEN]=_T('\0');
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
362 if (PyWin_DLLhModule &&
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
363 GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
364 WideCharToMultiByte(CP_ACP, 0,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
365 wprogpath, -1,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
366 dllpath, MAXPATHLEN+1,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
367 NULL, NULL);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
368 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
369 wprogpath[MAXPATHLEN]=_T('\0')';
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
370 if (GetModuleFileName(NULL, wprogpath, MAXPATHLEN)) {
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
371 WideCharToMultiByte(CP_ACP, 0,
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
372 wprogpath, -1,
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
373 progpath, MAXPATHLEN+1,
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
374 NULL, NULL);
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
375 return;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
376 }
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
377 #else
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
378 /* static init of progpath ensures final char remains \0 */
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
379 if (PyWin_DLLhModule)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
380 if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
381 dllpath[0] = 0;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
382 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
383 return;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
384 #endif
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
385 #endif
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
386 if (prog == NULL || *prog == '\0')
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
387 prog = "python";
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
388
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 /* If there is no slash in the argv0 path, then we have to
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 * assume python is on the user's $PATH, since there's no
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 * other way to find a directory to start the search from. If
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 * $PATH isn't exported, you lose.
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 */
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 #ifdef ALTSEP
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 #else
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 if (strchr(prog, SEP))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 #endif
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
399 strncpy(progpath, prog, MAXPATHLEN);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 else if (path) {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 while (1) {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 char *delim = strchr(path, DELIM);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
403
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 if (delim) {
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
405 size_t len = delim - path;
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
406 /* ensure we can't overwrite buffer */
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
407 len = min(MAXPATHLEN,len);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 strncpy(progpath, path, len);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 *(progpath + len) = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 else
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
412 strncpy(progpath, path, MAXPATHLEN);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
413
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
414 /* join() is safe for MAXPATHLEN+1 size buffer */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 join(progpath, prog);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 if (exists(progpath))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 break;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
418
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 if (!delim) {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 progpath[0] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 break;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 path = delim + 1;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 else
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 progpath[0] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
431 calculate_path(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 char argv0_path[MAXPATHLEN+1];
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 char *buf;
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
435 size_t bufsz;
8758
68fe63750f84 Two changes:
Guido van Rossum <guido@python.org>
parents: 7407
diff changeset
436 char *pythonhome = Py_GetPythonHome();
18808
a8b1acc7a21f Add -E command line switch (ignore environment variables like PYTHONHOME
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16748
diff changeset
437 char *envpath = Py_GETENV("PYTHONPATH");
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
438
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
439 #ifdef MS_WINDOWS
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
440 int skiphome, skipdefault;
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
441 char *machinepath = NULL;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
442 char *userpath = NULL;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
443 char zip_path[MAXPATHLEN+1];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
444 size_t len;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
445 #endif
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 get_progpath();
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
448 /* progpath guaranteed \0 terminated in MAXPATH+1 bytes. */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 strcpy(argv0_path, progpath);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 reduce(argv0_path);
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
451 if (pythonhome == NULL || *pythonhome == '\0') {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
452 if (search_for_prefix(argv0_path, LANDMARK))
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
453 pythonhome = prefix;
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
454 else
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
455 pythonhome = NULL;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
456 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
457 else
15084
9be67c5a823a Prevent possible buffer overflow exploits under Windows. As per (the very quick) patch Patch #101801.
Mark Hammond <mhammond@skippinet.com.au>
parents: 14500
diff changeset
458 strncpy(prefix, pythonhome, MAXPATHLEN);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
460 if (envpath && *envpath == '\0')
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
461 envpath = NULL;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
462
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
463
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
464 #ifdef MS_WINDOWS
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
465 /* Calculate zip archive path */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
466 if (dllpath[0]) /* use name of python DLL */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
467 strncpy(zip_path, dllpath, MAXPATHLEN);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
468 else /* use name of executable program */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
469 strncpy(zip_path, progpath, MAXPATHLEN);
26942
723721ea4025 Make sure zip_path is null-terminated, since it's on the stack
Neal Norwitz <nnorwitz@gmail.com>
parents: 26885
diff changeset
470 zip_path[MAXPATHLEN] = '\0';
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
471 len = strlen(zip_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
472 if (len > 4) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
473 zip_path[len-3] = 'z'; /* change ending to "zip" */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
474 zip_path[len-2] = 'i';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
475 zip_path[len-1] = 'p';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
476 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
477 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
478 zip_path[0] = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
479 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
480
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
481 skiphome = pythonhome==NULL ? 0 : 1;
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
482 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, skiphome);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
483 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
484 /* We only use the default relative PYTHONPATH if we havent
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
485 anything better to use! */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
486 skipdefault = envpath!=NULL || pythonhome!=NULL || \
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
487 machinepath!=NULL || userpath!=NULL;
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
488 #endif
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
489
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
490 /* We need to construct a path from the following parts.
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
491 (1) the PYTHONPATH environment variable, if set;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
492 (2) for Win32, the zip archive file path;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
493 (3) for Win32, the machinepath and userpath, if set;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
494 (4) the PYTHONPATH config macro, with the leading "."
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
495 of each component replaced with pythonhome, if set;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
496 (5) the directory containing the executable (argv0_path).
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
497 The length calculation calculates #4 first.
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
498 Extra rules:
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
499 - If PYTHONHOME is set (in any way) item (3) is ignored.
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
500 - If registry values are used, (4) and (5) are ignored.
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
501 */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
502
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
503 /* Calculate size of return buffer */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
504 if (pythonhome != NULL) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
505 char *p;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
506 bufsz = 1;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
507 for (p = PYTHONPATH; *p; p++) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
508 if (*p == DELIM)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
509 bufsz++; /* number of DELIM plus one */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
510 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
511 bufsz *= strlen(pythonhome);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
513 else
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
514 bufsz = 0;
6826
a1545cbf0adf Allocate one byte extra in some cases.
Guido van Rossum <guido@python.org>
parents: 6356
diff changeset
515 bufsz += strlen(PYTHONPATH) + 1;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
516 bufsz += strlen(argv0_path) + 1;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
517 #ifdef MS_WINDOWS
8892
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
518 if (userpath)
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
519 bufsz += strlen(userpath) + 1;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
520 if (machinepath)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
521 bufsz += strlen(machinepath) + 1;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
522 bufsz += strlen(zip_path) + 1;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
523 #endif
8892
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
524 if (envpath != NULL)
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
525 bufsz += strlen(envpath) + 1;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
526
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
527 module_search_path = buf = malloc(bufsz);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
528 if (buf == NULL) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
529 /* We can't exit, so print a warning and limp along */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
530 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
531 if (envpath) {
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
532 fprintf(stderr, "Using environment $PYTHONPATH.\n");
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
533 module_search_path = envpath;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
534 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
535 else {
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
536 fprintf(stderr, "Using default static path.\n");
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
537 module_search_path = PYTHONPATH;
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
539 #ifdef MS_WINDOWS
7407
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
540 if (machinepath)
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
541 free(machinepath);
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
542 if (userpath)
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
543 free(userpath);
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
544 #endif /* MS_WINDOWS */
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
545 return;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
546 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
547
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
548 if (envpath) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
549 strcpy(buf, envpath);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
550 buf = strchr(buf, '\0');
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
551 *buf++ = DELIM;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
552 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
553 #ifdef MS_WINDOWS
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
554 if (zip_path[0]) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
555 strcpy(buf, zip_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
556 buf = strchr(buf, '\0');
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
557 *buf++ = DELIM;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
558 }
8892
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
559 if (userpath) {
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
560 strcpy(buf, userpath);
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
561 buf = strchr(buf, '\0');
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
562 *buf++ = DELIM;
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
563 free(userpath);
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
564 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
565 if (machinepath) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
566 strcpy(buf, machinepath);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
567 buf = strchr(buf, '\0');
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
568 *buf++ = DELIM;
7407
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
569 free(machinepath);
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
570 }
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
571 if (pythonhome == NULL) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
572 if (!skipdefault) {
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
573 strcpy(buf, PYTHONPATH);
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
574 buf = strchr(buf, '\0');
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
575 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
576 }
12116
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
577 #else
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
578 if (pythonhome == NULL) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
579 strcpy(buf, PYTHONPATH);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
580 buf = strchr(buf, '\0');
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
581 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
582 #endif /* MS_WINDOWS */
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
583 else {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
584 char *p = PYTHONPATH;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
585 char *q;
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
586 size_t n;
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
587 for (;;) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
588 q = strchr(p, DELIM);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
589 if (q == NULL)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
590 n = strlen(p);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
591 else
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
592 n = q-p;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
593 if (p[0] == '.' && is_sep(p[1])) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
594 strcpy(buf, pythonhome);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
595 buf = strchr(buf, '\0');
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
596 p++;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
597 n--;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
598 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
599 strncpy(buf, p, n);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
600 buf += n;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
601 if (q == NULL)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
602 break;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
603 *buf++ = DELIM;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
604 p = q+1;
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
607 if (argv0_path) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
608 *buf++ = DELIM;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
609 strcpy(buf, argv0_path);
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
610 buf = strchr(buf, '\0');
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
611 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 *buf = '\0';
19795
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
613 /* Now to pull one last hack/trick. If sys.prefix is
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
614 empty, then try and find it somewhere on the paths
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
615 we calculated. We scan backwards, as our general policy
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
616 is that Python core directories are at the *end* of
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
617 sys.path. We assume that our "lib" directory is
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
618 on the path, and that our 'prefix' directory is
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
619 the parent of that.
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
620 */
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
621 if (*prefix=='\0') {
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
622 char lookBuf[MAXPATHLEN+1];
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
623 char *look = buf - 1; /* 'buf' is at the end of the buffer */
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
624 while (1) {
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
625 int nchars;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
626 char *lookEnd = look;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
627 /* 'look' will end up one character before the
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
628 start of the path in question - even if this
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
629 is one character before the start of the buffer
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
630 */
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
631 while (*look != DELIM && look >= module_search_path)
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
632 look--;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
633 nchars = lookEnd-look;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
634 strncpy(lookBuf, look+1, nchars);
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
635 lookBuf[nchars] = '\0';
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
636 /* Up one level to the parent */
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
637 reduce(lookBuf);
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
638 if (search_for_prefix(lookBuf, LANDMARK)) {
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
639 break;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
640 }
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
641 /* If we are out of paths to search - give up */
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
642 if (look < module_search_path)
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
643 break;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
644 look--;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
645 }
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
646 }
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
647 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
648
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
649
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 /* External interface */
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
651
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
653 Py_GetPath(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
654 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
655 if (!module_search_path)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 calculate_path();
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 return module_search_path;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
659
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
661 Py_GetPrefix(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 {
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
663 if (!module_search_path)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
664 calculate_path();
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
665 return prefix;
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
667
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
669 Py_GetExecPrefix(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 {
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
671 return Py_GetPrefix();
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
673
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
675 Py_GetProgramFullPath(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 if (!module_search_path)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 calculate_path();
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 return progpath;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 }