home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / uuhost-2.1 / uulookup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-18  |  5.4 KB  |  287 lines

  1. /*
  2.  * UUHOST, Copyright 1992,   Jan-Piet Mens [Logix GmbH, Wiesbaden, Germany]
  3.  * License to freely use and distribute this software is hereby granted 
  4.  * by the author, subject to the condition that this copyright notice 
  5.  * remains intact.  The author retains the exclusive right to publish 
  6.  * derivative works based on this work, including, but not limited
  7.  * to, revised versions of this work.
  8.  *
  9.  * $Id: uulookup.c,v 2.3 1992/01/16 09:40:58 jpm Exp $
  10.  *
  11.  * $Log: uulookup.c,v $
  12.  * Revision 2.3  1992/01/16  09:40:58  jpm
  13.  * Cleanup
  14.  *
  15.  * Revision 2.2  1991/10/22  18:14:09  jpm
  16.  * Added -u option. Thanks to Pat Myrto <pat@rwing> for suggesting it.
  17.  *
  18.  * Revision 2.1  1991/10/19  14:25:54  jpm
  19.  * *** empty log message ***
  20.  *
  21.  *
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #ifdef NEED_STRINGS_H
  27. # include <strings.h>
  28. #else
  29. # include <string.h>
  30. #endif
  31. #define MAXBUF    1024
  32.  
  33. #if defined(__STDC__) || defined(__cplusplus)
  34. # define P_(s) s
  35. #else
  36. # define P_(s) ()
  37. #endif
  38.  
  39. int main P_((int argc, char **argv));
  40. void splitpath P_((char *usr, char *adr));
  41. int look P_((char *key, char *result));
  42. int strsplit P_((register char *s, int sep, char **arg));
  43. void usage P_((void));
  44. int getopt();
  45. void exit();
  46.  
  47. #undef P_
  48. #define FALSE        0
  49. #define TRUE        1
  50. #define MAXDOMS        20
  51.  
  52. char *progname;
  53. char *database;            /* For look() */
  54.  
  55. #ifndef lint
  56. static char rcs_id[] = "@(#)$Id: uulookup.c,v 2.3 1992/01/16 09:40:58 jpm Exp $";
  57. #endif
  58. void usage();
  59.  
  60. int main(argc, argv)
  61. int argc;
  62. char **argv;
  63. {
  64.     char addr[100], user[100], buf[MAXBUF];
  65.     extern char *optarg;
  66.     char *doms[MAXDOMS];            /* Each component of domain */
  67.     int count, i, c, hf = FALSE, mf = FALSE, uf = FALSE;
  68.     int verbose = FALSE, found;
  69.  
  70.     progname = argv[0];
  71.     (void)strcpy(user, "");
  72.  
  73.     while ((c = getopt(argc, argv, "vh:m:u:")) != EOF)
  74.         switch (c)
  75.         {
  76.             case 'h':
  77.                 database = UUHOSTINDEX;
  78.                 (void)strcpy(addr, optarg);
  79.                 hf = TRUE;
  80.                 break;
  81.             case 'm':
  82.                 database = PATHALIAS;
  83.                 (void)strcpy(addr, optarg);
  84.                 mf = TRUE;
  85.                 break;
  86.             case 'u':
  87.                 database = PATHALIAS;
  88.                 (void)strcpy(user, optarg);
  89.                 uf = TRUE;
  90.                 break;
  91.             case 'v':
  92.                 verbose = TRUE;
  93.                 break;
  94.             default:
  95.                 usage();
  96.         }
  97.  
  98.     if (hf + mf + uf != 1)        /* Options are exclusive */
  99.         usage();
  100.  
  101.     if (uf)
  102.         splitpath(user, addr);
  103.  
  104.     /*
  105.      * Split addr into a list of domains. If `addr' has foo.bar.top.level
  106.      * it will get split into
  107.      *
  108.      *    foo.bar.top.level
  109.      *    .bar.top.level
  110.      *    .top.level
  111.      *    .level
  112.      */
  113.  
  114.     count = strsplit(addr, '.', doms);
  115.  
  116.     /*
  117.      * Forget about the top-level domains. They are not in (my) 
  118.      * PATHALIAS.
  119.      */
  120.  
  121.     --count;
  122.  
  123.     if (verbose)
  124.         (void)printf("Trying '%s'\n", addr);
  125.  
  126.     found = FALSE;
  127.     if (look(addr, buf) != 0)
  128.     {
  129.         for (i = 0; i < count; i++)
  130.         {
  131.             if (verbose)
  132.                 (void)printf("Trying '%s'\n", doms[i]);
  133.             if (look(doms[i] - 1, buf) == 0     /* With dot */
  134.                 || look(doms[i] - 1, buf) == 0)    /* W/o dot */
  135.             {
  136.                 found = TRUE;
  137.                 break;
  138.             }
  139.         }
  140.     }
  141.     else found = TRUE;
  142.  
  143.     if (found == FALSE)
  144.         if (hf)
  145.             return fprintf(stderr, "HOST: '%s' not found\n", addr);
  146.         else 
  147.             return fprintf(stderr, "Path to '%s' not found\n",addr);
  148.  
  149.     if (uf == TRUE)
  150.     {
  151.         (void)printf(buf, user);
  152.         (void)putchar('\n');    
  153.     }
  154.     else if (mf == TRUE)
  155.         (void)printf("Path to %s\n\t%s\n", addr, buf);
  156.     else
  157.         (void)printf("%s %s\n", addr, buf);
  158.     return (0);
  159. }
  160.  
  161. void splitpath(usr, adr)
  162. char *usr;                /* Contains user@site or site!user */
  163. char *adr;                /* Target will contain "site"  */
  164. {
  165.     char *strchr(), *ptr, a[100], u[100];
  166.  
  167.     /*
  168.      *  user@site  ?
  169.      *  Copy "site" into `adr' and chop `usr' at '@'
  170.      */
  171.  
  172.     if ((ptr = strchr(usr, '@')) != (char *)0)
  173.     {
  174.         (void)strcpy(a, ptr + 1);
  175.         *(ptr) = '\0';
  176.         (void)strcpy(u, usr);
  177.     }
  178.     else if ((ptr = strchr(usr, '!')) != (char *)0)
  179.     {
  180.         (void)strcpy(u, ptr + 1);
  181.         *(ptr) = '\0';
  182.         (void)strcpy(a, usr);
  183.     }
  184.     else 
  185.     {
  186.         (void)strcpy(a, usr);
  187.         (void)strcpy(u, "%s");
  188.     }
  189.  
  190.     (void)strcpy(adr, a);
  191.     (void)strcpy(usr, u);
  192. }
  193.     
  194. /*
  195.  * From "Portable C Software by Mark Horton"
  196.  */
  197.  
  198. int look(key, result)
  199. char *key, *result;
  200. {
  201.     long pos, middle, hi, lo;
  202.     static long pathlength = 0;
  203.     register char *s;
  204.     int c, flag;
  205.     static FILE *fp;
  206.  
  207.     if (!pathlength)
  208.     {
  209.         if ((fp = fopen(database,"r")) == (FILE *)0) {
  210.             (void)perror(database);
  211.             pathlength = -1;
  212.         }
  213.         else {
  214.             (void)fseek(fp, 0L, 2);
  215.             pathlength = ftell(fp);
  216.         }
  217.     }
  218.  
  219.     if (pathlength == -1)
  220.         return (-2);            /* No database file */
  221.  
  222.     lo = 0;
  223.     hi = pathlength;
  224.     (void)strcpy(result, key);
  225.     (void)strcat(result, "\t");
  226.  
  227.     while (1)
  228.     {
  229.         pos = middle = (hi + lo + 1) /2;
  230.         (void)fseek(fp, pos, 0);        /* Find midpoint */
  231.         if (pos)            /*  to begin of next line */
  232.             while ((c = getc(fp)) != EOF && c != '\n')
  233.                 ;    /* Nothing */
  234.             
  235.         for (flag = 0, s = result; !flag; s++) {    /* Match ? */
  236.             if (*s == '\0')
  237.                 goto solved;
  238.             c = getc(fp);
  239.             flag = tolower(c) - tolower(*s);
  240.         }
  241.         if (lo >= middle)        /* Failure ? */
  242.             return (-1);
  243.         if (c != EOF && flag < 0)    /* Close window */
  244.             lo = middle;
  245.         else
  246.             hi = middle - 1;
  247.     }
  248. solved:            /* Just copy result... */
  249.     while ((c = getc(fp)) != EOF && c != '\n')
  250.         *result++ = c;
  251.     *result = '\0';
  252.     return (0);
  253. }
  254.  
  255. int strsplit(s, sep, arg)
  256. register char *s;
  257. int sep;
  258. char **arg;
  259. {
  260.     int count = 0;
  261.     int wastoken = 0;
  262.  
  263.     for (; *s; ++s)
  264.     {
  265.         if (!wastoken)
  266.         {
  267.             ++count;
  268.             *arg++ = s;
  269.         }
  270.         wastoken = (sep != *s);
  271.     }
  272.  
  273.     if (!wastoken)
  274.     {
  275.         ++count;
  276.         *arg++ = s;
  277.     }
  278.     *arg = (char *)0;
  279.     return (count);
  280. }
  281.  
  282. void usage()
  283. {
  284.     (void)fprintf(stderr, "Usage: %s [-h hostname] [-m mailname] [-u user] [-v]\n", progname);
  285.     exit(1);
  286. }
  287.