1
- /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.23 2004/08/29 05:06:59 momjian Exp $ */
1
+ /* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.24 2004/12/30 09:36:37 meskes Exp $ */
2
2
3
3
#define POSTGRES_ECPG_INTERNAL
4
4
#include "postgres_fe.h"
@@ -242,7 +242,7 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
242
242
struct sqlca_t * sqlca = ECPGget_sqlca ();
243
243
enum COMPAT_MODE compat = c ;
244
244
struct connection * this ;
245
- char * dbname = strdup (name ),
245
+ char * dbname = name ? strdup (name ) : NULL ,
246
246
* host = NULL ,
247
247
* tmp ,
248
248
* port = NULL ,
@@ -275,75 +275,100 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
275
275
if (dbname == NULL && connection_name == NULL )
276
276
connection_name = "DEFAULT" ;
277
277
278
- /* get the detail information out of dbname */
279
- if (strchr (dbname , '@' ) != NULL )
280
- {
281
- /* old style: dbname[@server][:port] */
282
- tmp = strrchr (dbname , ':' );
283
- if (tmp != NULL ) /* port number given */
284
- {
285
- port = strdup (tmp + 1 );
286
- * tmp = '\0' ;
287
- }
288
-
289
- tmp = strrchr (dbname , '@' );
290
- if (tmp != NULL ) /* host name given */
291
- {
292
- host = strdup (tmp + 1 );
293
- * tmp = '\0' ;
294
- }
295
- realname = strdup (dbname );
296
- }
297
- else if (strncmp (dbname , "tcp:" , 4 ) == 0 || strncmp (dbname , "unix:" , 5 ) == 0 )
298
- {
299
- int offset = 0 ;
300
-
301
- /*
302
- * only allow protocols tcp and unix
303
- */
304
- if (strncmp (dbname , "tcp:" , 4 ) == 0 )
305
- offset = 4 ;
306
- else if (strncmp (dbname , "unix:" , 5 ) == 0 )
307
- offset = 5 ;
308
-
309
- if (strncmp (dbname + offset , "postgresql://" , strlen ("postgresql://" )) == 0 )
278
+ if (dbname != NULL )
279
+ {
280
+ /* get the detail information out of dbname */
281
+ if (strchr (dbname , '@' ) != NULL )
310
282
{
311
-
312
- /*------
313
- * new style:
314
- * <tcp|unix>:postgresql://server[:port|:/unixsocket/path:]
315
- * [/db name][?options]
316
- *------
317
- */
318
- offset += strlen ("postgresql://" );
319
-
320
- tmp = strrchr (dbname + offset , '?' );
321
- if (tmp != NULL ) /* options given */
283
+ /* old style: dbname[@server][:port] */
284
+ tmp = strrchr (dbname , ':' );
285
+ if (tmp != NULL ) /* port number given */
322
286
{
323
- options = strdup (tmp + 1 );
287
+ port = strdup (tmp + 1 );
324
288
* tmp = '\0' ;
325
289
}
326
290
327
- tmp = last_dir_separator (dbname + offset );
328
- if (tmp != NULL ) /* database name given */
291
+ tmp = strrchr (dbname , '@' );
292
+ if (tmp != NULL ) /* host name given */
329
293
{
330
- realname = strdup (tmp + 1 );
294
+ host = strdup (tmp + 1 );
331
295
* tmp = '\0' ;
332
296
}
297
+ realname = strdup (dbname );
298
+ }
299
+ else if (strncmp (dbname , "tcp:" , 4 ) == 0 || strncmp (dbname , "unix:" , 5 ) == 0 )
300
+ {
301
+ int offset = 0 ;
302
+
303
+ /*
304
+ * only allow protocols tcp and unix
305
+ */
306
+ if (strncmp (dbname , "tcp:" , 4 ) == 0 )
307
+ offset = 4 ;
308
+ else if (strncmp (dbname , "unix:" , 5 ) == 0 )
309
+ offset = 5 ;
333
310
334
- tmp = strrchr (dbname + offset , ':' );
335
- if (tmp != NULL ) /* port number or Unix socket path given */
311
+ if (strncmp (dbname + offset , "postgresql://" , strlen ("postgresql://" )) == 0 )
336
312
{
337
- char * tmp2 ;
338
313
339
- * tmp = '\0' ;
340
- if ((tmp2 = strchr (tmp + 1 , ':' )) != NULL )
314
+ /*------
315
+ * new style:
316
+ * <tcp|unix>:postgresql://server[:port|:/unixsocket/path:]
317
+ * [/db name][?options]
318
+ *------
319
+ */
320
+ offset += strlen ("postgresql://" );
321
+
322
+ tmp = strrchr (dbname + offset , '?' );
323
+ if (tmp != NULL ) /* options given */
324
+ {
325
+ options = strdup (tmp + 1 );
326
+ * tmp = '\0' ;
327
+ }
328
+
329
+ tmp = last_dir_separator (dbname + offset );
330
+ if (tmp != NULL ) /* database name given */
341
331
{
342
- * tmp2 = '\0' ;
343
- host = strdup (tmp + 1 );
344
- if (strncmp (dbname , "unix:" , 5 ) != 0 )
332
+ realname = strdup (tmp + 1 );
333
+ * tmp = '\0' ;
334
+ }
335
+
336
+ tmp = strrchr (dbname + offset , ':' );
337
+ if (tmp != NULL ) /* port number or Unix socket path given */
338
+ {
339
+ char * tmp2 ;
340
+
341
+ * tmp = '\0' ;
342
+ if ((tmp2 = strchr (tmp + 1 , ':' )) != NULL )
345
343
{
346
- ECPGlog ("connect: socketname %s given for TCP connection in line %d\n" , host , lineno );
344
+ * tmp2 = '\0' ;
345
+ host = strdup (tmp + 1 );
346
+ if (strncmp (dbname , "unix:" , 5 ) != 0 )
347
+ {
348
+ ECPGlog ("connect: socketname %s given for TCP connection in line %d\n" , host , lineno );
349
+ ECPGraise (lineno , ECPG_CONNECT , ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION , realname ? realname : "<DEFAULT>" );
350
+ if (host )
351
+ ECPGfree (host );
352
+ if (port )
353
+ ECPGfree (port );
354
+ if (options )
355
+ ECPGfree (options );
356
+ if (realname )
357
+ ECPGfree (realname );
358
+ if (dbname )
359
+ ECPGfree (dbname );
360
+ return false;
361
+ }
362
+ }
363
+ else
364
+ port = strdup (tmp + 1 );
365
+ }
366
+
367
+ if (strncmp (dbname , "unix:" , 5 ) == 0 )
368
+ {
369
+ if (strcmp (dbname + offset , "localhost" ) != 0 && strcmp (dbname + offset , "127.0.0.1" ) != 0 )
370
+ {
371
+ ECPGlog ("connect: non-localhost access via sockets in line %d\n" , lineno );
347
372
ECPGraise (lineno , ECPG_CONNECT , ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION , realname ? realname : "<DEFAULT>" );
348
373
if (host )
349
374
ECPGfree (host );
@@ -359,37 +384,17 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
359
384
}
360
385
}
361
386
else
362
- port = strdup (tmp + 1 );
363
- }
387
+ host = strdup (dbname + offset );
364
388
365
- if (strncmp (dbname , "unix:" , 5 ) == 0 )
366
- {
367
- if (strcmp (dbname + offset , "localhost" ) != 0 && strcmp (dbname + offset , "127.0.0.1" ) != 0 )
368
- {
369
- ECPGlog ("connect: non-localhost access via sockets in line %d\n" , lineno );
370
- ECPGraise (lineno , ECPG_CONNECT , ECPG_SQLSTATE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION , realname ? realname : "<DEFAULT>" );
371
- if (host )
372
- ECPGfree (host );
373
- if (port )
374
- ECPGfree (port );
375
- if (options )
376
- ECPGfree (options );
377
- if (realname )
378
- ECPGfree (realname );
379
- if (dbname )
380
- ECPGfree (dbname );
381
- return false;
382
- }
383
389
}
384
390
else
385
- host = strdup (dbname + offset );
386
-
391
+ realname = strdup (dbname );
387
392
}
388
393
else
389
394
realname = strdup (dbname );
390
395
}
391
396
else
392
- realname = strdup ( dbname ) ;
397
+ realname = NULL ;
393
398
394
399
/* add connection to our list */
395
400
#ifdef ENABLE_THREAD_SAFETY
0 commit comments