xref: /original-bsd/usr.bin/whois/whois.c (revision d0e3910b)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)whois.c	5.3 (Berkeley) 02/08/88";
15 #endif not lint
16 
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 
20 #include <netinet/in.h>
21 
22 #include <stdio.h>
23 #include <netdb.h>
24 
25 #define	NICHOST	"sri-nic.arpa"
26 
27 main(argc, argv)
28 	int argc;
29 	char *argv[];
30 {
31 	int s;
32 	register FILE *sfi, *sfo;
33 	register char c;
34 	char *host = NICHOST;
35 	struct sockaddr_in sin;
36 	struct hostent *hp;
37 	struct servent *sp;
38 
39 	argc--, argv++;
40 	if (argc > 2 && strcmp(*argv, "-h") == 0) {
41 		argv++, argc--;
42 		host = *argv++;
43 		argc--;
44 	}
45 	if (argc != 1) {
46 		fprintf(stderr, "usage: whois [ -h host ] name\n");
47 		exit(1);
48 	}
49 	hp = gethostbyname(host);
50 	if (hp == NULL) {
51 		fprintf(stderr, "whois: %s: host unknown\n", host);
52 		exit(1);
53 	}
54 	host = hp->h_name;
55 	s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
56 	if (s < 0) {
57 		perror("whois: socket");
58 		exit(2);
59 	}
60 	bzero((caddr_t)&sin, sizeof (sin));
61 	sin.sin_family = hp->h_addrtype;
62 	if (bind(s, &sin, sizeof (sin), 0) < 0) {
63 		perror("whois: bind");
64 		exit(3);
65 	}
66 	bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
67 	sp = getservbyname("whois", "tcp");
68 	if (sp == NULL) {
69 		fprintf(stderr, "whois: whois/tcp: unknown service\n");
70 		exit(4);
71 	}
72 	sin.sin_port = sp->s_port;
73 	if (connect(s, &sin, sizeof (sin), 0) < 0) {
74 		perror("whois: connect");
75 		exit(5);
76 	}
77 	sfi = fdopen(s, "r");
78 	sfo = fdopen(s, "w");
79 	if (sfi == NULL || sfo == NULL) {
80 		perror("fdopen");
81 		close(s);
82 		exit(1);
83 	}
84 	fprintf(sfo, "%s\r\n", *argv);
85 	fflush(sfo);
86 	while ((c = getc(sfi)) != EOF)
87 		putchar(c);
88 }
89