xref: /titanic_51/usr/src/cmd/cmd-inet/usr.bin/whois.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 
45 #include <netinet/in.h>
46 
47 #include <stdio.h>
48 #include <netdb.h>
49 
50 #ifdef SYSV
51 #define	bcopy(a,b,c)	memcpy((b),(a),(c))
52 #endif /* SYSV */
53 
54 #define	NICHOST	"whois.internic.net"
55 
56 main(argc, argv)
57 	int argc;
58 	char *argv[];
59 {
60 	int s;
61 	register FILE *sfi, *sfo;
62 	register int c;
63 	char *host = NICHOST;
64 	struct sockaddr_in sin;
65 	struct hostent *hp;
66 	struct servent *sp;
67 	char hnamebuf[32];
68 	int addrtype;
69 
70 	argc--, argv++;
71 	if (argc > 2 && strcmp(*argv, "-h") == 0) {
72 		argv++, argc--;
73 		host = *argv++;
74 		argc--;
75 	}
76 	if (argc != 1) {
77 		fprintf(stderr, "usage: whois [ -h host ] name\n");
78 		exit(1);
79 	}
80 	sin.sin_addr.s_addr = inet_addr(host);
81 	if (sin.sin_addr.s_addr != -1 && sin.sin_addr.s_addr != 0) {
82 		addrtype = AF_INET;
83 	} else {
84 		hp = gethostbyname(host);
85 		if (hp == NULL) {
86 			fprintf(stderr, "whois: %s: host unknown\n", host);
87 			exit(1);
88 		}
89 		addrtype = hp->h_addrtype;
90 		host = hp->h_name;
91 		bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
92 	}
93 
94 	s = socket(addrtype, SOCK_STREAM, 0);
95 	if (s < 0) {
96 		perror("whois: socket");
97 		exit(2);
98 	}
99 	sin.sin_family = addrtype;
100 	sp = getservbyname("whois", "tcp");
101 	if (sp == NULL) {
102 		sin.sin_port = htons(IPPORT_WHOIS);
103 	}
104 	else sin.sin_port = sp->s_port;
105 	if (connect(s, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
106 		perror("whois: connect");
107 		exit(5);
108 	}
109 	sfi = fdopen(s, "r");
110 	sfo = fdopen(s, "w");
111 	if (sfi == NULL || sfo == NULL) {
112 		perror("fdopen");
113 		close(s);
114 		exit(1);
115 	}
116 	fprintf(sfo, "%s\r\n", *argv);
117 	fflush(sfo);
118 	while ((c = getc(sfi)) != EOF)
119 		putchar(c);
120 	exit(0);
121 	/* NOTREACHED */
122 }
123