@@ -20,9 +20,7 @@ static void
2020int
2121main (int argc , char * * argv )
2222{
23- int c ,
24- optindex ,
25- opt_index = 2 ;
23+ int c ;
2624
2725 const char * progname ;
2826
@@ -32,14 +30,22 @@ main(int argc, char **argv)
3230 const char * pgdbname = NULL ;
3331 const char * connect_timeout = DEFAULT_CONNECT_TIMEOUT ;
3432
35- const char * keywords [7 ] = {NULL };
36- const char * values [7 ] = {NULL };
33+ const char * pghost_str = NULL ;
34+ const char * pgport_str = NULL ;
35+
36+ #define PARAMS_ARRAY_SIZE 7
37+
38+ const char * keywords [PARAMS_ARRAY_SIZE ];
39+ const char * values [PARAMS_ARRAY_SIZE ];
3740
3841 bool quiet = false;
3942
40- PGPing rv ;
41- PQconninfoOption * connect_options ,
42- * conn_opt_ptr ;
43+ PGPing rv ;
44+ PQconninfoOption * opts = NULL ;
45+ PQconninfoOption * defs = NULL ;
46+ PQconninfoOption * opt ;
47+ PQconninfoOption * def ;
48+ char * errmsg = NULL ;
4349
4450 /*
4551 * We accept user and database as options to avoid useless errors from
@@ -60,7 +66,7 @@ main(int argc, char **argv)
6066 set_pglocale_pgservice (argv [0 ], PG_TEXTDOMAIN ("pgscripts" ));
6167 handle_help_version_opts (argc , argv , progname , help );
6268
63- while ((c = getopt_long (argc , argv , "d:h:p:qt:U:V " , long_options , & optindex )) != -1 )
69+ while ((c = getopt_long (argc , argv , "d:h:p:qt:U:" , long_options , NULL )) != -1 )
6470 {
6571 switch (c )
6672 {
@@ -106,66 +112,74 @@ main(int argc, char **argv)
106112 exit (PQPING_NO_ATTEMPT );
107113 }
108114
115+ keywords [0 ] = "host" ;
116+ values [0 ] = pghost ;
117+ keywords [1 ] = "port" ;
118+ values [1 ] = pgport ;
119+ keywords [2 ] = "user" ;
120+ values [2 ] = pguser ;
121+ keywords [3 ] = "dbname" ;
122+ values [3 ] = pgdbname ;
123+ keywords [4 ] = "connect_timeout" ;
124+ values [4 ] = connect_timeout ;
125+ keywords [5 ] = "fallback_application_name" ;
126+ values [5 ] = progname ;
127+ keywords [6 ] = NULL ;
128+ values [6 ] = NULL ;
129+
109130 /*
110- * Set connection options
131+ * Get the host and port so we can display them in our output
111132 */
112-
113- keywords [0 ] = "connect_timeout" ;
114- values [0 ] = connect_timeout ;
115- keywords [1 ] = "fallback_application_name" ;
116- values [1 ] = progname ;
117- if (pguser )
133+ if (pgdbname )
118134 {
119- keywords [opt_index ] = "user" ;
120- values [opt_index ] = pguser ;
121- opt_index ++ ;
135+ opts = PQconninfoParse (pgdbname , & errmsg );
136+ if (opts == NULL )
137+ {
138+ fprintf (stderr , _ ("%s: %s\n" ), progname , errmsg );
139+ exit (PQPING_NO_ATTEMPT );
140+ }
122141 }
123- if (pgdbname )
142+
143+ defs = PQconndefaults ();
144+ if (defs == NULL )
124145 {
125- keywords [opt_index ] = "dbname" ;
126- values [opt_index ] = pgdbname ;
127- opt_index ++ ;
146+ fprintf (stderr , _ ("%s: cannot fetch default options\n" ), progname );
147+ exit (PQPING_NO_ATTEMPT );
128148 }
129149
130- /*
131- * Get the default host and port so we can display them in our output
132- */
133- connect_options = PQconndefaults ();
134- conn_opt_ptr = connect_options ;
135- while (conn_opt_ptr -> keyword )
150+ for (opt = opts , def = defs ; def -> keyword ; def ++ )
136151 {
137- if (strncmp (conn_opt_ptr -> keyword , "host" , 5 ) == 0 )
152+ if (strcmp (def -> keyword , "hostaddr" ) == 0 ||
153+ strcmp (def -> keyword , "host" ) == 0 )
138154 {
139- if (pghost )
140- {
141- keywords [opt_index ] = conn_opt_ptr -> keyword ;
142- values [opt_index ] = pghost ;
143- opt_index ++ ;
144- }
145- else if (conn_opt_ptr -> val )
146- pghost = conn_opt_ptr -> val ;
155+ if (opt && opt -> val )
156+ pghost_str = opt -> val ;
157+ else if (pghost )
158+ pghost_str = pghost ;
159+ else if (def -> val )
160+ pghost_str = def -> val ;
147161 else
148- pghost = DEFAULT_PGSOCKET_DIR ;
162+ pghost_str = DEFAULT_PGSOCKET_DIR ;
149163 }
150- else if (strncmp ( conn_opt_ptr -> keyword , "port" , 5 ) == 0 )
164+ else if (strcmp ( def -> keyword , "port" ) == 0 )
151165 {
152- if (pgport )
153- {
154- keywords [opt_index ] = conn_opt_ptr -> keyword ;
155- values [opt_index ] = pgport ;
156- opt_index ++ ;
157- }
158- else if (conn_opt_ptr -> val )
159- pgport = conn_opt_ptr -> val ;
166+ if (opt && opt -> val )
167+ pgport_str = opt -> val ;
168+ else if (pgport )
169+ pgport_str = pgport ;
170+ else if (def -> val )
171+ pgport_str = def -> val ;
160172 }
161- conn_opt_ptr ++ ;
173+
174+ if (opt )
175+ opt ++ ;
162176 }
163177
164178 rv = PQpingParams (keywords , values , 1 );
165179
166180 if (!quiet )
167181 {
168- printf ("%s:%s - " , pghost , pgport );
182+ printf ("%s:%s - " , pghost_str , pgport_str );
169183
170184 switch (rv )
171185 {
@@ -186,8 +200,6 @@ main(int argc, char **argv)
186200 }
187201 }
188202
189- PQconninfoFree (connect_options );
190-
191203 exit (rv );
192204}
193205
0 commit comments