home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / whois / whois.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  3.7 KB  |  132 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)whois.c    5.11 (Berkeley) 3/2/91";
  42. #endif /* not lint */
  43.  
  44. #include <sys/types.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <netdb.h>
  48. #include <stdio.h>
  49.  
  50. #define    NICHOST    "nic.ddn.mil"
  51.  
  52. main(argc, argv)
  53.     int argc;
  54.     char **argv;
  55. {
  56.     extern char *optarg;
  57.     extern int optind;
  58.     register FILE *sfi, *sfo;
  59.     register int ch;
  60.     struct sockaddr_in sin;
  61.     struct hostent *hp;
  62.     struct servent *sp;
  63.     int s;
  64.     char *host;
  65.  
  66.     host = NICHOST;
  67.     while ((ch = getopt(argc, argv, "h:")) != EOF)
  68.         switch((char)ch) {
  69.         case 'h':
  70.             host = optarg;
  71.             break;
  72.         case '?':
  73.         default:
  74.             usage();
  75.         }
  76.     argc -= optind;
  77.     argv += optind;
  78.  
  79.     if (!argc)
  80.         usage();
  81.  
  82.     hp = gethostbyname(host);
  83.     if (hp == NULL) {
  84.         (void)fprintf(stderr, "whois: %s: ", host);
  85.         herror((char *)NULL);
  86.         exit(1);
  87.     }
  88.     host = hp->h_name;
  89.     s = socket(hp->h_addrtype, SOCK_STREAM, 0);
  90.     if (s < 0) {
  91.         perror("whois: socket");
  92.         exit(1);
  93.     }
  94.     bzero((caddr_t)&sin, sizeof (sin));
  95.     sin.sin_family = hp->h_addrtype;
  96.     if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  97.         perror("whois: bind");
  98.         exit(1);
  99.     }
  100.     bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
  101.     sp = getservbyname("whois", "tcp");
  102.     if (sp == NULL) {
  103.         (void)fprintf(stderr, "whois: whois/tcp: unknown service\n");
  104.         exit(1);
  105.     }
  106.     sin.sin_port = sp->s_port;
  107.     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  108.         perror("whois: connect");
  109.         exit(1);
  110.     }
  111.     sfi = fdopen(s, "r");
  112.     sfo = fdopen(s, "w");
  113.     if (sfi == NULL || sfo == NULL) {
  114.         perror("whois: fdopen");
  115.         (void)close(s);
  116.         exit(1);
  117.     }
  118.     while (argc-- > 1)
  119.         (void)fprintf(sfo, "%s ", *argv++);
  120.     (void)fprintf(sfo, "%s\r\n", *argv);
  121.     (void)fflush(sfo);
  122.     while ((ch = getc(sfi)) != EOF)
  123.         putchar(ch);
  124.     exit(0);
  125. }
  126.  
  127. usage()
  128. {
  129.     (void)fprintf(stderr, "usage: whois [-h hostname] name ...\n");
  130.     exit(1);
  131. }
  132.