xref: /csrg-svn/usr.bin/whois/whois.c (revision 62448)
121592Sdist /*
2*62448Sbostic  * Copyright (c) 1980, 1993
3*62448Sbostic  *	The Regents of the University of California.  All rights reserved.
434694Sbostic  *
542788Sbostic  * %sccs.include.redist.c%
621592Sdist  */
721592Sdist 
810040Ssam #ifndef lint
9*62448Sbostic static char copyright[] =
10*62448Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*62448Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1234694Sbostic #endif /* not lint */
1310040Ssam 
1421592Sdist #ifndef lint
15*62448Sbostic static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 06/06/93";
1634694Sbostic #endif /* not lint */
1721592Sdist 
1810040Ssam #include <sys/types.h>
1910040Ssam #include <sys/socket.h>
2010040Ssam #include <netinet/in.h>
2137639Sbostic #include <netdb.h>
2210040Ssam #include <stdio.h>
2310040Ssam 
2460320Smckusick #define	NICHOST	"whois.internic.net"
2510040Ssam 
main(argc,argv)2610040Ssam main(argc, argv)
2710040Ssam 	int argc;
2837639Sbostic 	char **argv;
2910040Ssam {
3034694Sbostic 	extern char *optarg;
3134694Sbostic 	extern int optind;
3210040Ssam 	register FILE *sfi, *sfo;
3337639Sbostic 	register int ch;
3410040Ssam 	struct sockaddr_in sin;
3510040Ssam 	struct hostent *hp;
3610040Ssam 	struct servent *sp;
3737639Sbostic 	int s;
3834694Sbostic 	char *host;
3910040Ssam 
4034694Sbostic 	host = NICHOST;
4135791Sbostic 	while ((ch = getopt(argc, argv, "h:")) != EOF)
4234694Sbostic 		switch((char)ch) {
4334694Sbostic 		case 'h':
4434694Sbostic 			host = optarg;
4534694Sbostic 			break;
4634694Sbostic 		case '?':
4734694Sbostic 		default:
4834694Sbostic 			usage();
4934694Sbostic 		}
5034694Sbostic 	argc -= optind;
5134694Sbostic 	argv += optind;
5234694Sbostic 
5345777Sbostic 	if (!argc)
5434694Sbostic 		usage();
5534694Sbostic 
5610040Ssam 	hp = gethostbyname(host);
5710040Ssam 	if (hp == NULL) {
5837639Sbostic 		(void)fprintf(stderr, "whois: %s: ", host);
5935791Sbostic 		herror((char *)NULL);
6010040Ssam 		exit(1);
6110040Ssam 	}
6210040Ssam 	host = hp->h_name;
6339110Sbostic 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
6410040Ssam 	if (s < 0) {
6510040Ssam 		perror("whois: socket");
6646898Sbostic 		exit(1);
6710040Ssam 	}
6833475Sleres 	bzero((caddr_t)&sin, sizeof (sin));
6910040Ssam 	sin.sin_family = hp->h_addrtype;
7039110Sbostic 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
7110040Ssam 		perror("whois: bind");
7246898Sbostic 		exit(1);
7310040Ssam 	}
7439110Sbostic 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
7510040Ssam 	sp = getservbyname("whois", "tcp");
7610040Ssam 	if (sp == NULL) {
7737639Sbostic 		(void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
7846898Sbostic 		exit(1);
7910040Ssam 	}
8010040Ssam 	sin.sin_port = sp->s_port;
8146898Sbostic 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
8210040Ssam 		perror("whois: connect");
8346898Sbostic 		exit(1);
8410040Ssam 	}
8510040Ssam 	sfi = fdopen(s, "r");
8610040Ssam 	sfo = fdopen(s, "w");
8710040Ssam 	if (sfi == NULL || sfo == NULL) {
8837639Sbostic 		perror("whois: fdopen");
8937639Sbostic 		(void)close(s);
9010040Ssam 		exit(1);
9110040Ssam 	}
9245777Sbostic 	while (argc-- > 1)
9345777Sbostic 		(void)fprintf(sfo, "%s ", *argv++);
9437639Sbostic 	(void)fprintf(sfo, "%s\r\n", *argv);
9534694Sbostic 	(void)fflush(sfo);
9637639Sbostic 	while ((ch = getc(sfi)) != EOF)
9737639Sbostic 		putchar(ch);
9839110Sbostic 	exit(0);
9910040Ssam }
10034694Sbostic 
usage()10134694Sbostic usage()
10234694Sbostic {
10345777Sbostic 	(void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
10434694Sbostic 	exit(1);
10534694Sbostic }
106