|
7 | 7 | *
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.104 1999/10/26 04:49:00 momjian Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.105 1999/11/05 06:43:45 tgl Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
@@ -854,50 +854,41 @@ void
|
854 | 854 | PQsetenv(PGconn *conn)
|
855 | 855 | {
|
856 | 856 | struct EnvironmentOptions *eo;
|
857 |
| - char setQuery[80]; /* mjl: size okay? XXX */ |
858 |
| - |
| 857 | + char setQuery[100]; /* note length limits in sprintf's below */ |
| 858 | + const char *val; |
| 859 | + PGresult *res; |
859 | 860 | #ifdef MULTIBYTE
|
860 | 861 | char *envname = "PGCLIENTENCODING";
|
861 |
| - static char envbuf[64]; /* big enough? */ |
862 |
| - char *env; |
863 |
| - char *encoding = 0; |
864 |
| - PGresult *rtn; |
865 |
| - |
866 |
| -#endif |
867 | 862 |
|
868 |
| -#ifdef MULTIBYTE |
869 |
| - /* query server encoding */ |
870 |
| - env = getenv(envname); |
871 |
| - if (!env || *env == '\0') |
| 863 | + /* Set env. variable PGCLIENTENCODING if it's not set already */ |
| 864 | + val = getenv(envname); |
| 865 | + if (!val || *val == '\0') |
872 | 866 | {
|
873 |
| - rtn = PQexec(conn, "select getdatabaseencoding()"); |
874 |
| - if (rtn && PQresultStatus(rtn) == PGRES_TUPLES_OK) |
| 867 | + const char *encoding = NULL; |
| 868 | + |
| 869 | + /* query server encoding */ |
| 870 | + res = PQexec(conn, "select getdatabaseencoding()"); |
| 871 | + if (res && PQresultStatus(res) == PGRES_TUPLES_OK) |
| 872 | + encoding = PQgetvalue(res, 0, 0); |
| 873 | + if (!encoding) /* this should not happen */ |
| 874 | + encoding = pg_encoding_to_char(MULTIBYTE); |
| 875 | + if (encoding) |
875 | 876 | {
|
876 |
| - encoding = PQgetvalue(rtn, 0, 0); |
877 |
| - if (encoding) |
878 |
| - { |
879 |
| - /* set client encoding */ |
880 |
| - sprintf(envbuf, "%s=%s", envname, encoding); |
881 |
| - putenv(envbuf); |
882 |
| - } |
883 |
| - } |
884 |
| - PQclear(rtn); |
885 |
| - if (!encoding) |
886 |
| - { /* this should not happen */ |
887 |
| - sprintf(envbuf, "%s=%s", envname, pg_encoding_to_char(MULTIBYTE)); |
| 877 | + /* set client encoding via environment variable */ |
| 878 | + char *envbuf; |
| 879 | + |
| 880 | + envbuf = (char *) malloc(strlen(envname) + strlen(encoding) + 2); |
| 881 | + sprintf(envbuf, "%s=%s", envname, encoding); |
888 | 882 | putenv(envbuf);
|
889 | 883 | }
|
| 884 | + PQclear(res); |
890 | 885 | }
|
891 | 886 | #endif
|
892 | 887 |
|
893 | 888 | for (eo = EnvironmentOptions; eo->envName; eo++)
|
894 | 889 | {
|
895 |
| - const char *val; |
896 |
| - |
897 | 890 | if ((val = getenv(eo->envName)))
|
898 | 891 | {
|
899 |
| - PGresult *res; |
900 |
| - |
901 | 892 | if (strcasecmp(val, "default") == 0)
|
902 | 893 | sprintf(setQuery, "SET %s = %.60s", eo->pgName, val);
|
903 | 894 | else
|
|
0 commit comments