annotate PC/getpathp.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 2adf9ed6ba7e
children fd25030223b0
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
35276
4adbd331d335 Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents: 33017
diff changeset
14 * If the PYTHONPATH env. var. exists, its entries are added next.
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
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
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36507
diff changeset
65 #ifdef HAVE_SYS_TYPES_H
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 #include <sys/types.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36507
diff changeset
67 #endif /* HAVE_SYS_TYPES_H */
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36507
diff changeset
68
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36507
diff changeset
69 #ifdef HAVE_SYS_STAT_H
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 #include <sys/stat.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36507
diff changeset
71 #endif /* HAVE_SYS_STAT_H */
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36507
diff changeset
72
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 #include <string.h>
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 /* 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
76 *
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 * 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
78 *
7407
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
79 * 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
80 * ../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
81 * information sources.
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 */
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 #ifndef LANDMARK
12775
8e2b35d386aa Patch from M.-A. Lemburg:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12116
diff changeset
85 #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
86 #endif
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 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
89 static char progpath[MAXPATHLEN+1];
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
90 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
91 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
92
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
93
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
95 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
96 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 #ifdef ALTSEP
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 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
99 #else
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return ch == SEP;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 #endif
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103
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
104 /* 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
105 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
106 */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
108 reduce(char *dir)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 {
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
110 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
111 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
112 --i;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 dir[i] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
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 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
118 exists(char *filename)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 struct stat buf;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 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
122 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
123
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
124 /* 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
125 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
126 */
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
127 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
128 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
129 {
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 /* Check for the compiled version of prefix. */
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
134 if (strlen(filename) < MAXPATHLEN) {
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
135 strcat(filename, Py_OptimizeFlag ? "o" : "c");
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
136 if (exists(filename))
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
137 return 1;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
138 }
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
139 return 0;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
140 }
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
141
33017
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
142 /* Add a path component, by appending stuff to buffer.
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
143 buffer must have at least MAXPATHLEN + 1 bytes allocated, and contain a
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
144 NUL-terminated string with no more than MAXPATHLEN characters (not counting
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
145 the trailing NUL). It's a fatal error if it contains a string longer than
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
146 that (callers must be careful!). If these requirements are met, it's
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
147 guaranteed that buffer will still be a NUL-terminated string with no more
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
148 than MAXPATHLEN characters at exit. If stuff is too long, only as much of
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
149 stuff as fits will be appended.
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
150 */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
152 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
153 {
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
154 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
155 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
156 n = 0;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 else {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 n = strlen(buffer);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 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
160 buffer[n++] = SEP;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 }
33017
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
162 if (n > MAXPATHLEN)
ab93aff4c90a SF bug 1003471: Python 1.5.2 security vulnerability
Tim Peters <tim.peters@gmail.com>
parents: 30180
diff changeset
163 Py_FatalError("buffer overflow in getpathp.c's joinpath()");
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 k = strlen(stuff);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 if (n + k > MAXPATHLEN)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 k = MAXPATHLEN - n;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 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
168 buffer[n+k] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
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 /* 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
172 '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
173 '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
174 */
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
176 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
177 {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35276
diff changeset
178 int ok;
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35276
diff changeset
179 Py_ssize_t n;
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
180
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
181 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
182 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
183 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
184 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
185 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
186 }
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
187
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
188 /* 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
189 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
190 static int
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
191 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
192 {
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
193 /* 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
194 strcpy(prefix, argv0_path);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 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
196 if (gotlandmark(landmark))
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 return 1;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 reduce(prefix);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 } while (prefix[0]);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 return 0;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
203 #ifdef MS_WINDOWS
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
204 #ifdef Py_ENABLE_SHARED
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
205
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 /* 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
207 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
208
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
209
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
210 /* 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
211 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
212
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 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
214 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
215
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
216 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
217
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
218 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
219 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
220 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
221 */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
222
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
223 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
224 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
225 {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
226 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
227 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
228 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
229 LONG rc;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
230 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
231 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
232 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
233 static const TCHAR keySuffix[] = _T("\\PythonPath");
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
234 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
235 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
236 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
237 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
238 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
239
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 /* 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
241 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
242 /* 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
243 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
244 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
245 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
246 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
247
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
248 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
249 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
250 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
251 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
252 /* 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
253 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
254 /* 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
255 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
256 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
257 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
258 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
259 &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
260 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
261 /* 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
262 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
263 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
264 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
265 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
266 /* 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
267 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
268 */
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 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
270 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
271 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
272 /* 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
273 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
274 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
275 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
276 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
277 /* 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
278 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
279 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
280 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
281 /* 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
282 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
283 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
284 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
285 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
286 &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
287 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
288 /* 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
289 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
290 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
291 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
292 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
293 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
294 (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
295 &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
296 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
297 }
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
298 }
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
299 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
300 }
39158
a4fc4e752634 Patch #1232023: Don't include empty path component from registry,
Martin v. Löwis <martin@v.loewis.de>
parents: 38716
diff changeset
301
a4fc4e752634 Patch #1232023: Don't include empty path component from registry,
Martin v. Löwis <martin@v.loewis.de>
parents: 38716
diff changeset
302 /* return null if no path to return */
a4fc4e752634 Patch #1232023: Don't include empty path component from registry,
Martin v. Löwis <martin@v.loewis.de>
parents: 38716
diff changeset
303 if (dataSize == 0) goto done;
a4fc4e752634 Patch #1232023: Don't include empty path component from registry,
Martin v. Löwis <martin@v.loewis.de>
parents: 38716
diff changeset
304
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
305 /* 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
306 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
307 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
308 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
309 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
310 /* 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
311 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
312 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
313 *(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
314 dataSize--;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
315 }
14500
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
316 if (ppPaths[index]) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35276
diff changeset
317 Py_ssize_t len = _tcslen(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
318 _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
319 szCur += len;
36507
767adc098416 getpythonregpath(): Squash compiler warning about
Tim Peters <tim.peters@gmail.com>
parents: 36501
diff changeset
320 assert(dataSize > (DWORD)len);
767adc098416 getpythonregpath(): Squash compiler warning about
Tim Peters <tim.peters@gmail.com>
parents: 36501
diff changeset
321 dataSize -= (DWORD)len;
14500
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
322 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
323 }
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
324 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
325 *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
326 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
327 /* 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
328 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
329 *(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
330 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
331 }
14500
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
332 /* 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
333 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
334 */
21d847653f54 Patch for [ Bug #113828 ] getpythonregpath with null data in registry key
Mark Hammond <mhammond@skippinet.com.au>
parents: 14375
diff changeset
335 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
336 (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
337 }
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
338 /* 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
339 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
340 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
341 */
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
342 #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
343 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
344 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
345 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
346 dataBuf, -1, /* source */
27524
d5f47e1465d6 Fix [ 583477 ] wrong dest size.
Mark Hammond <mhammond@skippinet.com.au>
parents: 26942
diff changeset
347 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
348 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
349 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
350 #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
351 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
352 #endif
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
353 }
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
354 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
355 /* 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
356 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
357 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
358 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
359 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
360 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
361 if (newKey)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
362 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
363 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
364 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
365 return retval;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
366 }
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
367 #endif /* Py_ENABLE_SHARED */
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
368 #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
369
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
371 get_progpath(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 {
13668
d7d81ea9c82d ANSIfication: remove very-old-varargs code, fix function declarations so
Thomas Wouters <thomas@python.org>
parents: 13660
diff changeset
373 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
374 char *path = getenv("PATH");
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 char *prog = Py_GetProgramName();
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
376
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
377 #ifdef MS_WINDOWS
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
378 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
379 #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
380 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
381 /* 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
382 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
383 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
384 */
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
385 #ifdef Py_ENABLE_SHARED
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
386 wprogpath[MAXPATHLEN]=_T('\0');
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
387 if (PyWin_DLLhModule &&
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
388 GetModuleFileName(PyWin_DLLhModule, wprogpath, MAXPATHLEN)) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
389 WideCharToMultiByte(CP_ACP, 0,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
390 wprogpath, -1,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
391 dllpath, MAXPATHLEN+1,
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
392 NULL, NULL);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
393 }
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
394 #else
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
395 dllpath[0] = 0;
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
396 #endif
30180
94665291ab1b Remove a stray single quote.
Thomas Heller <theller@ctypes.org>
parents: 27524
diff changeset
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 }
e6ab3c603ff9 Revamped path access again, by Mark Hammond, to be more robust in the
Guido van Rossum <guido@python.org>
parents: 12090
diff changeset
405 #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
406 /* static init of progpath ensures final char remains \0 */
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
407 #ifdef Py_ENABLE_SHARED
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
408 if (PyWin_DLLhModule)
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
409 if (!GetModuleFileName(PyWin_DLLhModule, dllpath, MAXPATHLEN))
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
410 dllpath[0] = 0;
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
411 #else
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
412 dllpath[0] = 0;
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
413 #endif
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
414 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
415 return;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
416 #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
417 #endif
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
418 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
419 prog = "python";
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
420
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 /* 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
422 * 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
423 * 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
424 * $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
425 */
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 #ifdef ALTSEP
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 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
428 #else
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 if (strchr(prog, SEP))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 #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
431 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
432 else if (path) {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 while (1) {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 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
435
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 if (delim) {
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
437 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
438 /* 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
439 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
440 strncpy(progpath, path, len);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 *(progpath + len) = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 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
444 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
445
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
446 /* 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
447 join(progpath, prog);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 if (exists(progpath))
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 break;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
450
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 if (!delim) {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 progpath[0] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 break;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 path = delim + 1;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 else
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 progpath[0] = '\0';
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
461
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 static void
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
463 calculate_path(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 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
466 char *buf;
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
467 size_t bufsz;
8758
68fe63750f84 Two changes:
Guido van Rossum <guido@python.org>
parents: 7407
diff changeset
468 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
469 char *envpath = Py_GETENV("PYTHONPATH");
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
470
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
471 #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
472 int skiphome, skipdefault;
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
473 char *machinepath = NULL;
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
474 char *userpath = NULL;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
475 char zip_path[MAXPATHLEN+1];
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
476 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
477 #endif
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
478
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 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
480 /* 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
481 strcpy(argv0_path, progpath);
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 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
483 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
484 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
485 pythonhome = prefix;
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
486 else
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
487 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
488 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
489 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
490 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
491
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
492 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
493 envpath = NULL;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
494
12090
9877e978170e Changes to completely ignore the registry when it detects that you are
Guido van Rossum <guido@python.org>
parents: 8967
diff changeset
495
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
496 #ifdef MS_WINDOWS
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
497 /* Calculate zip archive path */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
498 if (dllpath[0]) /* use name of python DLL */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
499 strncpy(zip_path, dllpath, MAXPATHLEN);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
500 else /* use name of executable program */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
501 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
502 zip_path[MAXPATHLEN] = '\0';
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
503 len = strlen(zip_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
504 if (len > 4) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
505 zip_path[len-3] = 'z'; /* change ending to "zip" */
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
506 zip_path[len-2] = 'i';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
507 zip_path[len-1] = 'p';
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
508 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
509 else {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
510 zip_path[0] = 0;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
511 }
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
512
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
513 skiphome = pythonhome==NULL ? 0 : 1;
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
514 #ifdef Py_ENABLE_SHARED
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
515 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
516 userpath = getpythonregpath(HKEY_CURRENT_USER, skiphome);
51262
2adf9ed6ba7e Merged revisions 69205 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 41460
diff changeset
517 #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
518 /* 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
519 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
520 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
521 machinepath!=NULL || userpath!=NULL;
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
522 #endif
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
523
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
524 /* 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
525 (1) the PYTHONPATH environment variable, if set;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
526 (2) for Win32, the zip archive file path;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
527 (3) for Win32, the machinepath and userpath, if set;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
528 (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
529 of each component replaced with pythonhome, if set;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
530 (5) the directory containing the executable (argv0_path).
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
531 The length calculation calculates #4 first.
8902
6f73ae1cf934 Sigh. More restructuring was needed.
Guido van Rossum <guido@python.org>
parents: 8895
diff changeset
532 Extra rules:
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
533 - 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
534 - 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
535 */
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
536
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
537 /* 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
538 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
539 char *p;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
540 bufsz = 1;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
541 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
542 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
543 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
544 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
545 bufsz *= strlen(pythonhome);
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
547 else
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
548 bufsz = 0;
6826
a1545cbf0adf Allocate one byte extra in some cases.
Guido van Rossum <guido@python.org>
parents: 6356
diff changeset
549 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
550 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
551 #ifdef MS_WINDOWS
8892
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
552 if (userpath)
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
553 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
554 if (machinepath)
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
555 bufsz += strlen(machinepath) + 1;
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
556 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
557 #endif
8892
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
558 if (envpath != NULL)
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
559 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
560
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
561 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
562 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
563 /* 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
564 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
565 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
566 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
567 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
568 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
569 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
570 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
571 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
572 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
573 #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
574 if (machinepath)
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
575 free(machinepath);
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
576 if (userpath)
2c54901471f5 Fix some outdated comments (mostly by removing a large comment block
Guido van Rossum <guido@python.org>
parents: 6826
diff changeset
577 free(userpath);
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
578 #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
579 return;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
580 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
581
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
582 if (envpath) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
583 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
584 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
585 *buf++ = DELIM;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
586 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
587 #ifdef MS_WINDOWS
26885
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
588 if (zip_path[0]) {
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
589 strcpy(buf, zip_path);
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
590 buf = strchr(buf, '\0');
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
591 *buf++ = DELIM;
54aacb87beda PEP 302 + zipimport:
Just van Rossum <just@letterror.com>
parents: 24353
diff changeset
592 }
8892
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
593 if (userpath) {
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
594 strcpy(buf, userpath);
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
595 buf = strchr(buf, '\0');
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
596 *buf++ = DELIM;
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
597 free(userpath);
a7083295eb38 Different version games:
Guido van Rossum <guido@python.org>
parents: 8758
diff changeset
598 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
599 if (machinepath) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
600 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
601 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
602 *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
603 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
604 }
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
605 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
606 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
607 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
608 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
609 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
610 }
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
611 #else
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
612 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
613 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
614 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
615 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
616 #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
617 else {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
618 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
619 char *q;
13101
3cea73a2bbd8 Trent Mick: familiar simple Win64 patches
Guido van Rossum <guido@python.org>
parents: 12775
diff changeset
620 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
621 for (;;) {
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
622 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
623 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
624 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
625 else
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
626 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
627 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
628 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
629 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
630 p++;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
631 n--;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
632 }
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
633 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
634 buf += n;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
635 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
636 break;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
637 *buf++ = DELIM;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
638 p = q+1;
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 }
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
641 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
642 *buf++ = DELIM;
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
643 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
644 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
645 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 *buf = '\0';
19795
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
647 /* 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
648 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
649 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
650 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
651 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
652 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
653 the parent of that.
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
654 */
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
655 if (*prefix=='\0') {
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
656 char lookBuf[MAXPATHLEN+1];
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
657 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
658 while (1) {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 35276
diff changeset
659 Py_ssize_t nchars;
19795
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
660 char *lookEnd = look;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
661 /* '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
662 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
663 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
664 */
41460
ea3ca495f321 Fix potential crash in path manipulation on windows
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 39158
diff changeset
665 while (look >= module_search_path && *look != DELIM)
19795
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
666 look--;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
667 nchars = lookEnd-look;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
668 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
669 lookBuf[nchars] = '\0';
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
670 /* 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
671 reduce(lookBuf);
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
672 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
673 break;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
674 }
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
675 /* 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
676 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
677 break;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
678 look--;
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
679 }
9c54a16ac8b9 Fix bug #410274 - sys.prefix isn't always set.
Mark Hammond <mhammond@skippinet.com.au>
parents: 18808
diff changeset
680 }
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
681 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
682
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
683
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
684 /* External interface */
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
685
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
687 Py_GetPath(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 if (!module_search_path)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 calculate_path();
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 return module_search_path;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
693
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
694 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
695 Py_GetPrefix(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 {
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
697 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
698 calculate_path();
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
699 return prefix;
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
701
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
703 Py_GetExecPrefix(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 {
6001
8056169ec448 Much rewritten. Added Win32 registry stuff (from getpath_nt.c, which
Guido van Rossum <guido@python.org>
parents: 5999
diff changeset
705 return Py_GetPrefix();
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 }
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
707
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 char *
13660
a1ce07218781 Miscelaneous ANSIfications. I'm assuming here 'main' should take (int,
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
709 Py_GetProgramFullPath(void)
5545
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 {
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 if (!module_search_path)
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 calculate_path();
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 return progpath;
7e4228780ff8 Jim Ahlstrom's contributions for DOS, OS/2, WIN3.1.
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 }