date and time created 89/08/23 19:36:24 by edward
[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 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
f42904bc
DF
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1980 Regents of the University of California.\n\
21 All rights reserved.\n";
067198bd 22#endif /* not lint */
f42904bc 23
09908f00 24#ifndef lint
f3bbfed6 25static char sccsid[] = "@(#)whois.c 5.7 (Berkeley) %G%";
067198bd 26#endif /* not lint */
09908f00
SL
27
28#include <sys/types.h>
29#include <sys/socket.h>
09908f00 30#include <netinet/in.h>
09908f00 31#include <netdb.h>
f3bbfed6 32#include <stdio.h>
09908f00 33
f3bbfed6 34#define NICHOST "nic.ddn.mil"
09908f00
SL
35
36main(argc, argv)
37 int argc;
f3bbfed6 38 char **argv;
09908f00 39{
067198bd
KB
40 extern char *optarg;
41 extern int optind;
09908f00 42 register FILE *sfi, *sfo;
f3bbfed6 43 register int ch;
09908f00
SL
44 struct sockaddr_in sin;
45 struct hostent *hp;
46 struct servent *sp;
f3bbfed6 47 int s;
067198bd
KB
48 char *host;
49
50 host = NICHOST;
b1c6645f 51 while ((ch = getopt(argc, argv, "h:")) != EOF)
067198bd
KB
52 switch((char)ch) {
53 case 'h':
54 host = optarg;
55 break;
56 case '?':
57 default:
58 usage();
59 }
60 argc -= optind;
61 argv += optind;
62
63 if (argc != 1)
64 usage();
09908f00 65
09908f00
SL
66 hp = gethostbyname(host);
67 if (hp == NULL) {
f3bbfed6 68 (void)fprintf(stderr, "whois: %s: ", host);
b1c6645f 69 herror((char *)NULL);
09908f00
SL
70 exit(1);
71 }
72 host = hp->h_name;
73 s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
74 if (s < 0) {
75 perror("whois: socket");
76 exit(2);
77 }
58e160ea 78 bzero((caddr_t)&sin, sizeof (sin));
09908f00
SL
79 sin.sin_family = hp->h_addrtype;
80 if (bind(s, &sin, sizeof (sin), 0) < 0) {
81 perror("whois: bind");
82 exit(3);
83 }
84 bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
85 sp = getservbyname("whois", "tcp");
86 if (sp == NULL) {
f3bbfed6 87 (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
09908f00
SL
88 exit(4);
89 }
90 sin.sin_port = sp->s_port;
91 if (connect(s, &sin, sizeof (sin), 0) < 0) {
92 perror("whois: connect");
93 exit(5);
94 }
95 sfi = fdopen(s, "r");
96 sfo = fdopen(s, "w");
97 if (sfi == NULL || sfo == NULL) {
f3bbfed6
KB
98 perror("whois: fdopen");
99 (void)close(s);
09908f00
SL
100 exit(1);
101 }
f3bbfed6 102 (void)fprintf(sfo, "%s\r\n", *argv);
067198bd 103 (void)fflush(sfo);
f3bbfed6
KB
104 while ((ch = getc(sfi)) != EOF)
105 putchar(ch);
09908f00 106}
067198bd
KB
107
108static
109usage()
110{
f3bbfed6 111 (void)fprintf(stderr, "usage: whois [-h host] name\n");
067198bd
KB
112 exit(1);
113}