File tree Expand file tree Collapse file tree 1 file changed +22
-32
lines changed Expand file tree Collapse file tree 1 file changed +22
-32
lines changed Original file line number Diff line number Diff line change @@ -105,51 +105,41 @@ cache_put(Oid oid, char *name)
105
105
bool
106
106
DBConnect (const char * host , const char * port , char * database , const char * user )
107
107
{
108
- char * password = NULL ;
109
- char * password_prompt = NULL ;
110
- bool need_pass ;
111
-
112
108
pghost = strdup (host );
113
109
pgport = strdup (port );
114
110
pguser = strdup (user );
111
+ pgpass = NULL ;
112
+
113
+ retry_login :
114
+ conn = PQsetdbLogin (pghost ,
115
+ pgport ,
116
+ NULL ,
117
+ NULL ,
118
+ database ,
119
+ pguser ,
120
+ pgpass );
115
121
116
- /* loop until we have a password if requested by backend */
117
- do
122
+ if (PQstatus (conn ) == CONNECTION_BAD )
118
123
{
119
- need_pass = false;
120
-
121
- conn = PQsetdbLogin (host ,
122
- port ,
123
- NULL ,
124
- NULL ,
125
- database ,
126
- user ,
127
- password );
128
-
129
- if (PQstatus (conn ) == CONNECTION_BAD &&
130
- strcmp (PQerrorMessage (conn ), PQnoPasswordSupplied ) == 0 &&
131
- !feof (stdin ))
124
+ /* wait a password if required to login, then retry. */
125
+ if (strcmp (PQerrorMessage (conn ), PQnoPasswordSupplied ) == 0 &&
126
+ !feof (stdin ))
132
127
{
133
128
PQfinish (conn );
134
- need_pass = true;
135
- free (password );
136
- password = NULL ;
137
- printf ("\nPassword: " );
138
- password = simple_prompt (password_prompt , 100 , false);
129
+ conn = NULL ;
130
+ pgpass = simple_prompt ("Password: " , 100 , false);
131
+ goto retry_login ;
139
132
}
140
- } while (need_pass );
141
133
142
- if (password )
143
- pgpass = strdup (password );
144
-
145
- /* Check to see that the backend connection was successfully made */
146
- if (PQstatus (conn ) == CONNECTION_BAD )
147
- {
148
134
fprintf (stderr , "Connection to database failed: %s" ,
149
135
PQerrorMessage (conn ));
136
+
137
+ PQfinish (conn );
138
+ conn = NULL ;
139
+
150
140
return false;
151
141
}
152
-
142
+
153
143
return true;
154
144
}
155
145
You can’t perform that action at this time.
0 commit comments