summarylogtreecommitdiffstats
path: root/0001-restore-DllMain-symbol.patch
blob: c25878be136ea70f16d4e62d14e5202312c0f9f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--- a/gettext-runtime/intl/compat.c
+++ b/gettext-runtime/intl/compat.c
@@ -23,3 +23,46 @@
 libintl_set_relocation_prefix (const char *orig_prefix, const char *curr_prefix)
 {
 }
+
+#if (defined _WIN32 && defined DLL_EXPORT)
+#include <windows.h>
+
+# define HAS_DEVICE(P) \
+    ((((P)[0] >= 'A' && (P)[0] <= 'Z') || ((P)[0] >= 'a' && (P)[0] <= 'z')) \
+     && (P)[1] == ':')
+#define IS_FILE_NAME_WITH_DIR(P) \
+    (strchr (P, '/') != NULL || strchr (P, '\\') != NULL || HAS_DEVICE (P))
+
+static char *shared_library_fullname;
+
+BOOL WINAPI
+DllMain (HINSTANCE module_handle, DWORD event, LPVOID reserved)
+{
+  (void) reserved;
+
+  if (event == DLL_PROCESS_ATTACH)
+    {
+      /* The DLL is being loaded into an application's address range.  */
+      static char location[MAX_PATH];
+
+      if (!GetModuleFileName (module_handle, location, sizeof (location)))
+        /* Shouldn't happen.  */
+        return FALSE;
+
+      if (!IS_FILE_NAME_WITH_DIR (location))
+        /* Shouldn't happen.  */
+        return FALSE;
+
+      /* Avoid a memory leak when the same DLL get attached, detached,
+         attached, detached, and so on.  This happens e.g. when a spell
+         checker DLL is used repeatedly by a mail program.  */
+      if (!(shared_library_fullname != NULL
+            && strcmp (shared_library_fullname, location) == 0))
+        /* Remember the full pathname of the shared library.  */
+        shared_library_fullname = strdup (location);
+    }
+
+  return TRUE;
+}
+
+#endif