@@ -46,6 +46,7 @@ struct options
4646/* function prototypes */
4747void get_opts (int ,char * * ,struct options * );
4848void * myalloc (size_t size );
49+ char * mystrdup (const char * str );
4950void add_one_elt (char * eltname ,eary * eary );
5051char * get_comma_elts (eary * eary );
5152PGconn * sql_conn (struct options * );
@@ -68,6 +69,11 @@ get_opts(int argc, char **argv, struct options * my_opts)
6869my_opts -> nodb = false;
6970my_opts -> extended = false;
7071my_opts -> tablespaces = false;
72+ my_opts -> dbname = NULL ;
73+ my_opts -> hostname = NULL ;
74+ my_opts -> port = NULL ;
75+ my_opts -> username = NULL ;
76+ my_opts -> password = NULL ;
7177
7278/* get opts */
7379while ((c = getopt (argc ,argv ,"H:p:U:P:d:t:o:f:qSxish?" ))!= -1 )
@@ -76,8 +82,7 @@ get_opts(int argc, char **argv, struct options * my_opts)
7682{
7783/* specify the database */
7884case 'd' :
79- my_opts -> dbname = (char * )myalloc (strlen (optarg ));
80- sscanf (optarg ,"%s" ,my_opts -> dbname );
85+ my_opts -> dbname = mystrdup (optarg );
8186break ;
8287
8388/* specify one tablename to show */
@@ -102,26 +107,22 @@ get_opts(int argc, char **argv, struct options * my_opts)
102107
103108/* host to connect to */
104109case 'H' :
105- my_opts -> hostname = (char * )myalloc (strlen (optarg ));
106- sscanf (optarg ,"%s" ,my_opts -> hostname );
110+ my_opts -> hostname = mystrdup (optarg );
107111break ;
108112
109113/* port to connect to on remote host */
110114case 'p' :
111- my_opts -> port = (char * )myalloc (strlen (optarg ));
112- sscanf (optarg ,"%s" ,my_opts -> port );
115+ my_opts -> port = mystrdup (optarg );
113116break ;
114117
115118/* username */
116119case 'U' :
117- my_opts -> username = (char * )myalloc (strlen (optarg ));
118- sscanf (optarg ,"%s" ,my_opts -> username );
120+ my_opts -> username = mystrdup (optarg );
119121break ;
120122
121123/* password */
122124case 'P' :
123- my_opts -> password = (char * )myalloc (strlen (optarg ));
124- sscanf (optarg ,"%s" ,my_opts -> password );
125+ my_opts -> password = mystrdup (optarg );
125126break ;
126127
127128/* display system tables */
@@ -183,6 +184,18 @@ myalloc(size_t size)
183184return ptr ;
184185}
185186
187+ char *
188+ mystrdup (const char * str )
189+ {
190+ char * result = strdup (str );
191+ if (!result )
192+ {
193+ fprintf (stderr ,"out of memory" );
194+ exit (1 );
195+ }
196+ return result ;
197+ }
198+
186199/*
187200 * add_one_elt
188201 *
@@ -208,7 +221,7 @@ add_one_elt(char *eltname, eary *eary)
208221}
209222}
210223
211- eary -> array [eary -> num ]= strdup (eltname );
224+ eary -> array [eary -> num ]= mystrdup (eltname );
212225eary -> num ++ ;
213226}
214227
@@ -227,7 +240,7 @@ get_comma_elts(eary *eary)
227240int i ,length = 0 ;
228241
229242if (eary -> num == 0 )
230- return "" ;
243+ return mystrdup ( "" ) ;
231244
232245/*
233246 * PQescapeString wants 2 * length + 1 bytes of breath space. Add two