home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / fingercl.zip / finger.c < prev    next >
Text File  |  1997-08-29  |  6KB  |  263 lines

  1. /* finger.c -- The program that users directly interact with. */
  2.  
  3. /* Copyright (C) 1988, 1990, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Finger.
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2, or (at your option)
  10.    any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <config.h>
  25. #include <setjmp.h>
  26. #include <netdb.h>
  27. #include <sys/types.h>
  28. #include <sys/file.h>
  29.  
  30. #include <general.h>
  31. #include <tcp.h>
  32. #include <error.h>
  33. #ifndef __EMX__
  34. #include <fingerpaths.h>
  35. #include <face-finger.h>
  36. #endif
  37. #include <text-finger.h>
  38. #include <netinet/in.h>
  39.  
  40. #include "getopt.h"
  41. extern int tcp_to_service(char *service, char *address);
  42. extern int default_error_handling (char *progname);
  43.  
  44. /* **************************************************************** */
  45. /*                                                                  */
  46. /*                            "finger"                              */
  47. /*                                                                  */
  48. /* **************************************************************** */
  49.  
  50. /* Our version of "finger". */
  51.  
  52. /* Possible values of the "options" to finger. */
  53. #define INFO 0x02
  54. #define VERS 0x04
  55.  
  56. void call_finger ();
  57. extern char *baseprefix ();
  58.  
  59.  
  60. static void
  61. usage (argc, argv)
  62.   int argc;
  63.   char **argv;
  64. {
  65.   fprintf (stderr,
  66.            "Usage: %s [-v] [--version] [-lim] [--info] [-f] [--faces]\n", baseprefix (*argv));
  67.   fprintf (stderr, "       [-sb] [--brief] [-P port#] [--port port#] [-h] [--help]\n");
  68.   fprintf (stderr, "       [user | user@host ...]\n");
  69.   exit (1);
  70. }
  71.  
  72.  
  73. static void
  74. help ()
  75. {
  76.   fputs ("This is (not quite) GNU Finger. Choose among the following options:\n\n", stderr);
  77.   fputs ("-v, \t\tdisplay version number\n", stderr);
  78.   fputs ("-i, -l, -m,\tdisplay full user information\n", stderr);
  79.   fputs ("-b, -s, \topposite of -i; only display login records\n", stderr);
  80.   fputs ("-P,    #p\tconnect to finger daemon using port or service #p\n", stderr);
  81.   fputs ("-h, \t\tdisplay this message\n", stderr);
  82.   fputc ('\n', stderr);
  83.  
  84.   exit (1);
  85. }
  86.  
  87.  
  88. main (argc, argv)
  89.   int argc;
  90.   char **argv;
  91. {
  92.   int arg_index, opt_index = 0;
  93.   long options;
  94.   char *arg, *port;
  95.   int optc;
  96.  
  97. #ifdef ERROR_HANDLER
  98. default_error_handling (argv[0]);
  99. #endif
  100.   /* Parse the arguments passed on the command line. */
  101.  
  102. #if defined (INFO_IS_DEFAULT)
  103.   options = INFO;
  104. #else
  105.   options = 0;
  106. #endif
  107.  
  108.   arg_index = 1;
  109.   port = NULL;
  110.  
  111.   while ((optc = getopt (argc, argv, "vfilmP:bsh")) > 0)
  112.     switch (optc)
  113.       {
  114.       case 'i':
  115.       case 'l':
  116.       case 'm':
  117.  
  118.         options |= INFO;
  119.         break;
  120.  
  121.       case 'b':
  122.       case 's':
  123.  
  124.         options &= ~INFO;
  125.         break;
  126.  
  127.       case 'v':
  128.  
  129.         options |= VERS;
  130.         break;
  131.  
  132.       case 'P':
  133.  
  134.         port = optarg;
  135.         break;
  136.  
  137.       case 'h':
  138.  
  139.         help ();
  140.  
  141.       default:
  142.  
  143.         usage (argc, argv);
  144.       }
  145.  
  146.   if (options & VERS)
  147.     printf ("(reduced) GNU Finger client version %s.\n", VERSION_STRING);
  148.  
  149. #ifdef BASENAME_IS_SERVICE
  150.   if (!port)
  151.     port = baseprefix (argv[0]);
  152. #endif
  153.  
  154.   if (optind >= argc)
  155.     call_finger ("", options, port);
  156.   else
  157.     for (arg_index = optind; arg_index < argc; arg_index++)
  158.       call_finger (argv[arg_index], options, port, arg_index,argc);
  159.  
  160.   exit (1);
  161. }
  162.  
  163. /* Call the finger daemon to actually do the fingering. */
  164. void
  165. call_finger (arg, options, portnum, arg_ind, argcount)
  166.   char *arg, *portnum;
  167.   long options;
  168.   int arg_ind, argcount;
  169. {
  170.   char *t;
  171.   char *username, *hostname;
  172.   char *localhost;
  173.   struct hostent *host = NULL;
  174.   int connection, info;
  175.   long addr;
  176.    int suppress_hostname = 0;
  177.   int n_flag=0;
  178.  
  179.   username = savestring (arg);
  180.   info = options & INFO;
  181.  
  182.  
  183.   hostname = NULL;
  184.       for (t = username; *t && *t != '@'; t++);
  185.  
  186.       if (*t)
  187.         hostname = t + 1;
  188.  
  189.       *t = '\0';
  190.  
  191.   if (!hostname)
  192.     {
  193.       if (!(localhost = xgethostname ()))
  194.         localhost = "localhost";
  195.  
  196.       hostname = localhost;
  197.       suppress_hostname = 1;
  198.     }
  199.  
  200.   if (digit (*hostname))
  201.     {
  202.       addr = (long)inet_addr (hostname);
  203.       host = gethostbyaddr ((const char *) &addr, 4, AF_INET);
  204.  
  205.       if (!host)
  206.         {
  207.           host = (struct hostent *) xmalloc (sizeof (struct hostent));
  208.          n_flag=1;
  209.           host->h_name = hostname;
  210.         }
  211.     }
  212.   else
  213.     {
  214.       host = gethostbyname (hostname);
  215.  
  216.       if (!host)
  217.         handle_error (FATAL, "%s: unknown host", hostname);
  218.  
  219.       bcopy (host->h_addr, &addr, 4);
  220.     }
  221.  
  222.   if (!suppress_hostname)
  223.     {
  224.       printf ("[%s]", host->h_name);
  225.       fflush (stdout);
  226.     }
  227.  
  228.   connection = tcp_to_service (portnum ? portnum : "finger", (char *) &addr);
  229.  
  230.   if (connection < 0)
  231.     {
  232.       printf ("\n");
  233.       fflush (stdout);
  234.       file_error (FATAL, host->h_name);
  235.     }
  236.  
  237.   if (!suppress_hostname)
  238.     {
  239.       printf ("\n");
  240.       fflush (stdout);
  241.     }
  242.  
  243.   /* Okay, connection established.  Now actually do the fingering. */
  244.    if (info && *username)
  245.         {
  246.           char *new_username = (char *) xmalloc (4 + strlen (username));
  247.           strcpy (new_username, "/W ");
  248.           strcat (new_username, username);
  249.           free (username);
  250.           username = new_username;
  251.  
  252.          if (arg_ind == argcount) free(new_username);
  253.         }
  254.  
  255.    text_finger (username, connection);
  256.  
  257.   close (connection);
  258.   free (username);
  259.  
  260. if (host && n_flag)
  261.     free (host);
  262. }
  263.