diff options
Diffstat (limited to 'fd.c')
-rw-r--r-- | fd.c | 112 |
1 files changed, 99 insertions, 13 deletions
@@ -23,10 +23,45 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. * * + * * + * Copyright 2002 Sun Microsystems, Inc. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons + * to whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT + * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL + * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Except as contained in this notice, the name of a copyright holder + * shall not be used in advertising or otherwise to promote the sale, use + * or other dealings in this Software without prior written authorization + * of the copyright holder. + * \* *********************************************************** */ #include "scope.h" +#ifdef SYSV +#include <unistd.h> +#define getdtablesize() sysconf(_SC_OPEN_MAX) +#define bzero(s,l) memset(s, 0, l) +#define bcopy(s,d,l) memmove(d,s,l) +#endif + #include <sys/uio.h> /* for struct iovec, used by socket.h */ #include <sys/socket.h> /* for AF_INET, SOCK_STREAM, ... */ #include <sys/ioctl.h> /* for FIONCLEX, FIONBIO, ... */ @@ -56,6 +91,7 @@ extern int errno; /* */ /* ************************************************************ */ +void InitializeFD() { register short i; @@ -66,6 +102,9 @@ InitializeFD() MaxFD = _NFILE - 1; #else MaxFD = getdtablesize(); + if (MaxFD > FD_SETSIZE) { + MaxFD = FD_SETSIZE; + } #endif if (MaxFD > StaticMaxFD) { @@ -76,14 +115,18 @@ InitializeFD() /* allocate space for a File Descriptor (FD) Table */ FDD = (struct FDDescriptor *) Malloc ((long)(MaxFD * sizeof (struct FDDescriptor))); + if (FDD == NULL) { + panic("Can't allocate memory!"); + } + bzero(FDD, (MaxFD * sizeof (struct FDDescriptor))); /* be sure all fd's are closed and marked not busy */ for (i = 0; i < MaxFD; i++) { /* 0, 1, 2 are special (stdin, stdout, stderr) */ if (i > 2) - (void)close(i); - FDD[i].Busy = false; + close(i); + /* FDD[i].Busy = false; - not needed since false==0*/ } /* save one FD for single file input or output like debugging */ @@ -94,17 +137,19 @@ InitializeFD() ReadDescriptors = 0; HighestFD = 0; - UsingFD(fileno(stdin), (int (*)())NULL, (int (*)())NULL); - UsingFD(fileno(stdout), (int (*)())NULL, (int (*)())NULL); - UsingFD(fileno(stderr), (int (*)())NULL); + UsingFD(fileno(stdin), NULL, NULL, NULL); + UsingFD(fileno(stdout), NULL, NULL, NULL); + UsingFD(fileno(stderr), NULL, NULL, NULL); } /* ************************************************************ */ -UsingFD(fd, Handler, FlushHandler) +void +UsingFD(fd, Handler, FlushHandler, trans_conn) FD fd; - int (*Handler)(); - int (*FlushHandler)(); + void (*Handler)(int); + void (*FlushHandler)(int); + XtransConnInfo trans_conn; { if (FDD[fd].Busy) NotUsingFD(fd); @@ -113,6 +158,9 @@ UsingFD(fd, Handler, FlushHandler) FDD[fd].Busy = true; FDD[fd].InputHandler = Handler; FDD[fd].FlushHandler = FlushHandler; +#ifdef USE_XTRANS + FDD[fd].trans_conn = trans_conn; +#endif if (Handler == NULL) ReadDescriptors &= ~(1 << fd) /* clear fd bit */ ; else @@ -129,6 +177,7 @@ UsingFD(fd, Handler, FlushHandler) /* ************************************************************ */ +void NotUsingFD(fd) FD fd; { @@ -148,12 +197,27 @@ NotUsingFD(fd) /* ************************************************************ */ +#ifdef USE_XTRANS +XtransConnInfo GetXTransConnInfo(FD fd) +{ + return FDD[fd].trans_conn; +} +#endif + +/* ************************************************************ */ + +static void EOFonFD(fd) FD fd; { enterprocedure("EOFonFD"); debug(128,(stderr, "EOF on %d\n", fd)); - (void)close(fd); +#ifdef USE_XTRANS + if (FDD[fd].trans_conn) + _X11TransClose(FDD[fd].trans_conn); + else +#endif + close(fd); NotUsingFD(fd); } @@ -201,12 +265,32 @@ AcceptConnection (ConnectionSocket) } FD -MakeConnection(server, port, report) +MakeConnection(server, port, report, trans_conn) char *server; short port; int report; + XtransConnInfo *trans_conn; /* transport connection object */ { FD ServerFD; +#ifdef USE_XTRANS + char address[256]; + int connect_stat; + extern long ServerBasePort; + + snprintf (address, sizeof(address), "%s:%d", server, port - ServerBasePort); + if ( (*trans_conn = _X11TransOpenCOTSClient(address)) == NULL ) { + debug(1,(stderr, "OpenCOTSClient failed\n")); + panic("Can't open connection to Server"); + } + if ((connect_stat = _X11TransConnect(*trans_conn,address)) < 0 ) { + _X11TransClose(*trans_conn); + *trans_conn = NULL; + debug(1,(stderr, "TransConnect failed\n")); + panic("Can't open connection to Server"); + } + + ServerFD = _X11TransGetConnectionNumber(*trans_conn); +#else /* !USE_XTRANS */ char HostName[512]; struct sockaddr_in sin; struct sockaddr_un sun; @@ -308,6 +392,7 @@ MakeConnection(server, port, report) panic("Can't open connection to Server"); } } +#endif /* USE_XTRANS */ debug(4,(stderr, "Connect To Server: FD %d\n", ServerFD)); return(ServerFD); @@ -319,11 +404,11 @@ MakeConnection(server, port, report) /* */ /* ************************************************************ */ -#include <errno.h> /* for EINTR, EADDRINUSE, ... */ +#include <errno.h> /* for EINTR, EADDRINUSE, ... */ extern int errno; - -MainLoop() +int +MainLoop(void) { enterprocedure("MainLoop"); @@ -409,4 +494,5 @@ MainLoop() } } } + return 0; } |