From ef3f7c3f74961883c355a6e7ec9ee67d52d92eef Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Fri, 3 Mar 2006 23:49:12 +0000
Subject: [PATCH] Avoid trying to open /dev/tty on Win32.  Some Win32 systems
 have /dev/tty, but it isn't a device file and doesn't work as expected.

This fixes a known bug where psql does not prompt for a password on some
Win32 systems.

Backpatch to 8.1.X.

Robert Kinberg
---
 src/bin/psql/command.c |  7 +++++--
 src/port/sprompt.c     | 12 +++++++++---
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 8277021859..2ff454c842 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.161 2006/02/12 04:04:32 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.162 2006/03/03 23:49:12 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -753,8 +753,11 @@ exec_command(const char *cmd,
 
 		expand_tilde(&fname);
 		/* This scrolls off the screen when using /dev/tty */
+#ifndef WIN32
 		success = saveHistory(fname ? fname : "/dev/tty");
-
+#else
+		success = saveHistory(fname ? fname : stderr);
+#endif
 		if (success && !quiet && fname)
 			printf(gettext("Wrote history to file \"%s/%s\".\n"),
 				   pset.dirname ? pset.dirname : ".", fname);
diff --git a/src/port/sprompt.c b/src/port/sprompt.c
index cb0c910afb..46d3c627c8 100644
--- a/src/port/sprompt.c
+++ b/src/port/sprompt.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/port/sprompt.c,v 1.12 2005/10/15 02:49:51 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/port/sprompt.c,v 1.13 2006/03/03 23:49:12 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -40,8 +40,8 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
 {
 	int			length;
 	char	   *destination;
-	FILE	   *termin,
-			   *termout;
+	FILE	   *termin = NULL,
+			   *termout = NULL;
 
 #ifdef HAVE_TERMIOS_H
 	struct termios t_orig,
@@ -63,8 +63,14 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
 	 * Do not try to collapse these into one "w+" mode file. Doesn't work on
 	 * some platforms (eg, HPUX 10.20).
 	 */
+#ifndef WIN32
+	/*
+	 *	Some win32 platforms actually have a /dev/tty file, but it isn't
+	 *	a device file, and it doesn't work as expected, so we avoid trying.
+	 */
 	termin = fopen("/dev/tty", "r");
 	termout = fopen("/dev/tty", "w");
+#endif
 	if (!termin || !termout)
 	{
 		if (termin)