From c765b4b052262416a2212ba206d9669f88ca68b9 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Fri, 3 Jul 1998 04:24:16 +0000 Subject: [PATCH] Hello! Through some minor changes, I have been able to compile the libpq client libraries on the Win32 platform. Since the libpq communications part has been rewritten, this has become much easier. Enclosed is a patch that will allow at least Microsoft Visual C++ to compile libpq into both a static and a dynamic library. I will take a look at porting the psql frontend as well, but I figured it was a good idea to send in these patches first - so no major changes are done to the files before it gets applied (if it does). Regards, Magnus Hagander --- src/include/postgres.h | 4 +- src/interfaces/libpq/fe-auth.c | 14 +++- src/interfaces/libpq/fe-connect.c | 36 ++++++++-- src/interfaces/libpq/fe-exec.c | 5 +- src/interfaces/libpq/fe-lobj.c | 7 +- src/interfaces/libpq/fe-misc.c | 10 ++- src/interfaces/libpq/fe-print.c | 27 ++++++- src/interfaces/libpq/libpqdll.c | 6 ++ src/interfaces/libpq/libpqdll.def | 73 +++++++++++++++++++ src/interfaces/libpq/win32.h | 35 ++++++++++ src/interfaces/libpq/win32.mak | 112 ++++++++++++++++++++++++++++++ src/win32.mak | 17 +++++ 12 files changed, 334 insertions(+), 12 deletions(-) create mode 100644 src/interfaces/libpq/libpqdll.c create mode 100644 src/interfaces/libpq/libpqdll.def create mode 100644 src/interfaces/libpq/win32.h create mode 100644 src/interfaces/libpq/win32.mak create mode 100644 src/win32.mak diff --git a/src/include/postgres.h b/src/include/postgres.h index 82806f0f31..f6cbc47c55 100644 --- a/src/include/postgres.h +++ b/src/include/postgres.h @@ -6,7 +6,7 @@ * * Copyright (c) 1995, Regents of the University of California * - * $Id: postgres.h,v 1.16 1998/04/26 04:08:18 momjian Exp $ + * $Id: postgres.h,v 1.17 1998/07/03 04:24:10 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -37,7 +37,9 @@ #define POSTGRES_H #include "postgres_ext.h" +#ifndef WIN32 #include "config.h" +#endif #include "c.h" #include "utils/elog.h" #include "utils/palloc.h" diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c index a5d842d905..022c5cb386 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -7,7 +7,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.17 1998/06/15 19:30:22 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-auth.c,v 1.18 1998/07/03 04:24:11 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -24,6 +24,9 @@ * * */ +#ifdef WIN32 +#include "win32.h" +#else #include #include #include /* for MAXHOSTNAMELEN on most */ @@ -33,6 +36,7 @@ #endif #include #include +#endif /* WIN32 */ #include "postgres.h" @@ -600,10 +604,18 @@ fe_getauthname(char *PQerrormsg) #endif case STARTUP_MSG: { +#ifdef WIN32 + char username[128]; + DWORD namesize = sizeof(username) - 1; + + if (GetUserName(username,&namesize)) + name = username; +#else struct passwd *pw = getpwuid(geteuid()); if (pw) name = pw->pw_name; +#endif } break; default: diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index b95f86b02b..ba886d4c64 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.69 1998/06/21 16:39:11 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.70 1998/07/03 04:24:12 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -407,6 +407,7 @@ connectDB(PGconn *conn) family, len; char beresp; + int on = 1; /* * Initialize the startup packet. @@ -456,8 +457,11 @@ connectDB(PGconn *conn) conn->raddr.in.sin_port = htons((unsigned short) (portno)); len = sizeof(struct sockaddr_in); } +#ifndef WIN32 else len = UNIXSOCK_PATH(conn->raddr.un, portno); +#endif + /* Connect to the server */ if ((conn->sock = socket(family, SOCK_STREAM, 0)) < 0) @@ -482,7 +486,11 @@ connectDB(PGconn *conn) * We need nonblocking I/O, and we don't want delay of outgoing data. */ +#ifndef WIN32 if (fcntl(conn->sock, F_SETFL, O_NONBLOCK) < 0) +#else + if (ioctlsocket(conn->sock,FIONBIO, &on) != 0) +#endif { (void) sprintf(conn->errorMessage, "connectDB() -- fcntl() failed: errno=%d\n%s\n", @@ -493,7 +501,6 @@ connectDB(PGconn *conn) if (family == AF_INET) { struct protoent *pe; - int on = 1; pe = getprotobyname("TCP"); if (pe == NULL) @@ -503,11 +510,18 @@ connectDB(PGconn *conn) goto connect_errReturn; } if (setsockopt(conn->sock, pe->p_proto, TCP_NODELAY, - &on, sizeof(on)) < 0) +#ifdef WIN32 + (char *) +#endif + &on, + sizeof(on)) < 0) { (void) sprintf(conn->errorMessage, "connectDB() -- setsockopt failed: errno=%d\n%s\n", errno, strerror(errno)); +#ifdef WIN32 + printf("Winsock error: %i\n",WSAGetLastError()); +#endif goto connect_errReturn; } } @@ -666,7 +680,11 @@ connectDB(PGconn *conn) connect_errReturn: if (conn->sock >= 0) { +#ifdef WIN32 + closesocket(conn->sock); +#else close(conn->sock); +#endif conn->sock = -1; } return CONNECTION_BAD; @@ -742,7 +760,11 @@ freePGconn(PGconn *conn) return; PQclearAsyncResult(conn); /* deallocate result and curTuple */ if (conn->sock >= 0) - close(conn->sock); /* shouldn't happen, but... */ +#ifdef WIN32 + closesocket(conn->sock); +#else + close(conn->sock); +#endif if (conn->pghost) free(conn->pghost); if (conn->pgport) @@ -783,6 +805,7 @@ closePGconn(PGconn *conn) * If connection is already gone, that's cool. No reason for kernel * to kill us when we try to write to it. So ignore SIGPIPE signals. */ +#ifndef WIN32 #if defined(USE_POSIX_SIGNALS) struct sigaction ignore_action; struct sigaction oldaction; @@ -806,13 +829,18 @@ closePGconn(PGconn *conn) signal(SIGPIPE, oldsignal); #endif +#endif /* Win32 uses no signals at all */ } /* * Close the connection, reset all transient state, flush I/O buffers. */ if (conn->sock >= 0) +#ifdef WIN32 + closesocket(conn->sock); +#else close(conn->sock); +#endif conn->sock = -1; conn->status = CONNECTION_BAD; /* Well, not really _bad_ - just * absent */ diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 506edc8b1a..49bd6d07e5 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -7,10 +7,13 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.54 1998/06/16 07:29:48 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.55 1998/07/03 04:24:13 momjian Exp $ * *------------------------------------------------------------------------- */ +#ifdef WIN32 +#include "win32.h" +#endif #include #include #include diff --git a/src/interfaces/libpq/fe-lobj.c b/src/interfaces/libpq/fe-lobj.c index d18a4d53c1..a59815fc52 100644 --- a/src/interfaces/libpq/fe-lobj.c +++ b/src/interfaces/libpq/fe-lobj.c @@ -7,11 +7,16 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.13 1998/06/15 19:30:26 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.14 1998/07/03 04:24:14 momjian Exp $ * *------------------------------------------------------------------------- */ +#ifdef WIN32 +#include "win32.h" +#include +#else #include +#endif #include #include #include diff --git a/src/interfaces/libpq/fe-misc.c b/src/interfaces/libpq/fe-misc.c index e79460798d..52bf28b612 100644 --- a/src/interfaces/libpq/fe-misc.c +++ b/src/interfaces/libpq/fe-misc.c @@ -24,7 +24,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.15 1998/06/15 19:30:26 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.16 1998/07/03 04:24:14 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -34,10 +34,14 @@ #include #include #include +#ifdef WIN32 +#include "win32.h" +#else #include #if !defined(NO_UNISTD_H) #include #endif +#endif /* WIN32 */ #include /* for fd_set stuff */ #ifdef HAVE_SYS_SELECT_H #include @@ -412,7 +416,11 @@ tryAgain2: " before or while processing the request.\n"); conn->status = CONNECTION_BAD; /* No more connection to * backend */ +#ifdef WIN32 + closesocket(conn->sock); +#else close(conn->sock); +#endif conn->sock = -1; return -1; diff --git a/src/interfaces/libpq/fe-print.c b/src/interfaces/libpq/fe-print.c index ae820561c0..586ec370aa 100644 --- a/src/interfaces/libpq/fe-print.c +++ b/src/interfaces/libpq/fe-print.c @@ -9,24 +9,31 @@ * didn't really belong there. * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.5 1998/06/16 07:29:49 momjian Exp $ + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.6 1998/07/03 04:24:15 momjian Exp $ * *------------------------------------------------------------------------- */ +#ifdef WIN32 +#include "win32.h" +#endif #include #include #include #include #include +#ifndef WIN32 #include #include +#endif #include "libpq/pqsignal.h" #include "libpq-fe.h" +#ifndef WIN32 #ifndef HAVE_TERMIOS_H #include #else #include #endif +#endif /* WIN32 */ #ifdef MB #include "regex/pg_wchar.h" @@ -143,9 +150,13 @@ PQprint(FILE *fout, if (fout == NULL) fout = stdout; - if (po->pager && fout == stdout && + if (po->pager && fout == stdout +#ifndef WIN32 + && isatty(fileno(stdin)) && - isatty(fileno(stdout))) + isatty(fileno(stdout)) +#endif + ) { /* try to pipe to the pager program if possible */ #ifdef TIOCGWINSZ @@ -174,11 +185,17 @@ PQprint(FILE *fout, - (po->header != 0) * 2 /* row count and newline */ ))) { +#ifdef WIN32 + fout = _popen(pagerenv, "w"); +#else fout = popen(pagerenv, "w"); +#endif if (fout) { usePipe = 1; +#ifndef WIN32 pqsignal(SIGPIPE, SIG_IGN); +#endif } else fout = stdout; @@ -289,8 +306,12 @@ PQprint(FILE *fout, free(fieldNames); if (usePipe) { +#ifdef WIN32 + _pclose(fout); +#else pclose(fout); pqsignal(SIGPIPE, SIG_DFL); +#endif } if (po->html3 && !po->expanded) fputs("\n", fout); diff --git a/src/interfaces/libpq/libpqdll.c b/src/interfaces/libpq/libpqdll.c new file mode 100644 index 0000000000..aa67806657 --- /dev/null +++ b/src/interfaces/libpq/libpqdll.c @@ -0,0 +1,6 @@ +#define WIN32_LEAN_AND_MEAN +#include +BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, + LPVOID lpReserved ){ + return (TRUE); +} diff --git a/src/interfaces/libpq/libpqdll.def b/src/interfaces/libpq/libpqdll.def new file mode 100644 index 0000000000..0a46f8273c --- /dev/null +++ b/src/interfaces/libpq/libpqdll.def @@ -0,0 +1,73 @@ +LIBRARY LIBPQ +DESCRIPTION "Postgres Client Access Library" +EXPORTS + PQconnectdb @ 1 + PQconndefaults @ 2 + PQsetdbLogin @ 3 + PQfinish @ 4 + PQreset @ 5 + PQdb @ 6 + PQuser @ 7 + PQhost @ 8 + PQoptions @ 9 + PQport @ 10 + PQtty @ 11 + PQstatus @ 12 + PQerrorMessage @ 13 + PQsocket @ 14 + PQtrace @ 15 + PQuntrace @ 16 + PQexec @ 17 + PQnotifies @ 18 + PQsendQuery @ 19 + PQgetResult @ 20 + PQisBusy @ 21 + PQconsumeInput @ 22 + PQrequestCancel @ 23 + PQgetline @ 24 + PQputline @ 25 + PQendcopy @ 26 + PQfn @ 27 + PQclearAsyncResult @ 28 + PQresultStatus @ 29 + PQntuples @ 30 + PQnfields @ 31 + PQfname @ 32 + PQfnumber @ 33 + PQftype @ 34 + PQfsize @ 35 + PQfmod @ 36 + PQcmdStatus @ 37 + PQoidStatus @ 38 + PQcmdTuples @ 39 + PQgetvalue @ 40 + PQgetlength @ 41 + PQgetisnull @ 42 + PQclear @ 43 + PQprint @ 44 + PQdisplayTuples @ 45 + PQprintTuples @ 46 + fe_getauthsvc @ 47 + fe_setauthsvc @ 48 + fe_getauthname @ 49 + pqGetc @ 50 + pqGets @ 51 + pqPuts @ 52 + pqGetnchar @ 53 + pqPutnchar @ 54 + pqGetInt @ 55 + pqPutInt @ 56 + pqReadData @ 57 + pqFlush @ 58 + pqWait @ 59 + lo_open @ 60 + lo_close @ 61 + lo_read @ 62 + lo_write @ 63 + lo_lseek @ 64 + lo_creat @ 65 + lo_tell @ 66 + lo_unlink @ 67 + lo_import @ 68 + lo_export @ 69 + diff --git a/src/interfaces/libpq/win32.h b/src/interfaces/libpq/win32.h new file mode 100644 index 0000000000..878c7afbd4 --- /dev/null +++ b/src/interfaces/libpq/win32.h @@ -0,0 +1,35 @@ +#include + +/* + * strcasecmp() is not in Windows, stricmp is, though + */ +#define strcasecmp(a,b) stricmp(a,b) + + + +#define NO_UNISTD_H + + +/* + * Some compat functions + */ +#define open(a,b,c) _open(a,b,c) +#define read(a,b,c) _read(a,b,c) +#define write(a,b,c) _write(a,b,c) + + +/* + * crypt not available (yet) + */ +#define crypt(a,b) a + + + +/* + * Parts of config.h that you get with autoconf on other systems + */ + +/* + * Default port to connect to + */ +#define DEF_PGPORT "5432" diff --git a/src/interfaces/libpq/win32.mak b/src/interfaces/libpq/win32.mak new file mode 100644 index 0000000000..4c50caf5e4 --- /dev/null +++ b/src/interfaces/libpq/win32.mak @@ -0,0 +1,112 @@ +# Makefile for Microsoft Visual C++ 5.0 (or compat) + +# Will build a Win32 static library (non-debug) libpq.lib +# and a Win32 dynamic library (non-debug) libpq.dll with import library libpqdll.lib + + +!IF "$(OS)" == "Windows_NT" +NULL= +!ELSE +NULL=nul +!ENDIF + +CPP=cl.exe + +OUTDIR=.\Release +INTDIR=.\Release +# Begin Custom Macros +OutDir=.\Release +# End Custom Macros + +ALL : "$(OUTDIR)\libpq.lib" "$(OUTDIR)\libpq.dll" + +CLEAN : + -@erase "$(INTDIR)\dllist.obj" + -@erase "$(INTDIR)\fe-auth.obj" + -@erase "$(INTDIR)\fe-connect.obj" + -@erase "$(INTDIR)\fe-exec.obj" + -@erase "$(INTDIR)\fe-lobj.obj" + -@erase "$(INTDIR)\fe-misc.obj" + -@erase "$(INTDIR)\fe-print.obj" + -@erase "$(OUTDIR)\libpqdll.obj" + -@erase "$(INTDIR)\vc50.idb" + -@erase "$(OUTDIR)\libpq.lib" + -@erase "$(OUTDIR)\libpq.dll" + +"$(OUTDIR)" : + if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)" + +CPP_PROJ=/nologo /ML /W3 /GX /O2 /I "..\..\include" /D "NDEBUG" /D\ + "WIN32" /D "_WINDOWS" /Fp"$(INTDIR)\libpq.pch" /YX\ + /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\" /FD /c +CPP_OBJS=.\Release/ +CPP_SBRS=. + +LIB32=link.exe -lib +LIB32_FLAGS=/nologo /out:"$(OUTDIR)\libpq.lib" +LIB32_OBJS= \ + "$(INTDIR)\dllist.obj" \ + "$(INTDIR)\fe-auth.obj" \ + "$(INTDIR)\fe-connect.obj" \ + "$(INTDIR)\fe-exec.obj" \ + "$(INTDIR)\fe-lobj.obj" \ + "$(INTDIR)\fe-misc.obj" \ + "$(INTDIR)\fe-print.obj" + +LINK32=link.exe +LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\ + advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib wsock32.lib\ + odbccp32.lib /nologo /subsystem:windows /dll /incremental:no\ + /pdb:"$(OUTDIR)\libpqdll.pdb" /machine:I386 /out:"$(OUTDIR)\libpq.dll"\ + /implib:"$(OUTDIR)\libpqdll.lib" /def:libpqdll.def +LINK32_OBJS= \ + "$(INTDIR)\libpqdll.obj" \ + "$(OUTDIR)\libpq.lib" + + +"$(OUTDIR)\libpq.lib" : "$(OUTDIR)" $(DEF_FILE) $(LIB32_OBJS) + $(LIB32) @<< + $(LIB32_FLAGS) $(DEF_FLAGS) $(LIB32_OBJS) +<< + +"$(OUTDIR)\libpq.dll" : "$(OUTDIR)" "$(OUTDIR)\libpqdll.obj" "$(INTDIR)\libpqdll.obj" + $(LINK32) @<< + $(LINK32_FLAGS) $(LINK32_OBJS) +<< + + +"$(OUTDIR)\dllist.obj" : ..\..\backend\lib\dllist.c + $(CPP) @<< + $(CPP_PROJ) ..\..\backend\lib\dllist.c +<< + + +.c{$(CPP_OBJS)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(CPP_OBJS)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(CPP_OBJS)}.obj:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.c{$(CPP_SBRS)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cpp{$(CPP_SBRS)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< + +.cxx{$(CPP_SBRS)}.sbr:: + $(CPP) @<< + $(CPP_PROJ) $< +<< diff --git a/src/win32.mak b/src/win32.mak new file mode 100644 index 0000000000..fbfc35048a --- /dev/null +++ b/src/win32.mak @@ -0,0 +1,17 @@ +# Makefile for Microsoft Visual C++ 5.0 (or compat) + +# Top-file makefile for Win32 parts of postgresql. + +# Note that most parts are not ported to Win32! + +!IF "$(OS)" == "Windows_NT" +NULL= +!ELSE +NULL=nul +!ENDIF + +ALL: + cd interfaces\libpq + nmake /f win32.mak + cd ..\.. + echo All Win32 parts have been built!