changed INDEX operator to handle multiple subscript arrays correctly
[unix-history] / usr / src / usr.bin / whois / whois.c
#ifndef lint
static char sccsid[] = "@(#)whois.c 4.1 82/12/31";
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <netdb.h>
#define NICHOST "sri-nic"
main(argc, argv)
int argc;
char *argv[];
{
int s;
register FILE *sfi, *sfo;
register char c;
char *host = NICHOST;
struct sockaddr_in sin;
struct hostent *hp;
struct servent *sp;
argc--, argv++;
if (argc > 2) {
usage:
fprintf(stderr, "usage: whois [ -h host ] name\n");
exit(1);
}
if (strcmp(*argv, "-h") == 0) {
argv++, argc--;
host = *argv++;
argc--;
}
if (argc != 1)
goto usage;
hp = gethostbyname(host);
if (hp == NULL) {
fprintf(stderr, "whois: %s: host unknown\n", host);
exit(1);
}
host = hp->h_name;
s = socket(hp->h_addrtype, SOCK_STREAM, 0, 0);
if (s < 0) {
perror("whois: socket");
exit(2);
}
sin.sin_family = hp->h_addrtype;
if (bind(s, &sin, sizeof (sin), 0) < 0) {
perror("whois: bind");
exit(3);
}
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
sp = getservbyname("whois", "tcp");
if (sp == NULL) {
fprintf(stderr, "whois: whois/tcp: unknown service\n");
exit(4);
}
sin.sin_port = sp->s_port;
if (connect(s, &sin, sizeof (sin), 0) < 0) {
perror("whois: connect");
exit(5);
}
sfi = fdopen(s, "r");
sfo = fdopen(s, "w");
if (sfi == NULL || sfo == NULL) {
perror("fdopen");
close(s);
exit(1);
}
fprintf(sfo, "%s\r\n", *argv);
fflush(sfo);
while ((c = getc(sfi)) != EOF)
putchar(c);
}