home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / comm / mail / smail / src / rcs / getpath.c,v < prev    next >
Encoding:
Text File  |  1993-12-21  |  3.4 KB  |  172 lines

  1. head    1.2;
  2. access;
  3. symbols
  4.     C_1:1.2;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    93.09.18.16.47.47;    author Aussem;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    93.09.08.16.27.13;    author Aussem;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @analyse and return the path
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @insert GNU license text in the header
  28. @
  29. text
  30. @/*
  31.  *  getpath.c
  32.  *
  33.  *  Routines to lookup path for an address
  34.  *
  35.  * This program is free software; you can redistribute it and/or
  36.  * modify it under the terms of the GNU General Public License as
  37.  * published by the Free Software Foundation; either version 2 of
  38.  * the License, or (at your option) any later version.
  39.  *
  40.  * This program is distributed in the hope that it will be useful,
  41.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  43.  * General Public License for more details.
  44.  *
  45.  * You should have received a copy of the GNU General Public License
  46.  * along with this program; if not, write to the Free Software
  47.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  48.  *
  49.  * $Log: getpath.c,v $
  50.  * Revision 1.1  1993/09/08  16:27:13  Aussem
  51.  * Initial revision
  52.  *
  53.  *
  54.  */
  55.  
  56. static char     *rcsid="$Id: getpath.c,v 1.1 1993/09/08 16:27:13 Aussem Exp Aussem $";
  57.  
  58. # include    <stdio.h>
  59. # include    <sys/types.h>
  60. # include    <ctype.h>
  61. # include    "defs.h"
  62.  
  63. extern enum edebug debug;    /* how verbose we are         */ 
  64. extern char *pathfile;        /* location of path database    */
  65.  
  66. /*
  67. **
  68. ** getpath(): look up key in ascii sorted path database.
  69. **
  70. */
  71.  
  72. getpath( key, path , cost)
  73. char *key;        /* what we are looking for */
  74. char *path;        /* where the path results go */
  75. int *cost;        /* where the cost results go */
  76. {
  77.     long pos, middle, hi, lo;
  78.     static long pathlength = 0;
  79.     register char *s;
  80.     int c;
  81.     static FILE *file;
  82.     int flag;
  83.  
  84. DEBUG("getpath: looking for '%s'\n", key);
  85.  
  86.     if(pathlength == 0) {    /* open file on first use */
  87.         if((file = fopen(pathfile, "r")) == NULL) {
  88.             (void) printf("can't access %s.\n", pathfile);
  89.             pathlength = -1;
  90.         } else {
  91.             (void) fseek(file, 0L, 2);    /* find length */
  92.             pathlength = ftell(file);
  93.         }
  94.     }
  95.     if( pathlength == -1 )
  96.         return( EX_OSFILE );
  97.  
  98.     lo = 0;
  99.     hi = pathlength;
  100.     (void) strcpy( path, key );
  101.     (void) strcat( path, "\t" );
  102. /*
  103. ** "Binary search routines are never written right the first time around."
  104. ** - Robert G. Sheldon.
  105. */
  106.     for( ;; ) {
  107.         pos = middle = ( hi+lo+1 )/2;
  108.         (void) fseek(file, pos, 0);    /* find midpoint */
  109.         if(pos != 0)
  110.             while(((c = getc(file)) != EOF) && (c != '\n'))
  111.                 ;    /* go to beginning of next line */
  112.         if(c == EOF) {
  113.             return(EX_NOHOST);
  114.         }
  115.         for( flag = 0, s = path; flag == 0; s++ ) { /* match??? */
  116.             if( *s == '\0' ) {
  117.                 goto solved;
  118.             }
  119.             if((c = getc(file)) == EOF) {
  120.                 return(EX_NOHOST);
  121.             }
  122.             flag = lower(c) - lower(*s);
  123.         } 
  124.         if(lo >= middle) {        /* failure? */
  125.             return(EX_NOHOST);
  126.         }
  127.         if((c != EOF) && (flag < 0)) {    /* close window */
  128.             lo = middle;
  129.         } else {
  130.             hi = middle - 1;
  131.         }
  132.     }
  133. /* 
  134. ** Now just copy the result.
  135. */
  136. solved:
  137.     while(((c  = getc(file)) != EOF) && (c != '\t') && (c != '\n')) {
  138.         *path++ = c;
  139.     }
  140.     *path = '\0';
  141. /*
  142. ** See if the next field on the line is numeric.
  143. ** If so, use it as the cost for the route.
  144. */
  145.     if(c == '\t') {
  146.         int tcost = -1;
  147.         while(((c = getc(file)) != EOF) && isdigit(c)) {
  148.             if(tcost < 0) tcost = 0;
  149.             tcost *= 10;
  150.             tcost += c - '0';
  151.         }
  152.         if(tcost >= 0) *cost = tcost;
  153.     }
  154.     return (EX_OK);
  155. }
  156. @
  157.  
  158.  
  159. 1.1
  160. log
  161. @Initial revision
  162. @
  163. text
  164. @d6 4
  165. a9 1
  166.  * $Log$
  167. d11 14
  168. d27 1
  169. a27 1
  170. static char     *rcsid="$Id$";
  171. @
  172.