--- tail.c.ori	Thu Aug 05 16:38:02 1999
+++ tail.c	Tue Jul 04 21:58:17 2000
@@ -37,6 +37,14 @@
 #include "safe-read.h"
 #include "xstrtol.h"
 
+#ifdef _WIN32
+#define STRICT
+#define NOGDI
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <io.h>
+#endif
+
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "tail"
 
@@ -213,6 +221,14 @@
 Usage: %s [OPTION]... [FILE]...\n\
 "),
 	      program_name);
+#ifdef _WIN32
+      {
+/* This string is too big, it goes into a ressource file. */
+	HMODULE hMod = GetModuleHandle(NULL);
+	HRSRC hRes = FindResource(hMod, "helpmsg", RT_RCDATA);
+	LPSTR msg = LockResource(LoadResource(hMod, hRes));
+	printf(_(msg), 
+#else
       printf (_("\
 Print the last %d lines of each FILE to standard output.\n\
 With more than one FILE, precede each with a header giving the file name.\n\
@@ -254,10 +270,14 @@
 recreated by some other program.\n\
 \n\
 "),
+#endif
 	      DEFAULT_N_LINES, DEFAULT_N_LINES,
 	      DEFAULT_MAX_N_UNCHANGED_STATS_BETWEEN_OPENS,
 	      DEFAULT_MAX_N_CONSECUTIVE_SIZE_CHANGES
 	      );
+#ifdef _WIN32
+      }
+#endif
       puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
     }
   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
@@ -384,7 +404,7 @@
 	  if (buffer[i] == '\n' && n_lines-- == 0)
 	    {
 	      /* If this newline wasn't the last character in the buffer,
-	         print the text after it.  */
+		 print the text after it.  */
 	      if (i != bytes_read - 1)
 		xwrite (STDOUT_FILENO, &buffer[i + 1], bytes_read - (i + 1));
 	      dump_remainder (pretty_filename, fd,
@@ -454,8 +474,8 @@
       total_lines += tmp->nlines;
 
       /* If there is enough room in the last buffer read, just append the new
-         one to it.  This is because when reading from a pipe, `nbytes' can
-         often be very small.  */
+	 one to it.  This is because when reading from a pipe, `nbytes' can
+	 often be very small.  */
       if (tmp->nbytes + last->nbytes < BUFSIZ)
 	{
 	  memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
@@ -512,7 +532,7 @@
       char *cp;
 
       /* Skip `total_lines' - `n_lines' newlines.  We made sure that
-         `total_lines' - `n_lines' <= `tmp->nlines'.  */
+	 `total_lines' - `n_lines' <= `tmp->nlines'.  */
       cp = tmp->buffer;
       for (i = total_lines - n_lines; i; --i)
 	while (*cp++ != '\n')
@@ -567,8 +587,8 @@
 
       total_bytes += tmp->nbytes;
       /* If there is enough room in the last buffer read, just append the new
-         one to it.  This is because when reading from a pipe, `nbytes' can
-         often be very small.  */
+	 one to it.  This is because when reading from a pipe, `nbytes' can
+	 often be very small.  */
       if (tmp->nbytes + last->nbytes < BUFSIZ)
 	{
 	  memcpy (&last->buffer[last->nbytes], tmp->buffer, tmp->nbytes);
@@ -731,7 +751,10 @@
 	}
     }
   else if (!S_ISREG (new_stats.st_mode)
-	   && !S_ISFIFO (new_stats.st_mode))
+#ifndef _WIN32
+	   && !S_ISFIFO (new_stats.st_mode)
+#endif
+	   )
     {
       fail = 1;
       f->errnum = -1;
@@ -758,7 +781,11 @@
       assert (f->fd == -1);
       error (0, 0, _("`%s' has become accessible"), pretty_name (f));
     }
+#ifdef _WIN32
+  else if (f->fd != -1 && ! fdmatch(f->fd, fd))
+#else
   else if (f->ino != new_stats.st_ino || f->dev != new_stats.st_dev)
+#endif
     {
       new_file = 1;
       if (f->fd == -1)
@@ -824,8 +851,24 @@
 static void
 tail_forever (struct File_spec *f, int nfiles)
 {
-  int last;
+#ifdef _WIN32
+  HANDLE hProc;
+#endif
   int writer_is_dead = 0;
+  int last;
+
+#ifdef _WIN32
+  if (pid != 0)
+    {
+      hProc = OpenProcess(SYNCHRONIZE, FALSE, pid);
+      if (hProc == NULL && GetLastError() == ERROR_INVALID_PARAMETER)
+        writer_is_dead = 1;
+    }
+  else
+    {
+      hProc = NULL;
+    }
+#endif
 
   last = nfiles - 1;
 
@@ -917,7 +960,25 @@
       if (!any_changed)
 	{
 	  if (writer_is_dead)
-	    break;
+  	    break;
+
+#ifdef _WIN32
+	  if (hProc == NULL)
+	    {
+	      Sleep((DWORD) (1000 * sleep_interval));
+	    }
+	  else
+	    {
+	      if (WaitForSingleObject(hProc,
+	  			(DWORD) (1000 * sleep_interval))
+			== WAIT_OBJECT_0)
+		{
+		  CloseHandle(hProc);
+		  hProc = NULL;
+		  writer_is_dead = 1;
+		}
+	    }
+#else
 	  sleep (sleep_interval);
 
 	  /* Once the writer is dead, read the files once more to
@@ -928,6 +989,7 @@
 			       signal to the writer, so kill fails and sets
 			       errno to EPERM.  */
 			    && errno != EPERM);
+#endif
 	}
     }
 }
@@ -1036,7 +1098,7 @@
   else
     {
       /* Use file_lines only if FD refers to a regular file with
-         its file pointer positioned at beginning of file.  */
+	 its file pointer positioned at beginning of file.  */
       /* FIXME: adding the lseek conjunct is a kludge.
 	 Once there's a reasonable test suite, fix the true culprit:
 	 file_lines.  file_lines shouldn't presume that the input
@@ -1115,7 +1177,11 @@
 	      errors = 1;
 	      f->errnum = errno;
 	    }
-	  else if (!S_ISREG (stats.st_mode) && !S_ISFIFO (stats.st_mode))
+	  else if (!S_ISREG (stats.st_mode)
+#ifndef _WIN32
+	  	&& !S_ISFIFO (stats.st_mode)
+#endif
+		)
 	    {
 	      error (0, 0, _("%s: cannot follow end of non-regular file"),
 		     pretty_name (f));
@@ -1374,7 +1440,7 @@
 	  break;
 
 	case CHAR_MAX + 3:
-  	  /* --max-consecutive-size-changes=N */
+	  /* --max-consecutive-size-changes=N */
 	  if (xstrtoul (optarg, NULL, 10,
 			&max_n_consecutive_size_changes_between_opens, "")
 	      != LONGINT_OK)
