Eliminate using putenv().
authorTatsuo Ishii
Sat, 15 Jan 2000 05:37:21 +0000 (05:37 +0000)
committerTatsuo Ishii
Sat, 15 Jan 2000 05:37:21 +0000 (05:37 +0000)
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/fe-print.c
src/interfaces/libpq/libpq-fe.h
src/interfaces/libpq/libpq-int.h

index 1d2426e798246740d1f08ea8ead04161cc873e79..dc110b0fbced35147f7234695ca70889ba3e5b19 100644 (file)
@@ -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)
 {
index 522e1c57006eb3fc8517e4db8880fa703ece057d..d6f952d32bd2616ee44279443f34c7e52f50047f 100644 (file)
@@ -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
index 719d64d5a6a581b09c88fe42c89033151943483f..ea07bf11cbd06197d532be6069865027431b1aed 100644 (file)
@@ -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 === */
 
index 779371fbda2dc72c05aa1f8dffd7c490d29c116e..b310beb4c4d36d16295675d9b32a955803af42a5 100644 (file)
@@ -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 */
 };
 
 /* ----------------