Rewrite to use test_support's fine fcmp instead -- I didn't know that
[python/dscho.git] / PC / import_nt.c
blob58cd9e4850647a7cb3b14d4627fa3ef4133e3918
1 /********************************************************************
3 import_nt.c
5 Win32 specific import code.
7 */
9 #include "Python.h"
10 #include "osdefs.h"
11 #include <windows.h>
12 #include "importdl.h"
13 #include "malloc.h" /* for alloca */
15 /* a string loaded from the DLL at startup */
16 extern const char *PyWin_DLLVersionString;
18 FILE *PyWin_FindRegisteredModule(const char *moduleName,
19 struct filedescr **ppFileDesc,
20 char *pathBuf,
21 int pathLen)
23 char *moduleKey;
24 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
25 const char keySuffix[] = "\\Modules\\";
26 #ifdef _DEBUG
27 /* In debugging builds, we _must_ have the debug version
28 * registered.
30 const char debugString[] = "\\Debug";
31 #else
32 const char debugString[] = "";
33 #endif
34 struct filedescr *fdp = NULL;
35 FILE *fp;
36 HKEY keyBase = HKEY_CURRENT_USER;
37 int modNameSize;
38 long regStat;
40 /* Calculate the size for the sprintf buffer.
41 * Get the size of the chars only, plus 1 NULL.
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!
51 moduleKey = alloca(bufSize);
52 sprintf(moduleKey,
53 "Software\\Python\\PythonCore\\%s\\Modules\\%s%s",
54 PyWin_DLLVersionString, moduleName, debugString);
56 modNameSize = pathLen;
57 regStat = RegQueryValue(keyBase, moduleKey, pathBuf, &modNameSize);
58 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);
66 if (regStat != ERROR_SUCCESS)
67 return NULL;
69 /* use the file extension to locate the type entry. */
70 for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) {
71 size_t extLen = strlen(fdp->suffix);
72 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)
77 break;
79 if (fdp->suffix == NULL)
80 return NULL;
81 fp = fopen(pathBuf, fdp->mode);
82 if (fp != NULL)
83 *ppFileDesc = fdp;
84 return fp;