From f5df006a04bdaed8ca8e7595bdd5c7c037d65dab Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Mon, 25 Jul 2005 17:17:41 +0000
Subject: [PATCH] Add username for psql password prompt, if the username was
 specified.

Adrian Maier
---
 src/bin/psql/command.c | 17 ++++++++++++++---
 src/bin/psql/startup.c | 17 ++++++++++++++---
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 6e5730e4436..824f2321035 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.150 2005/07/18 20:57:53 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.151 2005/07/25 17:17:41 momjian Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -911,6 +911,7 @@ do_connect(const char *new_dbname, const char *new_user)
 	const char *dbparam = NULL;
 	const char *userparam = NULL;
 	const char *pwparam = NULL;
+	char       *password_prompt = NULL;
 	char	   *prompted_password = NULL;
 	bool		need_pass;
 	bool		success = false;
@@ -930,9 +931,18 @@ do_connect(const char *new_dbname, const char *new_user)
 	else
 		userparam = new_user;
 
+	if (userparam == NULL) 
+		password_prompt = strdup("Password: ");
+	else
+	{
+		password_prompt = malloc(strlen("Password for user %s: ") - 2 +
+								 strlen(userparam) + 1);
+		sprintf(password_prompt,"Password for user %s: ", userparam);
+	}
+
 	/* need to prompt for password? */
 	if (pset.getPassword)
-		pwparam = prompted_password = simple_prompt("Password: ", 100, false);
+		pwparam = prompted_password = simple_prompt(password_prompt, 100, false);
 
 	/*
 	 * Use old password (if any) if no new one given and we are
@@ -956,11 +966,12 @@ do_connect(const char *new_dbname, const char *new_user)
 			need_pass = true;
 			free(prompted_password);
 			prompted_password = NULL;
-			pwparam = prompted_password = simple_prompt("Password: ", 100, false);
+			pwparam = prompted_password = simple_prompt(password_prompt, 100, false);
 		}
 	} while (need_pass);
 
 	free(prompted_password);
+	free(password_prompt);
 
 	/*
 	 * If connection failed, try at least keep the old one. That's
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 3f603757c4a..0c3606ed25b 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2005, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.119 2005/07/14 08:42:37 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.120 2005/07/25 17:17:41 momjian Exp $
  */
 #include "postgres_fe.h"
 
@@ -106,6 +106,7 @@ main(int argc, char *argv[])
 
 	char	   *username = NULL;
 	char	   *password = NULL;
+	char       *password_prompt = NULL;
 	bool		need_pass;
 
 	set_pglocale_pgservice(argv[0], "psql");
@@ -188,8 +189,17 @@ main(int argc, char *argv[])
 			username = pg_strdup(options.username);
 	}
 
+	if (options.username == NULL)
+		password_prompt = strdup("Password: ");
+	else
+	{
+		password_prompt = malloc(strlen("Password for user %s: ") - 2 +
+								 strlen(options.username) + 1);
+		sprintf(password_prompt,"Password for user %s: ", options.username);
+	}
+	
 	if (pset.getPassword)
-		password = simple_prompt("Password: ", 100, false);
+		password = simple_prompt(password_prompt, 100, false);
 
 	/* loop until we have a password if requested by backend */
 	do
@@ -207,12 +217,13 @@ main(int argc, char *argv[])
 			need_pass = true;
 			free(password);
 			password = NULL;
-			password = simple_prompt("Password: ", 100, false);
+			password = simple_prompt(password_prompt, 100, false);
 		}
 	} while (need_pass);
 
 	free(username);
 	free(password);
+	free(password_prompt);
 
 	if (PQstatus(pset.db) == CONNECTION_BAD)
 	{
-- 
GitLab