whois.c revision 1.3 1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)whois.c 5.11 (Berkeley) 3/2/91";*/
42 static char rcsid[] = "$Id: whois.c,v 1.3 1993/08/01 18:02:49 mycroft Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <netdb.h>
49 #include <stdio.h>
50
51 #define NICHOST "whois.internic.net"
52
53 main(argc, argv)
54 int argc;
55 char **argv;
56 {
57 extern char *optarg;
58 extern int optind;
59 register FILE *sfi, *sfo;
60 register int ch;
61 struct sockaddr_in sin;
62 struct hostent *hp;
63 struct servent *sp;
64 int s;
65 char *host;
66
67 host = NICHOST;
68 while ((ch = getopt(argc, argv, "h:")) != EOF)
69 switch((char)ch) {
70 case 'h':
71 host = optarg;
72 break;
73 case '?':
74 default:
75 usage();
76 }
77 argc -= optind;
78 argv += optind;
79
80 if (!argc)
81 usage();
82
83 hp = gethostbyname(host);
84 if (hp == NULL) {
85 (void)fprintf(stderr, "whois: %s: ", host);
86 herror((char *)NULL);
87 exit(1);
88 }
89 host = hp->h_name;
90 s = socket(hp->h_addrtype, SOCK_STREAM, 0);
91 if (s < 0) {
92 perror("whois: socket");
93 exit(1);
94 }
95 bzero((caddr_t)&sin, sizeof (sin));
96 sin.sin_family = hp->h_addrtype;
97 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
98 perror("whois: bind");
99 exit(1);
100 }
101 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
102 sp = getservbyname("whois", "tcp");
103 if (sp == NULL) {
104 (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
105 exit(1);
106 }
107 sin.sin_port = sp->s_port;
108 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
109 perror("whois: connect");
110 exit(1);
111 }
112 sfi = fdopen(s, "r");
113 sfo = fdopen(s, "w");
114 if (sfi == NULL || sfo == NULL) {
115 perror("whois: fdopen");
116 (void)close(s);
117 exit(1);
118 }
119 while (argc-- > 1)
120 (void)fprintf(sfo, "%s ", *argv++);
121 (void)fprintf(sfo, "%s\r\n", *argv);
122 (void)fflush(sfo);
123 while ((ch = getc(sfi)) != EOF)
124 putchar(ch);
125 exit(0);
126 }
127
128 usage()
129 {
130 (void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
131 exit(1);
132 }
133