date and time created 91/01/17 18:23:46 by bostic
[unix-history] / usr / src / usr.bin / whois / whois.c
CommitLineData
f42904bc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
067198bd
KB
3 * All rights reserved.
4 *
32ce521f 5 * %sccs.include.redist.c%
f42904bc
DF
6 */
7
8#ifndef lint
9char copyright[] =
10"@(#) Copyright (c) 1980 Regents of the University of California.\n\
11 All rights reserved.\n";
067198bd 12#endif /* not lint */
f42904bc 13
09908f00 14#ifndef lint
9f48c250 15static char sccsid[] = "@(#)whois.c 5.10 (Berkeley) %G%";
067198bd 16#endif /* not lint */
09908f00
SL
17
18#include <sys/types.h>
19#include <sys/socket.h>
09908f00 20#include <netinet/in.h>
09908f00 21#include <netdb.h>
f3bbfed6 22#include <stdio.h>
09908f00 23
f3bbfed6 24#define NICHOST "nic.ddn.mil"
09908f00
SL
25
26main(argc, argv)
27 int argc;
f3bbfed6 28 char **argv;
09908f00 29{
067198bd
KB
30 extern char *optarg;
31 extern int optind;
09908f00 32 register FILE *sfi, *sfo;
f3bbfed6 33 register int ch;
09908f00
SL
34 struct sockaddr_in sin;
35 struct hostent *hp;
36 struct servent *sp;
f3bbfed6 37 int s;
067198bd
KB
38 char *host;
39
40 host = NICHOST;
b1c6645f 41 while ((ch = getopt(argc, argv, "h:")) != EOF)
067198bd
KB
42 switch((char)ch) {
43 case 'h':
44 host = optarg;
45 break;
46 case '?':
47 default:
48 usage();
49 }
50 argc -= optind;
51 argv += optind;
52
9f48c250 53 if (!argc)
067198bd 54 usage();
09908f00 55
09908f00
SL
56 hp = gethostbyname(host);
57 if (hp == NULL) {
f3bbfed6 58 (void)fprintf(stderr, "whois: %s: ", host);
b1c6645f 59 herror((char *)NULL);
09908f00
SL
60 exit(1);
61 }
62 host = hp->h_name;
0a2b6d04 63 s = socket(hp->h_addrtype, SOCK_STREAM, 0);
09908f00
SL
64 if (s < 0) {
65 perror("whois: socket");
66 exit(2);
67 }
58e160ea 68 bzero((caddr_t)&sin, sizeof (sin));
09908f00 69 sin.sin_family = hp->h_addrtype;
0a2b6d04 70 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
09908f00
SL
71 perror("whois: bind");
72 exit(3);
73 }
0a2b6d04 74 bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
09908f00
SL
75 sp = getservbyname("whois", "tcp");
76 if (sp == NULL) {
f3bbfed6 77 (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
09908f00
SL
78 exit(4);
79 }
80 sin.sin_port = sp->s_port;
0a2b6d04 81 if (connect(s, &sin, sizeof(sin)) < 0) {
09908f00
SL
82 perror("whois: connect");
83 exit(5);
84 }
85 sfi = fdopen(s, "r");
86 sfo = fdopen(s, "w");
87 if (sfi == NULL || sfo == NULL) {
f3bbfed6
KB
88 perror("whois: fdopen");
89 (void)close(s);
09908f00
SL
90 exit(1);
91 }
9f48c250
KB
92 while (argc-- > 1)
93 (void)fprintf(sfo, "%s ", *argv++);
f3bbfed6 94 (void)fprintf(sfo, "%s\r\n", *argv);
067198bd 95 (void)fflush(sfo);
f3bbfed6
KB
96 while ((ch = getc(sfi)) != EOF)
97 putchar(ch);
0a2b6d04 98 exit(0);
09908f00 99}
067198bd 100
067198bd
KB
101usage()
102{
9f48c250 103 (void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
067198bd
KB
104 exit(1);
105}