[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
LYNX-DEV Re: _WINDOWS patch for doing NSL_FORK
From: |
afn06760 |
Subject: |
LYNX-DEV Re: _WINDOWS patch for doing NSL_FORK |
Date: |
Sat, 14 Mar 1998 15:59:36 GMT |
> * From: address@hidden
> * Date: Fri, 13 Mar 1998 06:16:10 GMT
>> * From: address@hidden
>> * Date: Fri, 13 Mar 1998 03:44:38 GMT
>
>The following is a test program compiled with MINGW32 gcc2.8 to
>demonstrate a simple, but effective, use of subthreads to implement
>NSL_FORK for _WINDOWS in HTTCP.c. Note that the text at the bottom
>of this note is copied from a Dos window while running Windows 95 and
>after connecting to an internet server. Note also how the selective
>use of global variables simplifies the code.
>
>======================================================================
>socktest.c:
>======================================================================
>
>#define Win32_Winsock /* Needed for cygwin/mingw32 gcc */
>#include <windows.h>
>#include <stdio.h>
>#include <stdlib.h>
>#include <conio.h>
>
>struct hostent FAR *phost;
>const char FAR *host = "www.mit.edu";
>
>void _fork_func (void *arglist)
>{
> phost = gethostbyname(host);
>}
>
>int main (int argc, char **argv)
>{
> HANDLE hThread;
> WSADATA buffer;
> WORD wVerReq;
>
> wVerReq = MAKEWORD(1,1);
>
> if (WSAStartup(wVerReq, &buffer))
> {printf("Unable to start wsock32.dll\n"); return 1; };
> hThread = _beginthread((void (*)())_fork_func,
> 4096*2,
> (void *)NULL);
> while (!phost)
> if (kbhit())
> {CloseHandle(hThread); printf("Thread stopped. host: %s\n",host); ret
>urn 0; };
> printf("Host found: %s %X\n", host, phost);
> return WSACleanup();
>}
>
>
>
>========================================================
>E:\src\bcsock>gcc -O -o socktest socktest.c -lwsock32
>socktest.c: In function `main':
>socktest.c:27: warning: assignment makes pointer from integer without a cast
>
>E:\src\bcsock>socktest
>Host found: www.mit.edu 410C2C
>
>E:\src\bcsock>
>
>=========================================================
Here is the actual patch to HTTCP.c in Unix diff format, and I am writing
this from the resulting Windows95/crtdll.dll version of Lynx:
=========================================================================
--- HTTCP.c.orig Fri Feb 27 13:25:05 1998
+++ HTTCP.c Sat Mar 14 10:53:45 1998
@@ -310,14 +310,26 @@
** *soc_in is filled in. If no port is specified in str, that
** field is left unchanged in *soc_in.
*/
+#ifdef _WINDOWS
+ char host[512];
+ struct hostent *phost; /* Pointer to host - See netdb.h */
+unsigned long _fork_func (void *arglist)
+{
+ return (unsigned long)(phost = gethostbyname(host));
+}
+#endif
+
+
PUBLIC int HTParseInet ARGS2(
SockA *, soc_in,
CONST char *, str)
{
char *port;
- char *host = NULL;
int dotcount_ip = 0; /* for dotted decimal IP addr */
+#ifndef _WINDOWS
+ char *host = NULL;
struct hostent *phost; /* Pointer to host - See netdb.h */
+#endif
if (!str) {
if (TRACE) {
@@ -330,8 +342,12 @@
fprintf (stderr, "HTParseInet: INTERRUPTED for '%s'.\n", str);
}
return -1;
- }
+ };
+#ifdef _WINDOWS
+ strncpy(host, str, (size_t)512);
+#else
StrAllocCopy(host, str); /* Make a copy we can mutilate */
+#endif
/*
** Parse port number if present.
@@ -407,7 +423,9 @@
#endif /* GUSI */
#endif /* DGUX_OLD */
#endif /* DJGPP */
+#ifndef _WINDOWS
FREE(host);
+#endif
} else { /* Alphanumeric node name: */
#ifdef MVS /* Outstanding problem with crash in MVS gethostbyname */
if (TRACE) {
@@ -572,7 +590,34 @@
return -1; /* Fail? */
}
#else
+#ifdef _WINDOWS
+/* Note that this is a replacement for the `phost=gethostbyname()' */
+/* statement in the #else clause below: */
+ {
+ unsigned long hThread, dwThreadID;
+ phost = (struct hostent *)NULL;
+ hThread = CreateThread((void *)NULL, 4096UL,
+ (unsigned long (*)())_fork_func,
+ (void *)NULL, 0UL, (unsigned long *)&dwThreadID);
+ if (!hThread)
+ if (TRACE)
+ MessageBox((void *)NULL, "CreateThread",
+ "CreateThread Failed", 0L);
+ while (!phost)
+ if (HTCheckForInterrupt())
+ {
+ /* Note that host is a character array and is not freed */
+ /* to avoid possible subthread problems: */
+ if (!CloseHandle(hThread))
+ if (TRACE)
+ MessageBox((void *)NULL, "CloseHandle",
+ "CloseHandle Failed", 0L);
+ return HT_INTERRUPTED;
+ };
+ };
+#else
phost = gethostbyname(host); /* See netdb.h */
+#endif
#ifdef MVS
if (TRACE) {
fprintf(stderr,
@@ -584,11 +629,15 @@
fprintf(stderr,
"HTParseInet: Can't find internet node name `%s'.\n",
host);
- }
+ };
+#ifndef _WINDOWS
FREE(host);
+#endif
return -1; /* Fail? */
- }
+ };
+#ifndef _WINDOWS
FREE(host);
+#endif
#if defined(VMS) && defined(CMU_TCP)
/*
** In LIBCMU, phost->h_length contains not the length of one address
@@ -836,7 +885,11 @@
return HT_NO_DATA;
}
+#ifdef _WINDOWS
+ timeout.tv_sec = 100;
+#else
timeout.tv_sec = 0;
+#endif
timeout.tv_usec = 100000;
FD_ZERO(&writefds);
FD_SET(*s, &writefds);