From a0bf2503ea0d9a1a2208dd3cf74727bcda7e69d2 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Thu, 3 Oct 2002 17:09:42 +0000 Subject: [PATCH] The attached patch fixes a number of issues related to compiling the client utilities (libpq.dll and psql.exe) for win32 (missing defines, adjustments to includes, pedantic casting, non-existent functions) per: http://developer.postgresql.org/docs/postgres/install-win32.html. It compiles cleanly under Windows 2000 using Visual Studio .net. Also compiles clean and passes all regression tests (regular and contrib) under Linux. In addition to a review by the usual suspects, it would be very desirable for someone well versed in the peculiarities of win32 to take a look. Joe Conway --- src/backend/libpq/md5.c | 15 ++++++++--- src/bin/psql/command.c | 9 ++++--- src/bin/psql/common.c | 30 +++++++++++++++++---- src/bin/psql/copy.c | 4 ++- src/bin/psql/large_obj.c | 4 +-- src/bin/psql/mbprint.c | 4 +-- src/bin/psql/print.c | 8 +++--- src/include/pg_config.h.win32 | 6 +++++ src/interfaces/libpq/fe-connect.c | 43 +++++++++++------------------- src/interfaces/libpq/fe-misc.c | 8 +++--- src/interfaces/libpq/fe-print.c | 4 +-- src/interfaces/libpq/libpq-int.h | 6 +++-- src/interfaces/libpq/libpqdll.def | 5 +++- src/interfaces/libpq/pqexpbuffer.c | 6 ++--- src/interfaces/libpq/win32.h | 2 +- src/utils/Makefile | 4 +-- 16 files changed, 94 insertions(+), 64 deletions(-) diff --git a/src/backend/libpq/md5.c b/src/backend/libpq/md5.c index 5c12e70c7f..dbf639fc74 100644 --- a/src/backend/libpq/md5.c +++ b/src/backend/libpq/md5.c @@ -14,7 +14,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/libpq/md5.c,v 1.18 2002/09/04 20:31:19 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/libpq/md5.c,v 1.19 2002/10/03 17:09:41 momjian Exp $ */ @@ -26,10 +26,19 @@ * can be compiled stand-alone. */ -#ifndef MD5_ODBC +#if ! defined(MD5_ODBC) && ! defined(FRONTEND) #include "postgres.h" #include "libpq/crypt.h" -#else +#endif + +#ifdef FRONTEND +#include "postgres_fe.h" +#ifndef WIN32 +#include "libpq/crypt.h" +#endif /* WIN32 */ +#endif /* FRONTEND */ + +#ifdef MD5_ODBC #include "md5.h" #endif diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 32a94bad7f..a9477c68ed 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -3,7 +3,7 @@ * * Copyright 2000-2002 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.81 2002/09/22 20:57:21 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.82 2002/10/03 17:09:41 momjian Exp $ */ #include "postgres_fe.h" #include "command.h" @@ -23,6 +23,7 @@ #include #include #include +#include #endif #include "libpq-fe.h" @@ -1163,7 +1164,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon) return NULL; } - if (i < token_len - 1) + if (i < (int) token_len - 1) return_val[i + 1] = '\0'; } @@ -1240,7 +1241,7 @@ unescape(const unsigned char *source, size_t len) exit(EXIT_FAILURE); } - for (p = source; p - source < len && *p; p += PQmblen(p, pset.encoding)) + for (p = source; p - source < (int) len && *p; p += PQmblen(p, pset.encoding)) { if (esc) { @@ -1278,7 +1279,7 @@ unescape(const unsigned char *source, size_t len) char *end; l = strtol(p, &end, 0); - c = l; + c = (char) l; p = end - 1; break; } diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index a636372b02..c451640ee0 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.45 2002/09/14 19:46:01 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.46 2002/10/03 17:09:41 momjian Exp $ */ #include "postgres_fe.h" @@ -11,17 +11,18 @@ #include #include -#include #ifndef HAVE_STRDUP #include #endif #include #ifndef WIN32 +#include #include /* for write() */ #include #else #include /* for _write() */ #include +#include /* for _ftime() */ #endif #include "libpq-fe.h" @@ -295,9 +296,13 @@ SendQuery(const char *query) bool success = false; PGresult *results; PGnotify *notify; +#ifndef WIN32 struct timeval before, after; - struct timezone tz; +#else + struct _timeb before, + after; +#endif if (!pset.db) { @@ -327,11 +332,21 @@ SendQuery(const char *query) } cancelConn = pset.db; + +#ifndef WIN32 if (pset.timing) - gettimeofday(&before, &tz); + gettimeofday(&before, NULL); results = PQexec(pset.db, query); if (pset.timing) - gettimeofday(&after, &tz); + gettimeofday(&after, NULL); +#else + if (pset.timing) + _ftime(&before); + results = PQexec(pset.db, query); + if (pset.timing) + _ftime(&after); +#endif + if (PQresultStatus(results) == PGRES_COPY_IN) copy_in_state = true; /* keep cancel connection for copy out state */ @@ -463,8 +478,13 @@ SendQuery(const char *query) /* Possible microtiming output */ if (pset.timing && success) +#ifndef WIN32 printf(gettext("Time: %.2f ms\n"), ((after.tv_sec - before.tv_sec) * 1000000.0 + after.tv_usec - before.tv_usec) / 1000.0); +#else + printf(gettext("Time: %.2f ms\n"), + ((after.time - before.time) * 1000.0 + after.millitm - before.millitm)); +#endif return success; } diff --git a/src/bin/psql/copy.c b/src/bin/psql/copy.c index 74394d219b..926641f5f4 100644 --- a/src/bin/psql/copy.c +++ b/src/bin/psql/copy.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.25 2002/09/22 20:57:21 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.26 2002/10/03 17:09:41 momjian Exp $ */ #include "postgres_fe.h" #include "copy.h" @@ -28,6 +28,8 @@ #ifdef WIN32 #define strcasecmp(x,y) stricmp(x,y) +#define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask)) +#define S_ISDIR(mode) __S_ISTYPE((mode), S_IFDIR) #endif bool copy_in_state; diff --git a/src/bin/psql/large_obj.c b/src/bin/psql/large_obj.c index 2bca9da05e..9bf51622cc 100644 --- a/src/bin/psql/large_obj.c +++ b/src/bin/psql/large_obj.c @@ -3,7 +3,7 @@ * * Copyright 2000-2002 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.21 2002/09/04 20:31:36 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/large_obj.c,v 1.22 2002/10/03 17:09:41 momjian Exp $ */ #include "postgres_fe.h" #include "large_obj.h" @@ -196,7 +196,7 @@ do_lo_import(const char *filename_arg, const char *comment_arg) { char *cmdbuf; char *bufptr; - int slen = strlen(comment_arg); + size_t slen = strlen(comment_arg); cmdbuf = malloc(slen * 2 + 256); if (!cmdbuf) diff --git a/src/bin/psql/mbprint.c b/src/bin/psql/mbprint.c index dcd305d921..c91b71c9c5 100644 --- a/src/bin/psql/mbprint.c +++ b/src/bin/psql/mbprint.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/mbprint.c,v 1.4 2002/08/27 20:16:48 petere Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/mbprint.c,v 1.5 2002/10/03 17:09:42 momjian Exp $ */ #include "postgres_fe.h" @@ -202,7 +202,7 @@ mb_utf_wcswidth(unsigned char *pwcs, size_t len) for (; *pwcs && len > 0; pwcs += l) { l = pg_utf_mblen(pwcs); - if ((len < l) || ((w = ucs_wcwidth(utf2ucs(pwcs))) < 0)) + if ((len < (size_t) l) || ((w = ucs_wcwidth(utf2ucs(pwcs))) < 0)) return width; len -= l; width += w; diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c index 3aefbb5b4c..f74fedce07 100644 --- a/src/bin/psql/print.c +++ b/src/bin/psql/print.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.31 2002/09/01 23:30:46 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/print.c,v 1.32 2002/10/03 17:09:42 momjian Exp $ */ #include "postgres_fe.h" #include "print.h" @@ -282,7 +282,7 @@ print_aligned_text(const char *title, const char *const * headers, { int tlen; - if ((tlen = pg_wcswidth((unsigned char *) title, strlen(title))) >= total_w) + if ((unsigned int) (tlen = pg_wcswidth((unsigned char *) title, strlen(title))) >= total_w) fprintf(fout, "%s\n", title); else fprintf(fout, "%-*s%s\n", (int) (total_w - tlen) / 2, "", title); @@ -1184,8 +1184,8 @@ printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout) footers ? (const char *const *) footers : (const char *const *) (opt->footers), align, &opt->topt, fout); - free(headers); - free(cells); + free((void *) headers); + free((void *) cells); if (footers) { free(footers[0]); diff --git a/src/include/pg_config.h.win32 b/src/include/pg_config.h.win32 index 9f8fe46522..aaeecf5957 100644 --- a/src/include/pg_config.h.win32 +++ b/src/include/pg_config.h.win32 @@ -16,6 +16,8 @@ #define MAXPGPATH 1024 +#define INDEX_MAX_KEYS 32 + #define HAVE_ATEXIT #define HAVE_MEMMOVE @@ -50,4 +52,8 @@ #endif +#ifndef __CYGWIN__ +#include +#endif + #endif /* pg_config_h_win32__ */ diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index a51ade9bd2..cbb29fa4a5 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.205 2002/09/22 20:57:21 petere Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.206 2002/10/03 17:09:42 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -21,7 +21,6 @@ #include #include #include -#include #include "libpq-fe.h" #include "libpq-int.h" @@ -1053,10 +1052,10 @@ connectDBComplete(PGconn *conn) { PostgresPollingStatusType flag = PGRES_POLLING_WRITING; - struct timeval remains, - *rp = NULL, - finish_time, - start_time; + time_t finish_time = 0, + current_time; + struct timeval remains, + *rp = NULL; if (conn == NULL || conn->status == CONNECTION_BAD) return 0; @@ -1074,19 +1073,13 @@ connectDBComplete(PGconn *conn) } remains.tv_usec = 0; rp = &remains; + + /* calculate the finish time based on start + timeout */ + finish_time = time((time_t *) NULL) + remains.tv_sec; } while (rp == NULL || remains.tv_sec > 0 || remains.tv_usec > 0) { - /* - * If connecting timeout is set, get current time. - */ - if (rp != NULL && gettimeofday(&start_time, NULL) == -1) - { - conn->status = CONNECTION_BAD; - return 0; - } - /* * Wait, if necessary. Note that the initial state (just after * PQconnectStart) is to wait for the socket to select for @@ -1128,26 +1121,18 @@ connectDBComplete(PGconn *conn) flag = PQconnectPoll(conn); /* - * If connecting timeout is set, calculate remain time. + * If connecting timeout is set, calculate remaining time. */ if (rp != NULL) { - if (gettimeofday(&finish_time, NULL) == -1) + if (time(¤t_time) == -1) { conn->status = CONNECTION_BAD; return 0; } - if ((finish_time.tv_usec -= start_time.tv_usec) < 0) - { - remains.tv_sec++; - finish_time.tv_usec += 1000000; - } - if ((remains.tv_usec -= finish_time.tv_usec) < 0) - { - remains.tv_sec--; - remains.tv_usec += 1000000; - } - remains.tv_sec -= finish_time.tv_sec - start_time.tv_sec; + + remains.tv_sec = finish_time - current_time; + remains.tv_usec = 0; } } conn->status = CONNECTION_BAD; @@ -2946,6 +2931,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username) return NULL; } +#ifndef WIN32 /* If password file is insecure, alert the user and ignore it. */ if (stat_buf.st_mode & (S_IRWXG | S_IRWXO)) { @@ -2955,6 +2941,7 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username) free(pgpassfile); return NULL; } +#endif fp = fopen(pgpassfile, "r"); free(pgpassfile); diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index 794e757d56..ad41cf749d 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -25,7 +25,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.79 2002/09/04 20:31:47 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.80 2002/10/03 17:09:42 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -150,9 +150,9 @@ pqPutBytes(const char *s, size_t nbytes, PGconn *conn) * try to grow the buffer. FIXME: The new size could be * chosen more intelligently. */ - size_t buflen = conn->outCount + nbytes; + size_t buflen = (size_t) conn->outCount + nbytes; - if (buflen > conn->outBufSize) + if (buflen > (size_t) conn->outBufSize) { char *newbuf = realloc(conn->outBuffer, buflen); @@ -240,7 +240,7 @@ pqPuts(const char *s, PGconn *conn) int pqGetnchar(char *s, size_t len, PGconn *conn) { - if (len < 0 || len > conn->inEnd - conn->inCursor) + if (len < 0 || len > (size_t) (conn->inEnd - conn->inCursor)) return EOF; memcpy(s, conn->inBuffer + conn->inCursor, len); diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index baa9700934..2dfdc5641c 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -10,7 +10,7 @@ * didn't really belong there. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.46 2002/08/29 07:22:30 ishii Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.47 2002/10/03 17:09:42 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -299,7 +299,7 @@ PQprint(FILE *fout, (PQntuples(res) == 1) ? "" : "s"); free(fieldMax); free(fieldNotNum); - free(fieldNames); + free((void *) fieldNames); if (usePipe) { #ifdef WIN32 diff --git a/src/interfaces/libpq/libpq-int.h b/src/interfaces/libpq/libpq-int.h index daf2f9ae0b..cdf978e5b1 100644 --- a/src/interfaces/libpq/libpq-int.h +++ b/src/interfaces/libpq/libpq-int.h @@ -12,7 +12,7 @@ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Id: libpq-int.h,v 1.57 2002/09/04 20:31:47 momjian Exp $ + * $Id: libpq-int.h,v 1.58 2002/10/03 17:09:42 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -21,8 +21,10 @@ #define LIBPQ_INT_H #include -#include #include +#ifndef WIN32 +#include +#endif #if defined(WIN32) && (!defined(ssize_t)) typedef int ssize_t; /* ssize_t doesn't exist in VC (atleast diff --git a/src/interfaces/libpq/libpqdll.def b/src/interfaces/libpq/libpqdll.def index b75eb20fa6..524a7468e8 100644 --- a/src/interfaces/libpq/libpqdll.def +++ b/src/interfaces/libpq/libpqdll.def @@ -1,5 +1,4 @@ LIBRARY LIBPQ -DESCRIPTION "Postgres Client Access Library" EXPORTS PQconnectdb @ 1 PQsetdbLogin @ 2 @@ -90,3 +89,7 @@ EXPORTS PQfreeNotify @ 87 PQescapeString @ 88 PQescapeBytea @ 89 + printfPQExpBuffer @ 90 + appendPQExpBuffer @ 91 + pg_encoding_to_char @ 92 + pg_utf_mblen @ 93 diff --git a/src/interfaces/libpq/pqexpbuffer.c b/src/interfaces/libpq/pqexpbuffer.c index 9d679bb726..4bd78b1874 100644 --- a/src/interfaces/libpq/pqexpbuffer.c +++ b/src/interfaces/libpq/pqexpbuffer.c @@ -17,7 +17,7 @@ * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $Header: /cvsroot/pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.13 2002/06/20 20:29:54 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.14 2002/10/03 17:09:42 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -192,7 +192,7 @@ printfPQExpBuffer(PQExpBuffer str, const char *fmt,...) * actually stored, but at least one returns -1 on failure. Be * conservative about believing whether the print worked. */ - if (nprinted >= 0 && nprinted < avail - 1) + if (nprinted >= 0 && nprinted < (int) avail - 1) { /* Success. Note nprinted does not include trailing null. */ str->len += nprinted; @@ -240,7 +240,7 @@ appendPQExpBuffer(PQExpBuffer str, const char *fmt,...) * actually stored, but at least one returns -1 on failure. Be * conservative about believing whether the print worked. */ - if (nprinted >= 0 && nprinted < avail - 1) + if (nprinted >= 0 && nprinted < (int) avail - 1) { /* Success. Note nprinted does not include trailing null. */ str->len += nprinted; diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h index db1aa53b24..4e8a2141d5 100644 --- a/src/interfaces/libpq/win32.h +++ b/src/interfaces/libpq/win32.h @@ -22,7 +22,7 @@ /* * crypt not available (yet) */ -#define crypt(a,b) (a) +#define crypt(a,b) ((char *) a) #undef EAGAIN /* doesn't apply on sockets */ #undef EINTR diff --git a/src/utils/Makefile b/src/utils/Makefile index 26273cd4bd..ef9e121c08 100644 --- a/src/utils/Makefile +++ b/src/utils/Makefile @@ -2,7 +2,7 @@ # # Makefile for utils # -# $Header: /cvsroot/pgsql/src/utils/Attic/Makefile,v 1.14 2002/07/27 20:10:05 petere Exp $ +# $Header: /cvsroot/pgsql/src/utils/Attic/Makefile,v 1.15 2002/10/03 17:09:42 momjian Exp $ # # dllinit.o is only built on Win32 platform. # @@ -15,4 +15,4 @@ include $(top_builddir)/src/Makefile.global all: clean distclean maintainer-clean: - rm -f dllinit.o + rm -f dllinit.o getopt.o