setgid to group "write" so that terminals need not be world writable
[unix-history] / usr / src / usr.bin / whois / whois.c
CommitLineData
f42904bc
DF
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
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
09908f00 13#ifndef lint
5d891393 14static char sccsid[] = "@(#)whois.c 5.2 (Berkeley) %G%";
f42904bc 15#endif not lint
09908f00
SL
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
5d891393 25#define NICHOST "sri-nic.arpa"
09908f00
SL
26
27main(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++;
5aa2f89a 40 if (argc > 2 && strcmp(*argv, "-h") == 0) {
09908f00
SL
41 argv++, argc--;
42 host = *argv++;
43 argc--;
44 }
5aa2f89a
RC
45 if (argc != 1) {
46 fprintf(stderr, "usage: whois [ -h host ] name\n");
47 exit(1);
48 }
09908f00
SL
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 sin.sin_family = hp->h_addrtype;
61 if (bind(s, &sin, sizeof (sin), 0) < 0) {
62 perror("whois: bind");
63 exit(3);
64 }
65 bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
66 sp = getservbyname("whois", "tcp");
67 if (sp == NULL) {
68 fprintf(stderr, "whois: whois/tcp: unknown service\n");
69 exit(4);
70 }
71 sin.sin_port = sp->s_port;
72 if (connect(s, &sin, sizeof (sin), 0) < 0) {
73 perror("whois: connect");
74 exit(5);
75 }
76 sfi = fdopen(s, "r");
77 sfo = fdopen(s, "w");
78 if (sfi == NULL || sfo == NULL) {
79 perror("fdopen");
80 close(s);
81 exit(1);
82 }
83 fprintf(sfo, "%s\r\n", *argv);
84 fflush(sfo);
85 while ((c = getc(sfi)) != EOF)
86 putchar(c);
87}