7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.154 1998/08/17 03:50:17 scrappy Exp $
10
+ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.155 1998/08/22 04:49:05 momjian Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -132,9 +132,6 @@ static int tableDesc(PsqlSettings *pset, char *table, FILE *fout);
132
132
static int objectDescription (PsqlSettings * pset , char * object );
133
133
static int rightsList (PsqlSettings * pset );
134
134
static void prompt_for_password (char * username , char * password );
135
- static char *
136
- make_connect_string (char * host , char * port , char * dbname ,
137
- char * username , char * password );
138
135
139
136
static char * gets_noreadline (char * prompt , FILE * source );
140
137
static char * gets_readline (char * prompt , FILE * source );
@@ -1402,35 +1399,28 @@ do_connect(const char *new_dbname,
1402
1399
else
1403
1400
{
1404
1401
PGconn * olddb = pset -> db ;
1405
- static char * userenv = NULL ;
1406
- char * old_userenv = NULL ;
1407
1402
const char * dbparam ;
1408
-
1409
- if (new_user != NULL )
1410
- {
1411
-
1412
- /*
1413
- * PQsetdb() does not allow us to specify the user, so we have
1414
- * to do it via PGUSER
1415
- */
1416
- if (userenv != NULL )
1417
- old_userenv = userenv ;
1418
- userenv = malloc (strlen ("PGUSER=" ) + strlen (new_user ) + 1 );
1419
- sprintf (userenv , "PGUSER=%s" , new_user );
1420
- /* putenv() may continue to use memory as part of environment */
1421
- putenv (userenv );
1422
- /* can delete old memory if we malloc'ed it */
1423
- if (old_userenv != NULL )
1424
- free (old_userenv );
1425
- }
1403
+ const char * userparam ;
1404
+ const char * pwparam ;
1426
1405
1427
1406
if (strcmp (new_dbname , "-" ) != 0 )
1428
1407
dbparam = new_dbname ;
1429
1408
else
1430
1409
dbparam = PQdb (olddb );
1431
1410
1432
- pset -> db = PQsetdb (PQhost (olddb ), PQport (olddb ),
1433
- NULL , NULL , dbparam );
1411
+ if (new_user != NULL && strcmp (new_user , "-" ) != 0 )
1412
+ userparam = new_user ;
1413
+ else
1414
+ userparam = PQuser (olddb );
1415
+
1416
+ /* libpq doesn't provide an accessor function for the password,
1417
+ * so we cheat here.
1418
+ */
1419
+ pwparam = olddb -> pgpass ;
1420
+
1421
+ pset -> db = PQsetdbLogin (PQhost (olddb ), PQport (olddb ),
1422
+ NULL , NULL , dbparam , userparam , pwparam );
1423
+
1434
1424
if (!pset -> quiet )
1435
1425
{
1436
1426
if (!new_user )
@@ -2765,16 +2755,13 @@ main(int argc, char **argv)
2765
2755
2766
2756
if (settings .getPassword )
2767
2757
{
2768
- char username [9 ];
2769
- char password [9 ];
2770
- char * connect_string ;
2758
+ char username [100 ];
2759
+ char password [100 ];
2771
2760
2772
2761
prompt_for_password (username , password );
2773
2762
2774
- /* now use PQconnectdb so we can pass these options */
2775
- connect_string = make_connect_string (host , port , dbname , username , password );
2776
- settings .db = PQconnectdb (connect_string );
2777
- free (connect_string );
2763
+ settings .db = PQsetdbLogin (host , port , NULL , NULL , dbname ,
2764
+ username , password );
2778
2765
}
2779
2766
else
2780
2767
settings .db = PQsetdb (host , port , NULL , NULL , dbname );
@@ -2784,7 +2771,7 @@ main(int argc, char **argv)
2784
2771
if (PQstatus (settings .db ) == CONNECTION_BAD )
2785
2772
{
2786
2773
fprintf (stderr , "Connection to database '%s' failed.\n" , dbname );
2787
- fprintf (stderr , "%s" , PQerrorMessage (settings .db ));
2774
+ fprintf (stderr , "%s\n " , PQerrorMessage (settings .db ));
2788
2775
PQfinish (settings .db );
2789
2776
exit (1 );
2790
2777
}
@@ -3018,6 +3005,7 @@ setFout(PsqlSettings *pset, char *fname)
3018
3005
static void
3019
3006
prompt_for_password (char * username , char * password )
3020
3007
{
3008
+ char buf [512 ];
3021
3009
int length ;
3022
3010
3023
3011
#ifdef HAVE_TERMIOS_H
@@ -3027,13 +3015,11 @@ prompt_for_password(char *username, char *password)
3027
3015
#endif
3028
3016
3029
3017
printf ("Username: " );
3030
- fgets (username , 9 , stdin );
3018
+ fgets (username , 100 , stdin );
3031
3019
length = strlen (username );
3032
3020
/* skip rest of the line */
3033
3021
if (length > 0 && username [length - 1 ] != '\n' )
3034
3022
{
3035
- static char buf [512 ];
3036
-
3037
3023
do
3038
3024
{
3039
3025
fgets (buf , 512 , stdin );
@@ -3049,7 +3035,7 @@ prompt_for_password(char *username, char *password)
3049
3035
t .c_lflag &= ~ECHO ;
3050
3036
tcsetattr (0 , TCSADRAIN , & t );
3051
3037
#endif
3052
- fgets (password , 9 , stdin );
3038
+ fgets (password , 100 , stdin );
3053
3039
#ifdef HAVE_TERMIOS_H
3054
3040
tcsetattr (0 , TCSADRAIN , & t_orig );
3055
3041
#endif
@@ -3058,8 +3044,6 @@ prompt_for_password(char *username, char *password)
3058
3044
/* skip rest of the line */
3059
3045
if (length > 0 && password [length - 1 ] != '\n' )
3060
3046
{
3061
- static char buf [512 ];
3062
-
3063
3047
do
3064
3048
{
3065
3049
fgets (buf , 512 , stdin );
@@ -3070,62 +3054,3 @@ prompt_for_password(char *username, char *password)
3070
3054
3071
3055
printf ("\n\n" );
3072
3056
}
3073
-
3074
- static char *
3075
- make_connect_string (char * host , char * port , char * dbname ,
3076
- char * username , char * password )
3077
- {
3078
- int connect_string_len = 0 ;
3079
- char * connect_string ;
3080
-
3081
- if (host )
3082
- connect_string_len += 6 + strlen (host ); /* 6 == "host=" + " " */
3083
- if (username )
3084
- connect_string_len += 6 + strlen (username ); /* 6 == "user=" + " " */
3085
- if (password )
3086
- connect_string_len += 10 + strlen (password ); /* 10 == "password=" + "
3087
- * " */
3088
- if (port )
3089
- connect_string_len += 6 + strlen (port ); /* 6 == "port=" + " " */
3090
- if (dbname )
3091
- connect_string_len += 8 + strlen (dbname ); /* 8 == "dbname=" + " " */
3092
- connect_string_len += 18 ; /* "authtype=password" + null */
3093
-
3094
- connect_string = (char * ) malloc (connect_string_len );
3095
- if (!connect_string )
3096
- return 0 ;
3097
- connect_string [0 ] = '\0' ;
3098
- if (host )
3099
- {
3100
- strcat (connect_string , "host=" );
3101
- strcat (connect_string , host );
3102
- strcat (connect_string , " " );
3103
- }
3104
- if (username )
3105
- {
3106
- strcat (connect_string , "user=" );
3107
- strcat (connect_string , username );
3108
- strcat (connect_string , " " );
3109
- }
3110
- if (password )
3111
- {
3112
- strcat (connect_string , "password=" );
3113
- strcat (connect_string , password );
3114
- strcat (connect_string , " " );
3115
- }
3116
- if (port )
3117
- {
3118
- strcat (connect_string , "port=" );
3119
- strcat (connect_string , port );
3120
- strcat (connect_string , " " );
3121
- }
3122
- if (dbname )
3123
- {
3124
- strcat (connect_string , "dbname=" );
3125
- strcat (connect_string , dbname );
3126
- strcat (connect_string , " " );
3127
- }
3128
- strcat (connect_string , "authtype=password" );
3129
-
3130
- return connect_string ;
3131
- }
0 commit comments