home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / uumail2 / getpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  6.5 KB  |  277 lines

  1. /*
  2.  * Name: getpath -- return the full usenet path of the given name
  3.  *
  4.  * Paramaters: sysname (input) -- The system name to be expanded
  5.  *           pathname (output)  The usenet path of the given system name
  6.  *           pathfile (input) the file to search for the system name
  7.  *
  8.  * Returns: EX_OK     -- path found
  9.  *        EX_NOHOST -- path not found
  10.  *        EX_NOINPUT-- unable to open usemap
  11.  *          EX_TEMPFAIL -- database being rebuilt
  12.  *
  13.  * Author: J. Donnelly   3/82
  14.  *
  15.  */
  16.  
  17. /*    IF YOU ARE USING A DBM DATABASE, READ THIS!
  18.  *    If the special sentinel value of @@@ is not present in the
  19.  *    database, then it is assumed that the database is being
  20.  *    rebuilt and the requesting process is blocked for TIMEOUT
  21.  *    (default = 180) seconds.  If, after 5 such blocks, the
  22.  *    sentinel is not present, the error code EX_TEMPFAIL is returned.
  23.  *    The same is true if the dbm files cannot be initialized.
  24.  */
  25.  
  26. /* 22-jun-83 Sheppard
  27.  * modified to rewind path file (if open), rather than open again
  28.  *
  29.  * $Log:    getpath.c,v $
  30.  * Revision 1.14  85/12/13  15:23:21  sob
  31.  * Added patches from umd-cs!steve
  32.  * 
  33.  * Revision 1.13  85/12/10  20:36:58  sob
  34.  * Added modifications suggested in gatech's version of uumail.
  35.  * Now, the DBM version of the database needs to have a SENTINAL in it
  36.  * to indicate that the DATABASE is not being updated. Also added similiar
  37.  * indicators to the non-DBM version to compare modification times and act
  38.  * accordingly. 
  39.  * 
  40.  * Revision 1.12  85/12/02  15:48:39  sob
  41.  * Combined speed hacks and old way of reading database and
  42.  * added compile flag SORTED. If database is SORTED and not DBM, use
  43.  * -DSORTED in CFLAGS to make it fast. If database is not sorted
  44.  * DO NOT use this flag.
  45.  * 
  46.  * Revision 1.11  85/11/24  15:03:41  sob
  47.  * Added changes suggested by regina!mark
  48.  * 
  49.  * Revision 1.10  85/11/24  04:21:45  sob
  50.  * Added efficiency hacks supplied by meccts!asby (Shane P. McCarron)
  51.  * 
  52.  * Revision 1.9  85/11/14  20:21:49  sob
  53.  * Added #ifdef DEBUG to allow compilation without DEBUG
  54.  * 
  55.  * Revision 1.8  85/11/08  03:04:49  sob
  56.  * release version
  57.  * 
  58.  * Revision 1.7  85/09/30  02:47:40  sob
  59.  * Altered to use path filename from global variable.
  60.  * 
  61.  * Revision 1.6  85/08/03  00:48:57  UUCP
  62.  * Cleaned up with lint.
  63.  * Stan Barber
  64.  * 
  65.  * Revision 1.5  85/07/19  17:45:13  UUCP
  66.  * Added \t as a valid seperation character for the database
  67.  * in the non DBM case. This is what pathalias uses.
  68.  * 
  69.  * Revision 1.4  85/07/19  16:44:07  UUCP
  70.  * revised to return proper things in accordance with sysexits
  71.  * Stan
  72.  * 
  73.  * Revision 1.3  85/07/11  19:30:31  sob
  74.  * added "uuconf.h" include file and deleted duplicated information
  75.  * 
  76.  * Revision 1.2  85/07/10  18:30:59  sob
  77.  * updated to add DBM capabilities
  78.  * Stan Barber, Baylor College of Medicine
  79.  * 
  80.  * Revision 1.1  85/07/10  18:03:28  sob
  81.  * Initial revision
  82.  * 
  83.  */
  84.  
  85. #include    "uuconf.h"
  86. #ifndef DBM
  87. #include <sys/types.h>
  88. #include <sys/stat.h>
  89. #endif
  90.  
  91. static char rcsid[] = "$Header: getpath.c,v 1.14 85/12/13 15:23:21 sob Exp $";
  92.  
  93. FILE * fopen (), *in;
  94.  
  95. int getpath (sysname, pathname,pathfile)
  96. char   *sysname, *pathname,*pathfile;
  97. {
  98.     int retval,indx;
  99. #ifdef DBM
  100.     datum lhs,rhs;
  101. #else
  102.     struct stat st;
  103.         time_t modtime;
  104. #ifdef SORTED
  105.     int scomp();
  106.  
  107.     long lo,hi;
  108.     long cur;
  109.     long last;
  110.     static char buf[256];
  111. #else
  112.     char    name[NAMESIZ],*p,t;
  113. #endif
  114. #endif
  115.  
  116. #ifdef DEBUG
  117. if (Debug>2)
  118.     printf("In getpath: Sysname = %s, Pathfile = %s\n",sysname,paths);
  119. #endif
  120.  
  121. #ifdef DBM
  122.     for (indx = 0; indx < 5; indx++)
  123.     {
  124.     if ((retval = dbminit (pathfile)) >= 0)
  125.         break;
  126.     
  127. #ifdef DEBUG
  128.     if (Debug>2)
  129.         fprintf (stderr, "Database unavailable.  Sleeping.\n");
  130. #endif
  131.     sleep (TIMEOUT);
  132.     }
  133.  
  134.     if (retval < 0)
  135.     return(EX_NOINPUT);
  136.  
  137.     lhs.dptr = SENTINEL;
  138.     lhs.dsize = strlen (SENTINEL) + 1;
  139.     for (indx = 0; indx < 5; indx++)
  140.     {
  141.     rhs = fetch (lhs);
  142.     if (rhs.dsize > 0)
  143.         break;
  144.     
  145. #ifdef DEBUG
  146.     if (Debug>2)
  147.         fprintf (stderr, "Database incomplete.  Sleeping.\n");
  148. #endif
  149.     sleep (TIMEOUT);
  150.     }
  151.     if (rhs.dsize <= 0)
  152.     return(EX_TEMPFAIL);
  153.  
  154.         lhs.dptr = sysname;
  155.     lhs.dsize = strlen(sysname)+1;
  156.     rhs = fetch(lhs);
  157.     if (rhs.dptr == NULL) return(EX_NOHOST); /* no name found */
  158.     strcpy(pathname,rhs.dptr);
  159.         return(EX_OK);            /* system name found */
  160.  
  161. #else
  162. if (in == NULL) 
  163.     {
  164.     for (indx = 0; indx < 5; indx++)
  165.         {
  166.         if ((in = fopen(pathfile, "r")) != NULL)
  167.             break;
  168.     
  169. #ifdef DEBUG
  170.         if (Debug>2)
  171.             fprintf (stderr, "Database unavailable.  Sleeping.\n");
  172. #endif
  173.         sleep (TIMEOUT);
  174.         }
  175.     if (in == NULL)
  176.     return(EX_NOINPUT);
  177.     }
  178.     else
  179.     rewind(in);
  180.     indx = 0;
  181. restart:
  182.     indx++;
  183.     if (indx > 5) return(EX_TEMPFAIL);
  184.     stat(pathfile, &st);
  185.     modtime=st.st_mtime; /* original last modification time */
  186.  
  187. #ifdef SORTED
  188.     lo = 0;
  189.     hi = st.st_size;
  190.     last = 0;
  191.     cur = hi >> 1;
  192.  
  193.     while (cur > lo && cur < hi)
  194.     {
  195.         stat(pathfile,&st);
  196.         if (st.st_mtime > modtime) goto restart;
  197.         fseek(in, cur, 0);
  198.         fgets(buf, sizeof(buf), in);
  199.         cur = ftell(in);
  200.         fgets(buf, sizeof(buf), in);
  201.  
  202. #ifdef    DEBUG
  203.     if (Debug > 4)
  204.         printf("Found site %s\n", buf);
  205. #endif
  206.         if (scomp(sysname, buf) < 0) hi = cur;
  207.         else
  208.         if (scomp(sysname, buf) > 0) lo = cur;
  209.         else
  210.         {
  211.             buf[strlen(buf)-1] = '\0';
  212.             strcpy(pathname, (char *)index(buf, '\t') + 1);
  213.             return(EX_OK);
  214.         }
  215.         cur = lo + ((hi - lo)>>1);
  216.         if (last == cur) 
  217.         {
  218.             fseek(in, lo, 0);
  219.             do
  220.             {
  221.                 fgets(buf, sizeof(buf), in);
  222.                 lo = ftell(in);
  223.                 if (scomp(sysname, buf) == 0)
  224.                 {
  225.                     buf[strlen(buf)-1] = '\0';
  226.                     strcpy(pathname, (char *)index(buf, '\t') + 1);
  227.                     return(EX_OK);
  228.                 }
  229.             } while (lo <= hi);
  230.             break;
  231.         } /* end if */
  232.         last = cur;
  233.     } /* end while */
  234.     return(EX_NOHOST);
  235. #else
  236.  
  237.     for (;;)
  238.     {
  239.     p = &name[0];
  240.     while ((t = getc(in)) != EOF && (*p++ = t) != ' ' && t != '\t'); /* read the system name */
  241.         stat(pathfile,&st);
  242.         if (st.st_mtime > modtime) goto restart;        /* database update in progress */
  243.     if( t == EOF) return(EX_NOHOST);
  244.     *--p = '\0';                    /* set end of string */
  245.     p = &name[0];
  246. #ifdef DEBUG
  247.     if (Debug>4) printf("Found %s\n",p);
  248. #endif
  249.     if (strcmp (p,sysname) == 0)
  250.         break;
  251.     while ((getc (in) != '\n'));            /* skip this path */
  252.     }
  253.     p = pathname;                    /* save start loc of pathname */
  254.     while ((*pathname++ = getc (in)) != '\n');
  255.     *--pathname = '\0';
  256.     pathname = p;
  257.     return(EX_OK);            /* system name found */
  258.  
  259. #endif
  260. #endif
  261. }
  262.  
  263. #ifdef    SORTED
  264. #define MAPTAB(c) ((c=='\t')?'\0':c)
  265.  
  266. int scomp(a,b)
  267. char *a,*b;
  268. {
  269. int r;
  270.     while (!(r=(MAPTAB(*a)-MAPTAB(*b))) && *a && *b && *a!='\t' && *b!='\t')
  271.     {
  272.            a++; b++;
  273.     }
  274.     return(r);
  275. }
  276. #endif
  277.