[PORTS] WinCE/Pocket PC port for the C client library

"Nuno Lucas" <[email protected]> Thu, 13 Apr 2006 09:50:13 +0100
Newsgroups gmane.comp.db.postgresql.ports
Message-ID <[email protected]>
Hello,

I'm implementing a WinCE 4.x (Pocket PC 2003) application which needs
to get data from a postgres database, so I "hacked" the 8.1.2 libpq
source so I could get it working.

Well, it seems to be working ok, but, as I don't know the code, have
no idea if my hack is acceptable or induces other bugs I haven't seen
yet.

I attach my patch here, for any people interested.

I also extended a test application I had already made for sqlite3 so
it could be used as a basic test - SQLmd. It allows to open a sqlite3
or postgres database and make queries on it.

Full source, including eVC4 project files and a "sparse" version of
the postgres source are available at the following address:

http://www.xpto.ath.cx/~lucas/postgres/SQLmd_source.zip

For those who just want to try it, a PocketPC 2003 binary (ARM) is
available here:

http://www.xpto.ath.cx/~lucas/postgres/SQLmd_dbg.exe


Well, I'll wait for people to tell me how I should proceed now...


Best regards,
~Nuno Lucas


---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

               http://www.postgresql.org/docs/faq
postgres-wince-hack.patch (application/octet-stream, 10.7 KB)
# add_file "src/include/pg_config.h"
# add_file "src/include/pg_config_paths.h"
# patch "src/include/c.h"
# patch "src/include/getaddrinfo.h"
# patch "src/include/mb/pg_wchar.h"
# patch "src/include/port.h"
# patch "src/include/port/win32.h"
# patch "src/interfaces/libpq/fe-auth.c"
# patch "src/interfaces/libpq/fe-connect.c"
# patch "src/interfaces/libpq/fe-exec.c"
# patch "src/interfaces/libpq/fe-misc.c"
# patch "src/interfaces/libpq/fe-print.c"
# patch "src/interfaces/libpq/fe-protocol2.c"
# patch "src/interfaces/libpq/fe-protocol3.c"
# patch "src/interfaces/libpq/fe-secure.c"
# patch "src/interfaces/libpq/libpq-int.h"
# patch "src/port/getaddrinfo.c"
# patch "src/port/noblock.c"
============================================================
--- src/include/c.h	0994257edf9841f8294ad959a3a535adc3a4e349
+++ src/include/c.h	4afd242943a5078db57d29aa56cb009a5c93bcf4
@@ -69,12 +69,15 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
-#include <sys/types.h>

+#if !defined(_WIN32_WCE)
+#include <sys/types.h>
 #include <errno.h>
 #if defined(WIN32) || defined(__CYGWIN__)
 #include <fcntl.h>				/* ensure O_BINARY is available */
 #endif
+#endif		/* defined(_WIN32_WCE) */
+
 #ifdef HAVE_SUPPORTDEFS_H
 #include <SupportDefs.h>
 #endif
============================================================
--- src/include/getaddrinfo.h	2694f30932773d98155ec85efc1a379e565ceecf
+++ src/include/getaddrinfo.h	7a6f03c6dde5b75f3259ce12c2c2d2fcfd42d6b6
@@ -44,8 +44,10 @@
 #else							/* WIN32 */
 #if defined(WIN32_CLIENT_ONLY)
 #define WSA_NOT_ENOUGH_MEMORY   (WSAENOBUFS)
+#ifndef WSATYPE_NOT_FOUND
 #define WSATYPE_NOT_FOUND       (WSABASEERR+109)
 #endif
+#endif
 #define EAI_AGAIN		WSATRY_AGAIN
 #define EAI_BADFLAGS	WSAEINVAL
 #define EAI_FAIL		WSANO_RECOVERY
============================================================
--- src/include/mb/pg_wchar.h	4b2bab773d72070fcc8dfbf8fde837425c865360
+++ src/include/mb/pg_wchar.h	84659b8d031c67a7a0ffe2b4de5c442a7601b2c2
@@ -1,9 +1,11 @@
 /* $PostgreSQL: pgsql/src/include/mb/pg_wchar.h,v 1.63 2005/10/15 02:49:45 momjian Exp $ */
 
 #ifndef PG_WCHAR_H
 #define PG_WCHAR_H
 
+#ifndef _WIN32_WCE
 #include <sys/types.h>
+#endif
 
 #ifdef FRONTEND
 #undef palloc
============================================================
--- src/include/pg_config.h	
+++ src/include/pg_config.h	556e59c0f48b1bf098113929d0b653a5690baef4
@@ -0,0 +1,34 @@
+#ifndef	pg_config_h_win32_ce__
+#define	pg_config_h_win32_ce__
+/*
+ * Parts of pg_config.h that you get with autoconf on other systems
+ */
+#define PG_VERSION "8.1.3"
+#define PG_VERSION_STR "8.1.3 (win32-ce)"
+
+#define DEF_PGPORT 5432
+#define DEF_PGPORT_STR "5432"
+
+#define MAXIMUM_ALIGNOF 4
+#define ACCEPT_TYPE_ARG3 int
+
+#define MAXPGPATH 1024
+
+#define INDEX_MAX_KEYS 32
+
+#define HAVE_ATEXIT
+#define HAVE_MEMMOVE
+
+/* use _snprintf and _vsnprintf */
+#define HAVE_DECL_SNPRINTF 1
+#define snprintf        _snprintf
+#define HAVE_DECL_VSNPRINTF 1
+#define vsnprintf       _vsnprintf
+
+/* use _strdup */
+#define HAVE_STRDUP
+#define strdup          _strdup
+
+#include <windows.h>
+
+#endif /* pg_config_h_win32_ce__ */
============================================================
--- src/include/pg_config_paths.h	
+++ src/include/pg_config_paths.h	eac62c1b718e49eade3edfed9aafc68ad8b5b281
@@ -0,0 +1,11 @@
+#define PGBINDIR ""
+#define PGSHAREDIR ""
+#define SYSCONFDIR ""
+#define INCLUDEDIR ""
+#define PKGINCLUDEDIR ""
+#define INCLUDEDIRSERVER ""
+#define LIBDIR ""
+#define PKGLIBDIR ""
+#define LOCALEDIR ""
+#define DOCDIR ""
+#define MANDIR ""
============================================================
--- src/include/port.h	1592ad48aede9bc9b035d274b00b782903ee5865
+++ src/include/port.h	b01fb73781fe6a33dee7669cbe2244a936c17c62
@@ -218,7 +218,7 @@
 extern int	pgunlink(const char *path);
 
 /* Include this first so later includes don't see these defines */
-#ifdef WIN32_CLIENT_ONLY
+#if defined(WIN32_CLIENT_ONLY) && !defined(_WIN32_WCE)
 #include <io.h>
 #endif
 
============================================================
--- src/include/port/win32.h	1789bfba8ef0bc21eed53a965f975bba9461c985
+++ src/include/port/win32.h	43ecfea2b42f1cd2c2bcd462a5dc9b053e9d9b94
@@ -6,9 +6,14 @@
 #undef ERROR
 #include <windows.h>
 #include <winsock.h>
+
+#ifndef _WIN32_WCE
 #include <process.h>
 #include <signal.h>
 #include <errno.h>
+#else
+#define getenv(a)	NULL
+#endif
 
 #undef near
 
============================================================
--- src/interfaces/libpq/fe-auth.c	b73a32fa3b9f101306e20e92a29e2efd7138bc59
+++ src/interfaces/libpq/fe-auth.c	2973445813a5ada92b6c85ba5df6c841de55ce0e
@@ -517,8 +517,10 @@
 	if (!name)
 	{
 #ifdef WIN32
+#ifndef _WIN32_WCE
 		if (GetUserName(username, &namesize))
 			name = username;
+#endif
 #else
 		if (pqGetpwuid(geteuid(), &pwdstr, pwdbuf, sizeof(pwdbuf), &pw) == 0)
 			name = pw->pw_name;
============================================================
--- src/interfaces/libpq/fe-connect.c	80b9eeb22f25f8c8998a04fc932ada19f191e4cb
+++ src/interfaces/libpq/fe-connect.c	a6eccfc27b020130fe93eeab3fc1cc3292c3270d
@@ -15,9 +15,11 @@
 
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#endif
 #include <ctype.h>
 #include <time.h>
 #ifndef WIN32_CLIENT_ONLY
@@ -447,8 +449,10 @@
 	{
 		if (conn->pgpass)
 			free(conn->pgpass);
+#ifndef _WIN32_WCE
 		conn->pgpass = PasswordFromFile(conn->pghost, conn->pgport,
 										conn->dbName, conn->pguser);
+#endif
 		if (conn->pgpass == NULL)
 			conn->pgpass = strdup(DefaultPassword);
 	}
@@ -1148,8 +1152,15 @@
 				 * state waiting for us on the socket.
 				 */
 
-				if (getsockopt(conn->sock, SOL_SOCKET, SO_ERROR,
-							   (char *) &optval, &optlen) == -1)
+				int err = getsockopt(conn->sock, SOL_SOCKET, SO_ERROR,
+										(char *) &optval, &optlen);
+#if defined(_WIN32_WCE)
+				/* This was "googled" from the curl library source */
+				/* Always returns this error, bug in CE? */
+				if ( err == -1 && SOCK_ERRNO == WSAENOPROTOOPT )
+					err=0;
+#endif
+				if ( err == -1 )
 				{
 					printfPQExpBuffer(&conn->errorMessage,
 					libpq_gettext("could not get socket error status: %s\n"),
@@ -3074,7 +3085,7 @@
 	}
 	return NULL;
 }
-
+#ifndef _WIN32_WCE
 /* Get a password from the password file. Return value is malloc'd. */
 static char *
 PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
@@ -3171,6 +3182,7 @@
 
 #undef LINELEN
 }
+#endif		/* ifndef _WIN32_WCE */
 
 /*
  * Obtain user's home directory, return in given buffer
@@ -3194,6 +3206,9 @@
 		return false;
 	StrNCpy(buf, pwd->pw_dir, bufsize);
 	return true;
+#elif defined(_WIN32_WCE)
+	StrNCpy(buf, "/", bufsize);
+	return true;
 #else
 	char		tmppath[MAX_PATH];
 
============================================================
--- src/interfaces/libpq/fe-exec.c	a463e2f04fa626092ef9ddb620556861b7b4f652
+++ src/interfaces/libpq/fe-exec.c	a0059e4d2b94cf21789bcd0518257f299e59335c
@@ -14,9 +14,11 @@
  */
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <errno.h>
-#include <ctype.h>
 #include <fcntl.h>
+#endif
+#include <ctype.h>
 
 #include "libpq-fe.h"
 #include "libpq-int.h"
============================================================
--- src/interfaces/libpq/fe-misc.c	3c14ff18a129eb1390dbc99df6f94c562749f2f0
+++ src/interfaces/libpq/fe-misc.c	40dbd28932033b3ede22bd029f4467dda4045098
@@ -30,8 +30,10 @@
 
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <errno.h>
 #include <signal.h>
+#endif
 #include <time.h>
 
 #ifndef WIN32_CLIENT_ONLY
@@ -776,7 +778,9 @@
 				case EINTR:
 					continue;
 
+#ifdef EPIPE
 				case EPIPE:
+#endif
 #ifdef ECONNRESET
 				case ECONNRESET:
 #endif
============================================================
--- src/interfaces/libpq/fe-print.c	fab09f59a2ed56b03f86928775b04526eeb55733
+++ src/interfaces/libpq/fe-print.c	7a5db95b5eacb5e787989fbe0f1c51b0b07c7b8b
@@ -16,7 +16,9 @@
  */
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <signal.h>
+#endif
 
 #ifdef WIN32
 #include "win32.h"
@@ -65,6 +67,7 @@
  * doesn't use it anymore. It is unclear to what extent this is used
  * by external clients, however.
  */
+#ifndef _WIN32_WCE
 void
 PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
 {
@@ -325,8 +328,8 @@
 			fputs("</table>\n", fout);
 	}
 }
+#endif	/* _WIN32_WCE */
 
-
 static void
 do_field(const PQprintOpt *po, const PGresult *res,
 		 const int i, const int j, const int fs_len,
============================================================
--- src/interfaces/libpq/fe-protocol2.c	d1e7f27a0ee0460bfc656c7d22f2ca8312d7d9fa
+++ src/interfaces/libpq/fe-protocol2.c	fae1c31b7e06dac97a5caf31a49ca26529f4ada4
@@ -14,9 +14,11 @@
  */
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <errno.h>
-#include <ctype.h>
 #include <fcntl.h>
+#endif
+#include <ctype.h>
 
 #include "libpq-fe.h"
 #include "libpq-int.h"
============================================================
--- src/interfaces/libpq/fe-protocol3.c	61624b0108ff05e81262141b606201aab6357914
+++ src/interfaces/libpq/fe-protocol3.c	ed3e13f5e7ddf3de1ed2d59be67e1d8bc164f228
@@ -14,9 +14,11 @@
  */
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <errno.h>
-#include <ctype.h>
 #include <fcntl.h>
+#endif
+#include <ctype.h>
 
 #include "libpq-fe.h"
 #include "libpq-int.h"
============================================================
--- src/interfaces/libpq/fe-secure.c	33597b72fb1773da2d9000dc7b7dd22cdb3e9db9
+++ src/interfaces/libpq/fe-secure.c	9838f694da5da7ec91cf2816832cf0dac3bdec43
@@ -79,8 +79,10 @@
 
 #include "postgres_fe.h"
 
+#ifndef _WIN32_WCE
 #include <signal.h>
 #include <fcntl.h>
+#endif
 #include <ctype.h>
 
 #include "libpq-fe.h"
@@ -100,7 +102,9 @@
 #endif
 #include <arpa/inet.h>
 #endif
+#ifndef _WIN32_WCE
 #include <sys/stat.h>
+#endif
 
 #ifdef ENABLE_THREAD_SAFETY
 #ifdef WIN32
============================================================
--- src/interfaces/libpq/libpq-int.h	f314d13d02dc414e4e570c245a1580046f55a5e3
+++ src/interfaces/libpq/libpq-int.h	fb5d5cf369833cce02473fbd8a7c31e21b6394fb
@@ -24,7 +24,9 @@
 #include "postgres_fe.h"
 
 #include <time.h>
+#ifndef _WIN32_WCE
 #include <sys/types.h>
+#endif
 #ifndef WIN32
 #include <sys/time.h>
 #endif
============================================================
--- src/port/getaddrinfo.c	f77013d0b19508fd5f5b15b92b1833588f4e4e79
+++ src/port/getaddrinfo.c	982ae4de00514613e1534b6db5c3c21a88717e4f
@@ -196,7 +196,7 @@
 		{
 			struct hostent *hp;
 
-#ifdef FRONTEND
+#if defined(FRONTEND) && !defined(_WIN32_WCE)
 			struct hostent hpstr;
 			char		buf[BUFSIZ];
 			int			herrno = 0;
============================================================
--- src/port/noblock.c	27761d3f360a12c0a8be5ba55d4650d10182d0d6
+++ src/port/noblock.c	563395c66c9077c4c1171d9a991a6094c9a529e0
@@ -14,7 +14,9 @@
 
 #include "c.h"
 
+#ifndef _WIN32_WCE
 #include <fcntl.h>
+#endif
 
 
 bool