Bogus ioctl arguments fixed.
[unix-history] / usr.bin / whois / whois.c
CommitLineData
15637ed4
RG
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 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
34 * -------------------- ----- ----------------------
35 * CURRENT PATCH LEVEL: 1 00143
36 * -------------------- ----- ----------------------
37 *
38 * 20 Apr 93 Unknown New default 'whois' server address
39 *
40 */
41
42#ifndef lint
43char copyright[] =
44"@(#) Copyright (c) 1980 Regents of the University of California.\n\
45 All rights reserved.\n";
46#endif /* not lint */
47
48#ifndef lint
49static char sccsid[] = "@(#)whois.c 5.11 (Berkeley) 3/2/91";
50#endif /* not lint */
51
52#include <sys/types.h>
53#include <sys/socket.h>
54#include <netinet/in.h>
55#include <netdb.h>
56#include <stdio.h>
57
58#define NICHOST "whois.internic.net"
59
60main(argc, argv)
61 int argc;
62 char **argv;
63{
64 extern char *optarg;
65 extern int optind;
66 register FILE *sfi, *sfo;
67 register int ch;
68 struct sockaddr_in sin;
69 struct hostent *hp;
70 struct servent *sp;
71 int s;
72 char *host;
73
74 host = NICHOST;
75 while ((ch = getopt(argc, argv, "h:")) != EOF)
76 switch((char)ch) {
77 case 'h':
78 host = optarg;
79 break;
80 case '?':
81 default:
82 usage();
83 }
84 argc -= optind;
85 argv += optind;
86
87 if (!argc)
88 usage();
89
90 hp = gethostbyname(host);
91 if (hp == NULL) {
92 (void)fprintf(stderr, "whois: %s: ", host);
93 herror((char *)NULL);
94 exit(1);
95 }
96 host = hp->h_name;
97 s = socket(hp->h_addrtype, SOCK_STREAM, 0);
98 if (s < 0) {
99 perror("whois: socket");
100 exit(1);
101 }
102 bzero((caddr_t)&sin, sizeof (sin));
103 sin.sin_family = hp->h_addrtype;
104 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
105 perror("whois: bind");
106 exit(1);
107 }
108 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
109 sp = getservbyname("whois", "tcp");
110 if (sp == NULL) {
111 (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
112 exit(1);
113 }
114 sin.sin_port = sp->s_port;
115 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
116 perror("whois: connect");
117 exit(1);
118 }
119 sfi = fdopen(s, "r");
120 sfo = fdopen(s, "w");
121 if (sfi == NULL || sfo == NULL) {
122 perror("whois: fdopen");
123 (void)close(s);
124 exit(1);
125 }
126 while (argc-- > 1)
127 (void)fprintf(sfo, "%s ", *argv++);
128 (void)fprintf(sfo, "%s\r\n", *argv);
129 (void)fflush(sfo);
130 while ((ch = getc(sfi)) != EOF)
131 putchar(ch);
132 exit(0);
133}
134
135usage()
136{
137 (void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
138 exit(1);
139}