Make the 'link' module link on Windows NT 4.
authorBruno Haible <bruno@clisp.org>
Tue, 20 Jan 2009 23:58:26 +0000 (00:58 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 20 Jan 2009 23:58:26 +0000 (00:58 +0100)
ChangeLog
lib/link.c

index 521eb24d6363ecb42f6dd30604e18476e8d71d1e..5b4d64cff1083e12393a67c2bc3c6f16e98b22d9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2009-01-20  Bruno Haible  <bruno@clisp.org>
+
+       Make the 'link' module link on Windows NT 4.
+       * lib/link.c (_WIN32_WINNT): Don't define.
+       (CreateHardLinkFuncType): New type.
+       (CreateHardLinkFunc, initialized): New variables.
+       (initialize): New function.
+       (link): Invoke CreateHardLink indirectly through the function pointer.
+
 2009-01-20  Bruno Haible  <bruno@clisp.org>
 
        Fix compilation failure on mingw.
index 6737d37e9d7cc7bde589611f1ccd0a7a91874df1..2b5a97f6fb5f33eeec43b9e4891eee23cb787683 100644 (file)
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 
 #define WIN32_LEAN_AND_MEAN
-#define _WIN32_WINNT 0x0500
 #include <unistd.h>
 #include <windows.h>
 
 #include <errno.h>
 
+/* CreateHardLink was introduced only in Windows 2000.  */
+typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCTSTR lpFileName,
+                                               LPCTSTR lpExistingFileName,
+                                               LPSECURITY_ATTRIBUTES lpSecurityAttributes);
+static CreateHardLinkFuncType CreateHardLinkFunc = NULL;
+static BOOL initialized = FALSE;
+
+static void
+initialize (void)
+{
+  HMODULE kernel32 = LoadLibrary ("kernel32.dll");
+  if (kernel32 != NULL)
+    {
+      CreateHardLinkFunc =
+       (CreateHardLinkFuncType) GetProcAddress (kernel32, "CreateHardLinkA");
+    }
+  initialized = TRUE;
+}
+
 int
 link (const char *path1, const char *path2)
 {
-  if (CreateHardLink (path2, path1, NULL) == 0)
+  if (!initialized)
+    initialize ();
+  if (CreateHardLinkFunc == NULL)
+    {
+      /* System does not support hard links.  */
+      errno = EPERM;
+      return -1;
+    }
+  if (CreateHardLinkFunc (path2, path1, NULL) == 0)
     {
       /* It is not documented which errors CreateHardLink() can produce.
        * The following conversions are based on tests on a Windows XP SP2