xref: /original-bsd/usr.bin/whois/whois.c (revision 12442fd8)
1eb5dd98aSdist /*
2*12442fd8Sbostic  * Copyright (c) 1980, 1993
3*12442fd8Sbostic  *	The Regents of the University of California.  All rights reserved.
441c4ea2aSbostic  *
592ab5233Sbostic  * %sccs.include.redist.c%
6eb5dd98aSdist  */
7eb5dd98aSdist 
8bafa34bfSsam #ifndef lint
9*12442fd8Sbostic static char copyright[] =
10*12442fd8Sbostic "@(#) Copyright (c) 1980, 1993\n\
11*12442fd8Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1241c4ea2aSbostic #endif /* not lint */
13eb5dd98aSdist 
14eb5dd98aSdist #ifndef lint
15*12442fd8Sbostic static char sccsid[] = "@(#)whois.c	8.1 (Berkeley) 06/06/93";
1641c4ea2aSbostic #endif /* not lint */
17bafa34bfSsam 
18bafa34bfSsam #include <sys/types.h>
19bafa34bfSsam #include <sys/socket.h>
20bafa34bfSsam #include <netinet/in.h>
21bafa34bfSsam #include <netdb.h>
2202dde3a5Sbostic #include <stdio.h>
23bafa34bfSsam 
24ebc8ec2dSmckusick #define	NICHOST	"whois.internic.net"
25bafa34bfSsam 
main(argc,argv)26bafa34bfSsam main(argc, argv)
27bafa34bfSsam 	int argc;
2802dde3a5Sbostic 	char **argv;
29bafa34bfSsam {
3041c4ea2aSbostic 	extern char *optarg;
3141c4ea2aSbostic 	extern int optind;
32bafa34bfSsam 	register FILE *sfi, *sfo;
3302dde3a5Sbostic 	register int ch;
34bafa34bfSsam 	struct sockaddr_in sin;
35bafa34bfSsam 	struct hostent *hp;
36bafa34bfSsam 	struct servent *sp;
3702dde3a5Sbostic 	int s;
3841c4ea2aSbostic 	char *host;
39bafa34bfSsam 
4041c4ea2aSbostic 	host = NICHOST;
419e767a02Sbostic 	while ((ch = getopt(argc, argv, "h:")) != EOF)
4241c4ea2aSbostic 		switch((char)ch) {
4341c4ea2aSbostic 		case 'h':
4441c4ea2aSbostic 			host = optarg;
4541c4ea2aSbostic 			break;
4641c4ea2aSbostic 		case '?':
4741c4ea2aSbostic 		default:
4841c4ea2aSbostic 			usage();
49bafa34bfSsam 		}
5041c4ea2aSbostic 	argc -= optind;
5141c4ea2aSbostic 	argv += optind;
5241c4ea2aSbostic 
532027ae92Sbostic 	if (!argc)
5441c4ea2aSbostic 		usage();
5541c4ea2aSbostic 
56bafa34bfSsam 	hp = gethostbyname(host);
57bafa34bfSsam 	if (hp == NULL) {
5802dde3a5Sbostic 		(void)fprintf(stderr, "whois: %s: ", host);
599e767a02Sbostic 		herror((char *)NULL);
60bafa34bfSsam 		exit(1);
61bafa34bfSsam 	}
62bafa34bfSsam 	host = hp->h_name;
6341f32815Sbostic 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
64bafa34bfSsam 	if (s < 0) {
65bafa34bfSsam 		perror("whois: socket");
661c1bad9fSbostic 		exit(1);
67bafa34bfSsam 	}
6867cc5868Sleres 	bzero((caddr_t)&sin, sizeof (sin));
69bafa34bfSsam 	sin.sin_family = hp->h_addrtype;
7041f32815Sbostic 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
71bafa34bfSsam 		perror("whois: bind");
721c1bad9fSbostic 		exit(1);
73bafa34bfSsam 	}
7441f32815Sbostic 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
75bafa34bfSsam 	sp = getservbyname("whois", "tcp");
76bafa34bfSsam 	if (sp == NULL) {
7702dde3a5Sbostic 		(void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
781c1bad9fSbostic 		exit(1);
79bafa34bfSsam 	}
80bafa34bfSsam 	sin.sin_port = sp->s_port;
811c1bad9fSbostic 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
82bafa34bfSsam 		perror("whois: connect");
831c1bad9fSbostic 		exit(1);
84bafa34bfSsam 	}
85bafa34bfSsam 	sfi = fdopen(s, "r");
86bafa34bfSsam 	sfo = fdopen(s, "w");
87bafa34bfSsam 	if (sfi == NULL || sfo == NULL) {
8802dde3a5Sbostic 		perror("whois: fdopen");
8902dde3a5Sbostic 		(void)close(s);
90bafa34bfSsam 		exit(1);
91bafa34bfSsam 	}
922027ae92Sbostic 	while (argc-- > 1)
932027ae92Sbostic 		(void)fprintf(sfo, "%s ", *argv++);
9402dde3a5Sbostic 	(void)fprintf(sfo, "%s\r\n", *argv);
9541c4ea2aSbostic 	(void)fflush(sfo);
9602dde3a5Sbostic 	while ((ch = getc(sfi)) != EOF)
9702dde3a5Sbostic 		putchar(ch);
9841f32815Sbostic 	exit(0);
99bafa34bfSsam }
10041c4ea2aSbostic 
usage()10141c4ea2aSbostic usage()
10241c4ea2aSbostic {
1032027ae92Sbostic 	(void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
10441c4ea2aSbostic 	exit(1);
10541c4ea2aSbostic }
106