rationalized handling of child processes, cleaned up mail1 some more
[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
5e8b0e60 25static char sccsid[] = "@(#)whois.c 5.5 (Berkeley) %G%";
067198bd 26#endif /* not lint */
09908f00
SL
27
28#include <sys/types.h>
29#include <sys/socket.h>
30
31#include <netinet/in.h>
32
33#include <stdio.h>
34#include <netdb.h>
35
5d891393 36#define NICHOST "sri-nic.arpa"
09908f00
SL
37
38main(argc, argv)
39 int argc;
40 char *argv[];
41{
067198bd
KB
42 extern char *optarg;
43 extern int optind;
09908f00 44 register FILE *sfi, *sfo;
067198bd 45 register int c;
09908f00
SL
46 struct sockaddr_in sin;
47 struct hostent *hp;
48 struct servent *sp;
067198bd
KB
49 int ch, s;
50 char *host;
51
52 host = NICHOST;
53 while ((ch = getopt(argc, argv, "h")) != EOF)
54 switch((char)ch) {
55 case 'h':
56 host = optarg;
57 break;
58 case '?':
59 default:
60 usage();
61 }
62 argc -= optind;
63 argv += optind;
64
65 if (argc != 1)
66 usage();
09908f00 67
09908f00
SL
68 hp = gethostbyname(host);
69 if (hp == NULL) {
70 fprintf(stderr, "whois: %s: host unknown\n", host);
71 exit(1);
72 }
73 host = hp->h_name;
74 s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
75 if (s < 0) {
76 perror("whois: socket");
77 exit(2);
78 }
58e160ea 79 bzero((caddr_t)&sin, sizeof (sin));
09908f00
SL
80 sin.sin_family = hp->h_addrtype;
81 if (bind(s, &sin, sizeof (sin), 0) < 0) {
82 perror("whois: bind");
83 exit(3);
84 }
85 bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
86 sp = getservbyname("whois", "tcp");
87 if (sp == NULL) {
88 fprintf(stderr, "whois: whois/tcp: unknown service\n");
89 exit(4);
90 }
91 sin.sin_port = sp->s_port;
92 if (connect(s, &sin, sizeof (sin), 0) < 0) {
93 perror("whois: connect");
94 exit(5);
95 }
96 sfi = fdopen(s, "r");
97 sfo = fdopen(s, "w");
98 if (sfi == NULL || sfo == NULL) {
99 perror("fdopen");
100 close(s);
101 exit(1);
102 }
103 fprintf(sfo, "%s\r\n", *argv);
067198bd 104 (void)fflush(sfo);
09908f00
SL
105 while ((c = getc(sfi)) != EOF)
106 putchar(c);
107}
067198bd
KB
108
109static
110usage()
111{
112 fprintf(stderr, "usage: whois [-h host] name\n");
113 exit(1);
114}