DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T s

⟦92b23ec8b⟧ TextFile

    Length: 6412 (0x190c)
    Types: TextFile
    Names: »searchwho.c«

Derivation

└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen
    └─ ⟦this⟧ »cph85dist/search/searchwho.c« 

TextFile

#ifndef lint
static char rcsid[] = "$Header: searchwho.c,v 2.2 85/08/15 13:37:49 matt Exp $";
#endif
/*
 *
 * searchwho
 *
 * multi-player and multi-system search and destroy.
 *
 * Original by Dave Pare	1983
 * Ported & improved
 *      by Matt Crawford	1985
 *
 * who is playing search
 *
 * Copyright (c) 1983
 *
 * $Log:	searchwho.c,v $
 * Revision 2.2  85/08/15  13:37:49  matt
 * Limit dtabsiz to an int's worth of bits.
 * 
 * Revision 2.1  85/04/10  17:31:55  matt
 * Major de-linting and minor restructuring.
 * 
 * Revision 1.3  85/02/10  02:06:33  matt
 * Allow a port to be specified, as for search itself.
 * 
 * Revision 1.2  85/02/09  23:50:40  matt
 * Eliminated the dependence on the value of the mask after
 * select() times out.  Use the return value to distinguish
 * a timeout!!
 * 
 */

#include <stdio.h>
#include <sys/param.h>		/* includes <sys/types.h> and <signal.h> */
#include <sys/stat.h>
#include <sys/file.h>
#include <sgtty.h>
#include <ctype.h>
#include <pwd.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "defines.h"
#include "structs.h"

#ifdef INET
#include <netinet/in.h>
#include <netdb.h>
struct	servent	defserv = {
	"search",
	(char **)0,
	DEFAULT_IN_PORT,
	"tcp"
};
#endif

extern	int errno;
int	dtabsiz;			/* how many fd's here? */
int	sock;				/* socket to daemon */

main(argc, argv)
int argc;
char *argv[];
{
	void reset();
	register int    cc;	/* returned from all sorts of calls */
	register int    i;	/* general purpose register */
	register int    sockmask;
	register char   buf[4096];/* misc buffer for i/o */
	int     mask;		/* masks used in select() calls */
	struct timeval  timeout;/* time outs for select calls */
	int     save_mask;	/* don't calculate mask each time */
#ifdef INET
	int     to_in;		/* flag: using internet */
	char    hostname[32];	/* name of local host */
	struct servent *serv;	/* returned by getservbyname() */
	struct hostent *host;	/* returned by gethostbyname() */
	struct sockaddr_in      in_addr;/* internet socket address */
#endif
	int     first_time;	/* for printing header */
	int     junk;
	struct sockaddr loc_addr;/* local socket address */

	dtabsiz = getdtablesize();
	if (dtabsiz > NBBY * sizeof (int))
		dtabsiz = NBBY * sizeof (int);
#ifdef INET
	if (argc > 3) {
		printf("usage: searchwho [hostname [port]]\n");
		exit(1);
	}
	if (argc == 1)
		to_in = 0;
	else
		to_in = 1;
#endif
 /* 
  * set all the signals
  */
	(void) signal(SIGINT, SIG_IGN);
	(void) signal(SIGTERM, SIG_IGN);
	(void) signal(SIGQUIT, reset);
	(void) signal(SIGHUP, reset);
	(void) signal(SIGPIPE, reset);
	(void) signal(SIGALRM, reset);
	(void) signal(SIGTSTP, SIG_IGN);
	(void) signal(SIGSTOP, SIG_IGN);
#ifdef INET
	if (to_in) {
	/* 
	 * now find the remote host
	 */
		host = gethostbyname(argv[1]);
		if (host == NULL) {
			printf("%s: no such host \"%s\"\n", argv[0], argv[1]);
			exit(1);
		}
	/* 
	 * get the service from /etc/services (525?)
	 */
		sock = socket(AF_INET, SOCK_STREAM, 0);
		if (sock < 0) {
			perror("socket");
			exit(1);
		}
		serv = getservbyname("search", "tcp");
		if (serv == NULL)
			fprintf(stderr, "%s: no entry in /etc/services\n",
					argv[0]);
		if (argc == 3)
			defserv.s_port = atoi(argv[2]);
		if (serv == NULL || argc == 3) {
			serv = &defserv;
			fprintf(stderr, "Assuming port %d\n", serv -> s_port);
			defserv.s_port = (int)htons((u_short)defserv.s_port);
		}
		in_addr.sin_port = serv -> s_port;
		in_addr.sin_family = host -> h_addrtype;
		bcopy(host -> h_addr, &in_addr.sin_addr, host -> h_length);
	/* 
	 * and connect to the foreign machine's search daemon
	 */
		if (connect(sock, &in_addr, sizeof(in_addr))) {
			printf("%s: can't connect to %s's search daemon\n",
					argv[0], argv[1]);
			exit(1);
		}
	} else {
		sock = socket(AF_UNIX, SOCK_STREAM, 0);
		if (sock < 0) {
			perror("search, socket");
			exit(1);
		}
		(void) strcpy(loc_addr.sa_data, SLOCK);
	/* 
	 * connect to the local search daemon thru the lock
	 * file (usually /tmp/slock for historical reasons)
	 */
		if (connect(sock, &loc_addr, sizeof(loc_addr))) {
			printf("no local search daemon running\n");
			exit(1);
		}
	}
#else
	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	if (sock < 0) {
		perror("search, socket");
		exit(1);
	}
	(void) strcpy(loc_addr.sa_data, SLOCK);
 /* 
  * connect to the local search daemon thru the lock
  * file (usually /tmp/slock for historical reasons)
  */
	if (connect(sock, &loc_addr, sizeof(loc_addr))) {
		printf("no local search daemon running\n");
		exit(1);
	}
#endif
 /* 
  * daemon knows that a 1 byte message 'w' is from "searchwho".
  */
	if (write(sock, "w", 1) != 1) {
		perror("write");
		printf("searchwho: bad connect on socket\n");
		exit(1);
	}
 /* 
  * set up all the stuff for the select loop
  */
	sockmask = 1 << sock;
	save_mask = sockmask;
	timeout.tv_usec = 0L;
	timeout.tv_sec = 3L;
 /* 
  * just in case nobody's there
  */
	alarm(20);
	first_time = 1;
	for (;;) {
		mask = save_mask;
		i = select(dtabsiz, &mask, 0, 0, &timeout);
		if (i < 0) {
			if (errno = EINTR)
				continue;
			perror("select");
		}
	/* 
	 * nope - no data waiting.  select() timed out.
	 */
		if (!i) {
			printf("no data yet, we'll wait a bit longer...\n");
			fflush(stdout);
			continue;
		}
		if (!(sockmask & mask))
			continue;
	/* 
	 * input waiting from the daemon - read it in and
	 * write it to the screen (stdout).
	 */
		alarm (3);
		if (fgets(buf, 80, &_iob[sock]) != NULL) {
		/* 
		 * when you get zero characters from a socket that
		 * had input waiting, it means that the socket was
		 * closed down (from the other side)
		 */
			if (first_time) {
				if (strncmp(buf, "nobody", 6) != 0) {
					printf("    username   points \n");
					first_time = 0;
				} else
					puts(buf);
			} else
				puts(buf);
			fflush(stdout);
		} else
			reset(0);
		alarm(0);
	}
}

/*
 * reset disconnects us from any sockets we've connected to,
 * and shuts everything down nicely.
 */
void reset(signal)
int signal;
{
	char	buf[1024];
	struct	timeval delay;
	int	mask;
	int	i;

	/*
	 * disconnect from our socket (getting rid of the
	 * gunk possibly leftover)
	 */
	mask = 1 << sock;
	delay.tv_sec = 0L;
	delay.tv_usec = 500L;
	shutdown(sock, 1);
	i = select(dtabsiz, &mask, 0, 0, &delay);
	if (i > 0) {
		i = read(sock, buf, sizeof(buf));
		write(1, buf, i);
	}
	shutdown(sock, 2);
	(void) close(sock);
	putchar('\n');
	exit(0);
}