blob: f937df86bc8e80375bcbc9b607f6ef8489282d0a [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"
11#include <windows.h>
Guido van Rossum6dbd1901996-08-21 15:03:37 +000012#include "importdl.h"
Tim Petersdb638292000-07-03 23:51:17 +000013#include "malloc.h" /* for alloca */
Guido van Rossum271f9771997-09-29 23:39:31 +000014
Tim Petersdb638292000-07-03 23:51:17 +000015/* a string loaded from the DLL at startup */
16extern const char *PyWin_DLLVersionString;
Guido van Rossum6dbd1901996-08-21 15:03:37 +000017
Tim Petersdb638292000-07-03 23:51:17 +000018FILE *PyWin_FindRegisteredModule(const char *moduleName,
19 struct filedescr **ppFileDesc,
20 char *pathBuf,
21 int pathLen)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000022{
Guido van Rossum271f9771997-09-29 23:39:31 +000023 char *moduleKey;
Guido van Rossum271f9771997-09-29 23:39:31 +000024 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
25 const char keySuffix[] = "\\Modules\\";
Guido van Rossum1c096b71998-05-26 13:53:23 +000026#ifdef _DEBUG
Tim Petersdb638292000-07-03 23:51:17 +000027 /* In debugging builds, we _must_ have the debug version
28 * registered.
29 */
Guido van Rossum1c096b71998-05-26 13:53:23 +000030 const char debugString[] = "\\Debug";
31#else
32 const char debugString[] = "";
33#endif
Guido van Rossum6dbd1901996-08-21 15:03:37 +000034 struct filedescr *fdp = NULL;
35 FILE *fp;
Mark Hammond48b3eee2000-08-22 11:20:21 +000036 HKEY keyBase = HKEY_CURRENT_USER;
Guido van Rossum1c096b71998-05-26 13:53:23 +000037 int modNameSize;
Guido van Rossum76ec53c1998-06-15 18:01:34 +000038 long regStat;
Guido van Rossum271f9771997-09-29 23:39:31 +000039
Tim Petersdb638292000-07-03 23:51:17 +000040 /* Calculate the size for the sprintf buffer.
41 * Get the size of the chars only, plus 1 NULL.
42 */
43 size_t bufSize = sizeof(keyPrefix)-1 +
44 strlen(PyWin_DLLVersionString) +
45 sizeof(keySuffix) +
46 strlen(moduleName) +
47 sizeof(debugString) - 1;
48 /* alloca == no free required, but memory only local to fn,
49 * also no heap fragmentation!
50 */
Guido van Rossum1c096b71998-05-26 13:53:23 +000051 moduleKey = alloca(bufSize);
Barry Warsaw58ab0842001-11-28 21:03:37 +000052 PyOS_snprintf(moduleKey, bufSize,
53 "Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
54 PyWin_DLLVersionString, moduleName, debugString);
Guido van Rossum271f9771997-09-29 23:39:31 +000055
Guido van Rossum94555471999-01-14 22:40:30 +000056 modNameSize = pathLen;
Guido van Rossum76ec53c1998-06-15 18:01:34 +000057 regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
Mark Hammond48b3eee2000-08-22 11:20:21 +000058 if (regStat != ERROR_SUCCESS) {
59 /* No user setting - lookup in machine settings */
60 keyBase = HKEY_LOCAL_MACHINE;
61 /* be anal - failure may have reset size param */
62 modNameSize = pathLen;
63 regStat = RegQueryValue(keyBase, moduleKey,
64 pathBuf, &modNameSize);
65
66 if (regStat != ERROR_SUCCESS)
67 return NULL;
68 }
Tim Petersdb638292000-07-03 23:51:17 +000069 /* use the file extension to locate the type entry. */
Guido van Rossum22822d71997-05-08 23:43:52 +000070 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
Guido van Rossum1c44e282000-06-28 22:20:06 +000071 size_t extLen = strlen(fdp->suffix);
Tim Petersdb638292000-07-03 23:51:17 +000072 assert(modNameSize >= 0); /* else cast to size_t is wrong */
73 if ((size_t)modNameSize > extLen &&
74 strnicmp(pathBuf + ((size_t)modNameSize-extLen-1),
75 fdp->suffix,
76 extLen) == 0)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000077 break;
78 }
Tim Petersdb638292000-07-03 23:51:17 +000079 if (fdp->suffix == NULL)
Guido van Rossum6dbd1901996-08-21 15:03:37 +000080 return NULL;
81 fp = fopen(pathBuf, fdp->mode);
82 if (fp != NULL)
83 *ppFileDesc = fdp;
84 return fp;
85}