xref: /original-bsd/usr.bin/whois/whois.c (revision f1d75c93)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)whois.c	5.12 (Berkeley) 05/24/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <stdio.h>
23 
24 #define	NICHOST	"whois.internic.net"
25 
26 main(argc, argv)
27 	int argc;
28 	char **argv;
29 {
30 	extern char *optarg;
31 	extern int optind;
32 	register FILE *sfi, *sfo;
33 	register int ch;
34 	struct sockaddr_in sin;
35 	struct hostent *hp;
36 	struct servent *sp;
37 	int s;
38 	char *host;
39 
40 	host = NICHOST;
41 	while ((ch = getopt(argc, argv, "h:")) != EOF)
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 
53 	if (!argc)
54 		usage();
55 
56 	hp = gethostbyname(host);
57 	if (hp == NULL) {
58 		(void)fprintf(stderr, "whois: %s: ", host);
59 		herror((char *)NULL);
60 		exit(1);
61 	}
62 	host = hp->h_name;
63 	s = socket(hp->h_addrtype, SOCK_STREAM, 0);
64 	if (s < 0) {
65 		perror("whois: socket");
66 		exit(1);
67 	}
68 	bzero((caddr_t)&sin, sizeof (sin));
69 	sin.sin_family = hp->h_addrtype;
70 	if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
71 		perror("whois: bind");
72 		exit(1);
73 	}
74 	bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
75 	sp = getservbyname("whois", "tcp");
76 	if (sp == NULL) {
77 		(void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
78 		exit(1);
79 	}
80 	sin.sin_port = sp->s_port;
81 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
82 		perror("whois: connect");
83 		exit(1);
84 	}
85 	sfi = fdopen(s, "r");
86 	sfo = fdopen(s, "w");
87 	if (sfi == NULL || sfo == NULL) {
88 		perror("whois: fdopen");
89 		(void)close(s);
90 		exit(1);
91 	}
92 	while (argc-- > 1)
93 		(void)fprintf(sfo, "%s ", *argv++);
94 	(void)fprintf(sfo, "%s\r\n", *argv);
95 	(void)fflush(sfo);
96 	while ((ch = getc(sfi)) != EOF)
97 		putchar(ch);
98 	exit(0);
99 }
100 
101 usage()
102 {
103 	(void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
104 	exit(1);
105 }
106