From 8fc386a2d830c94dbf990e97297cb9ae75fa4453 Mon Sep 17 00:00:00 2001 From: Tatsuo Ishii Date: Sat, 15 Jan 2000 05:37:21 +0000 Subject: [PATCH] Eliminate using putenv(). --- src/interfaces/libpq/fe-connect.c | 27 ++++++++++++++++----------- src/interfaces/libpq/fe-print.c | 28 ++++++++++++++++++++-------- src/interfaces/libpq/libpq-fe.h | 10 +++++++--- src/interfaces/libpq/libpq-int.h | 4 +++- 4 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 1d2426e798..dc110b0fbc 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.109 2000/01/14 05:33:15 tgl Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.110 2000/01/15 05:37:21 ishii Exp $ * *------------------------------------------------------------------------- */ @@ -1299,7 +1299,7 @@ PQconnectPoll(PGconn *conn) these queries. */ conn->status = CONNECTION_OK; - switch (PQsetenvPoll(conn->setenv_handle)) + switch (PQsetenvPoll(conn)) { case PGRES_POLLING_OK: /* Success */ conn->status = CONNECTION_OK; @@ -1384,8 +1384,9 @@ PQsetenvStart(PGconn *conn) * ---------------- */ PostgresPollingStatusType -PQsetenvPoll(PGsetenvHandle handle) +PQsetenvPoll(PGconn *conn) { + PGsetenvHandle handle = conn->setenv_handle; #ifdef MULTIBYTE static const char envname[] = "PGCLIENTENCODING"; #endif @@ -1470,16 +1471,12 @@ PQsetenvPoll(PGsetenvHandle handle) encoding = PQgetvalue(handle->res, 0, 0); if (!encoding) /* this should not happen */ - encoding = pg_encoding_to_char(MULTIBYTE); + encoding = SQL_ASCII; if (encoding) { - /* set client encoding via environment variable */ - char *envbuf; - - envbuf = (char *) malloc(strlen(envname) + strlen(encoding) + 2); - sprintf(envbuf, "%s=%s", envname, encoding); - putenv(envbuf); + /* set client encoding to pg_conn struct */ + conn->client_encoding = atoi(encoding); } PQclear(handle->res); /* We have to keep going in order to clear up the query */ @@ -1630,7 +1627,7 @@ PQsetenv(PGconn *conn) return 0; for (;;) { - flag = PQsetenvPoll(handle); + flag = PQsetenvPoll(conn); switch (flag) { case PGRES_POLLING_ACTIVE: @@ -2355,6 +2352,14 @@ PQbackendPID(const PGconn *conn) return conn->be_pid; } +int +PQclientencoding(const PGconn *conn) +{ + if (!conn || conn->status != CONNECTION_OK) + return -1; + return conn->client_encoding; +} + void PQtrace(PGconn *conn, FILE *debug_port) { diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index 522e1c5700..d6f952d32b 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -9,7 +9,7 @@ * didn't really belong there. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.28 1999/11/11 00:10:14 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.29 2000/01/15 05:37:21 ishii Exp $ * *------------------------------------------------------------------------- */ @@ -498,27 +498,39 @@ PQprintTuples(const PGresult *res, * the backend is assumed. */ int -PQmblen(const unsigned char *s) +PQmblen(const unsigned char *s, int encoding) +{ + return (pg_encoding_mblen(encoding, s)); +} + +/* + * Get encoding id from environment variable PGCLIENTENCODING. + */ +int +PQenv2encoding(void) { char *str; - int encoding = -1; + int encoding = SQL_ASCII; str = getenv("PGCLIENTENCODING"); if (str && *str != '\0') encoding = pg_char_to_encoding(str); - if (encoding < 0) - encoding = MULTIBYTE; - return (pg_encoding_mblen(encoding, s)); + return(encoding); } #else /* Provide a default definition in case someone calls it anyway */ int -PQmblen(const unsigned char *s) +PQmblen(const unsigned char *s, int encoding) { return 1; } +int +PQenv2encoding(void) +{ + return 0; +} #endif /* MULTIBYTE */ @@ -560,7 +572,7 @@ do_field(const PQprintOpt *po, const PGresult *res, char ch = '0'; #ifdef MULTIBYTE - for (p = pval; *p; p += PQmblen(p)) + for (p = pval; *p; p += PQmblen(p, PQclientencoding(res->conn))) #else for (p = pval; *p; p++) #endif diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 719d64d5a6..ea07bf11cb 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -6,7 +6,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-fe.h,v 1.54 2000/01/14 05:33:15 tgl Exp $ + * $Id: libpq-fe.h,v 1.55 2000/01/15 05:37:21 ishii Exp $ * *------------------------------------------------------------------------- */ @@ -224,6 +224,7 @@ extern "C" extern const char *PQerrorMessage(const PGconn *conn); extern int PQsocket(const PGconn *conn); extern int PQbackendPID(const PGconn *conn); + extern int PQclientencoding(const PGconn *conn); /* Enable/disable tracing */ extern void PQtrace(PGconn *conn, FILE *debug_port); @@ -235,7 +236,7 @@ extern "C" /* Passing of environment variables */ /* Asynchronous (non-blocking) */ extern PGsetenvHandle PQsetenvStart(PGconn *conn); - extern PostgresPollingStatusType PQsetenvPoll(PGsetenvHandle handle); + extern PostgresPollingStatusType PQsetenvPoll(PGconn *conn); extern void PQsetenvAbort(PGsetenvHandle handle); /* Synchronous (blocking) */ @@ -333,7 +334,10 @@ extern "C" * 0, use variable width */ /* Determine length of multibyte encoded char at *s */ - extern int PQmblen(const unsigned char *s); + extern int PQmblen(const unsigned char *s, int encoding); + + /* Get encoding id from environment variable PGCLIENTENCODING */ + int PQenv2encoding(void); /* === in fe-lobj.c === */ diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index 779371fbda..b310beb4c4 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -11,7 +11,7 @@ * * Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-int.h,v 1.15 2000/01/14 05:33:15 tgl Exp $ + * $Id: libpq-int.h,v 1.16 2000/01/15 05:37:21 ishii Exp $ * *------------------------------------------------------------------------- */ @@ -236,6 +236,8 @@ struct pg_conn /* Buffer for receiving various parts of messages */ PQExpBufferData workBuffer; /* expansible string */ + + int client_encoding; /* encoding id */ }; /* ----------------