From b27af0d94aad98039f8cfec14ab9c81dbcb3712f Mon Sep 17 00:00:00 2001 From: "Marc G. Fournier" <scrappy@hub.org> Date: Tue, 7 Jul 1998 18:00:09 +0000 Subject: [PATCH] From: "Dr. Michael Meskes" <meskes@online-club.de> My first try at libpq. This one enables the two styles we agreed upon for database descriptors. --- src/interfaces/libpq/fe-connect.c | 103 +++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 61adfceace6..672948a287a 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.71 1998/07/03 04:29:04 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.72 1998/07/07 18:00:09 scrappy Exp $ * *------------------------------------------------------------------------- */ @@ -393,6 +393,101 @@ PQsetdbLogin(const char *pghost, const char *pgport, const char *pgoptions, cons } +/* + * update_db_info - + * get all additional infos out of dbName + * + */ +static int +update_db_info(PGconn *conn) +{ + char *tmp, *old = conn->dbName; + + if (strchr(conn->dbName, '@') != NULL) + { + /* old style: dbname[@server][:port] */ + tmp = strrchr(conn->dbName, ':'); + if (tmp != NULL) /* port number given */ + { + conn->pgport = strdup(tmp + 1); + *tmp = '\0'; + } + + tmp = strrchr(conn->dbName, '@'); + if (tmp != NULL) /* host name given */ + { + conn->pghost = strdup(tmp + 1); + *tmp = '\0'; + } + + conn->dbName = strdup(old); + free(old); + } + else + { + int offset; + + /* + * only allow protocols tcp and unix + */ + if (strncmp(conn->dbName, "tcp:", 4) == 0) + offset = 4; + else if (strncmp(conn->dbName, "unix:", 5) == 0) + offset = 5; + else return 0; + + if (strncmp(conn->dbName + offset, "postgresql://", strlen("postgresql://")) == 0) + { + /* new style: <tcp|unix>:postgresql://server[:port][/dbname][?options] */ + offset += strlen("postgresql://"); + + tmp = strrchr(conn->dbName + offset, '?'); + if (tmp != NULL) /* options given */ + { + conn->pgoptions = strdup(tmp + 1); + *tmp = '\0'; + } + + tmp = strrchr(conn->dbName + offset, '/'); + if (tmp != NULL) /* database name given */ + { + conn->dbName = strdup(tmp + 1); + *tmp = '\0'; + } + else + { + if ((tmp = getenv("PGDATABASE")) != NULL) + conn->dbName = strdup(tmp); + else if (conn->pguser) + conn->dbName = strdup(conn->pguser); + } + + tmp = strrchr(old + offset, ':'); + if (tmp != NULL) /* port number given */ + { + conn->pgport = strdup(tmp + 1); + *tmp = '\0'; + } + + if (strncmp(old, "unix:", 5) == 0) + { + conn->pghost = NULL; + if (strcmp(old + offset, "localhost") != 0) + { + (void) sprintf(conn->errorMessage, + "connectDB() -- non-tcp access only possible on localhost\n"); + return 1; + } + } + else conn->pghost = strdup(old + offset); + + free(old); + } + } + + return 0; +} + /* * connectDB - * make a connection to the backend so it is ready to receive queries. @@ -414,6 +509,12 @@ connectDB(PGconn *conn) char beresp; int on = 1; + /* + * parse dbName to get all additional info in it, if any + */ + if (update_db_info(conn) != 0) + goto connect_errReturn; + /* * Initialize the startup packet. */ -- GitLab