blob: 573106fd3ff2f390dcaa56fab41b7b37a0d38831 [file] [log] [blame]
Guido van Rossum6dbd1901996-08-21 15:03:37 +00001/********************************************************************
2
Guido van Rossumeaf9b6c1996-08-22 00:06:59 +00003 import_nt.c
Guido van Rossum6dbd1901996-08-21 15:03:37 +00004
5 Win32 specific import code.
6
7*/
8
Guido van Rossum7688bba1997-05-05 21:45:44 +00009#include "Python.h"
Guido van Rossum6dbd1901996-08-21 15:03:37 +000010#include "osdefs.h"
Tim Petersdb638292000-07-03 23:51:17 +000011#include <assert.h>
Guido van Rossum6dbd1901996-08-21 15:03:37 +000012#include <windows.h>
Guido van Rossum6dbd1901996-08-21 15:03:37 +000013#include "importdl.h"
Tim Petersdb638292000-07-03 23:51:17 +000014#include "malloc.h" /* for alloca */
Guido van Rossum271f9771997-09-29 23:39:31 +000015
Tim Petersdb638292000-07-03 23:51:17 +000016/* a string loaded from the DLL at startup */
17extern const char *PyWin_DLLVersionString;
Guido van Rossum6dbd1901996-08-21 15:03:37 +000018
Tim Petersdb638292000-07-03 23:51:17 +000019FILE *PyWin_FindRegisteredModule(const char *moduleName,
20 struct filedescr **ppFileDesc,
21 char *pathBuf,
22 int pathLen)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000023{
Guido van Rossum271f9771997-09-29 23:39:31 +000024 char *moduleKey;
Guido van Rossum271f9771997-09-29 23:39:31 +000025 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
26 const char keySuffix[] = "\\Modules\\";
Guido van Rossum1c096b71998-05-26 13:53:23 +000027#ifdef _DEBUG
Tim Petersdb638292000-07-03 23:51:17 +000028 /* In debugging builds, we _must_ have the debug version
29 * registered.
30 */
Guido van Rossum1c096b71998-05-26 13:53:23 +000031 const char debugString[] = "\\Debug";
32#else
33 const char debugString[] = "";
34#endif
Guido van Rossum6dbd1901996-08-21 15:03:37 +000035 struct filedescr *fdp = NULL;
36 FILE *fp;
Mark Hammond48b3eee2000-08-22 11:20:21 +000037 HKEY keyBase = HKEY_CURRENT_USER;
Guido van Rossum1c096b71998-05-26 13:53:23 +000038 int modNameSize;
Guido van Rossum76ec53c1998-06-15 18:01:34 +000039 long regStat;
Guido van Rossum271f9771997-09-29 23:39:31 +000040
Tim Petersdb638292000-07-03 23:51:17 +000041 /* Calculate the size for the sprintf buffer.
42 * Get the size of the chars only, plus 1 NULL.
43 */
44 size_t bufSize = sizeof(keyPrefix)-1 +
45 strlen(PyWin_DLLVersionString) +
46 sizeof(keySuffix) +
47 strlen(moduleName) +
48 sizeof(debugString) - 1;
49 /* alloca == no free required, but memory only local to fn,
50 * also no heap fragmentation!
51 */
Guido van Rossum1c096b71998-05-26 13:53:23 +000052 moduleKey = alloca(bufSize);
Tim Petersdb638292000-07-03 23:51:17 +000053 sprintf(moduleKey,
54 "Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
55 PyWin_DLLVersionString, moduleName, debugString);
Guido van Rossum271f9771997-09-29 23:39:31 +000056
Guido van Rossum94555471999-01-14 22:40:30 +000057 modNameSize = pathLen;
Guido van Rossum76ec53c1998-06-15 18:01:34 +000058 regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
Mark Hammond48b3eee2000-08-22 11:20:21 +000059 if (regStat != ERROR_SUCCESS) {
60 /* No user setting - lookup in machine settings */
61 keyBase = HKEY_LOCAL_MACHINE;
62 /* be anal - failure may have reset size param */
63 modNameSize = pathLen;
64 regStat = RegQueryValue(keyBase, moduleKey,
65 pathBuf, &modNameSize);
66
67 if (regStat != ERROR_SUCCESS)
68 return NULL;
69 }
Tim Petersdb638292000-07-03 23:51:17 +000070 /* use the file extension to locate the type entry. */
Guido van Rossum22822d71997-05-08 23:43:52 +000071 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1c44e282000-06-28 22:20:06 +000072 size_t extLen = strlen(fdp->suffix);
Tim Petersdb638292000-07-03 23:51:17 +000073 assert(modNameSize >= 0); /* else cast to size_t is wrong */
74 if ((size_t)modNameSize > extLen &&
75 strnicmp(pathBuf + ((size_t)modNameSize-extLen-1),
76 fdp->suffix,
77 extLen) == 0)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000078 break;
79 }
Tim Petersdb638292000-07-03 23:51:17 +000080 if (fdp->suffix == NULL)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000081 return NULL;
82 fp = fopen(pathBuf, fdp->mode);
83 if (fp != NULL)
84 *ppFileDesc = fdp;
85 return fp;
86}