home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / bbs_ra / msgq160s.arj / USERLIST.C < prev    next >
Text File  |  1991-07-28  |  6KB  |  145 lines

  1. /*
  2.  * USERLIST.C - Fidolist processing
  3.  *
  4.  * Msged/Q message editor for QuickBBS  Copyright 1990 by P.J. Muller
  5.  *
  6.  */
  7.  
  8. /*--------------------------------------------------------------------------*/
  9. /*                                                                          */
  10. /*                                                                          */
  11. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  12. /*      \ 10001101 /         Writers and Distributors of                    */
  13. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  14. /*        \ 1011 /                                                          */
  15. /*         ------            KopyRong (K) 1987.  ALL RIGHTS REVERSED.       */
  16. /*                                                                          */
  17. /*                                                                          */
  18. /*               This module was written by Vince Perriello                 */
  19. /*                                                                          */
  20. /*                                                                          */
  21. /*                 BinkleyTerm Fidolist processing module                   */
  22. /*                                                                          */
  23. /*                                                                          */
  24. /*   This  software  package is being distributed WITH FULL SOURCE CODE     */
  25. /*   with the  following  conditions:    1)  If  anything awful happens     */
  26. /*   because  you  use    it   (or  don't  use  it),  you  accept  full     */
  27. /*   responsibility;  2) you  don't start making tons of voice calls to     */
  28. /*   the authors to complain or  make  suggestions  about enhancements,     */
  29. /*   useful or otherwise;  3) you  do not reuse this code in commercial     */
  30. /*   products without specific permission to do so  from  the  authors;     */
  31. /*   4) If you find any problems you send  fixes  to  the  authors  for     */
  32. /*   inclusion  in  updates;    5) You find some way  to  express  your     */
  33. /*   appreciation  for  this  method of distribution, either by writing     */
  34. /*   code or  application  notes,  or  just sending along a "Thank You"     */
  35. /*   message.                                                               */
  36. /*                                                                          */
  37. /*   There is  copyrighted  code  in  this product.  We either wrote it     */
  38. /*   ourselves or got  permission  to use it.  Please don't force us to     */
  39. /*   pay a lawyer --  have some respect for our motives and don't abuse     */
  40. /*   this "license".                                                        */
  41. /*                                                                          */
  42. /*                                        */
  43. /*   heavily modified 02 Aug 1988 for use in msged by jim nutt              */
  44. /*   further modified 28 oct 1988 for use in msged by jim nutt              */
  45. /*   used with permission                             */
  46. /*                                                                          */
  47. /*--------------------------------------------------------------------------*/
  48.  
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <io.h>
  52. #include <sys/stat.h>
  53.  
  54. #ifdef __MSC__
  55. #include <sys/types.h>
  56. #endif
  57.  
  58. #if defined(__TURBOC__) || defined(__MSC__) || defined(__JPIC__)
  59. #include <fcntl.h>
  60. #endif
  61.  
  62. #include "msged.h"
  63.  
  64. #define TEXTLEN 200
  65.  
  66. ADDRESS lookup(char *name, char *fn)
  67. {
  68.     int     low, high, mid, f, cond, namelen;
  69.     struct stat buf;
  70.         char    midname[TEXTLEN];
  71.         char    last_name_first[TEXTLEN];
  72.     char   *c, *p, *m;
  73.     int     reclength;
  74.         int     nrecs;
  75.         ADDRESS tmp;
  76.  
  77.  
  78.     memset(&tmp, 0, sizeof(tmp));
  79.  
  80.     c = midname;        /* Start of temp name buff   */
  81.     p = name;        /* Point to start of name    */
  82.     m = NULL;        /* Init pointer to space     */
  83.  
  84.     while ((*c = *p++) != NULL) {    /* Go entire length of name  */
  85.         if (*c == ' ')    /* Look for space            */
  86.             m = c;    /* Save location             */
  87.         c++;
  88.     }
  89.  
  90.     if (m != NULL) {    /* If we have a pointer,     */
  91.         *m++ = '\0';    /* Terminate the first half  */
  92.         strcpy(last_name_first, m);    /* Now copy the last name    */
  93.         strcat(last_name_first, ", ");    /* Insert a comma and space  */
  94.         strcat(last_name_first, midname);    /* Finally copy first
  95.                              * half   */
  96.     }
  97.     else
  98.         strcpy(last_name_first, midname);    /* Use whole name
  99.                              * otherwise  */
  100.  
  101.     strlwr(last_name_first);/* all lower case */
  102.     namelen = strlen(last_name_first);    /* Calc length now           */
  103.  
  104.         stat(fn, &buf);   /* get the file size */
  105.  
  106.         if ((f = open(fn, O_RDONLY)) == -1) {
  107.         reclength = -1;    /* Reset all on open failure */
  108.                 return (tmp);
  109.     }
  110.  
  111.         memset(midname, 0, sizeof(midname));
  112.         read(f, midname, sizeof(midname));   /* Read 1 record */
  113.     reclength = (int) (strchr(midname, '\n') - midname) + 1;    /* FindEnd */
  114.     nrecs = (int) (buf.st_size / reclength); /* Now get num of records    */
  115.  
  116.     /* Binary search algorithm */
  117.     low = 0;
  118.     high = nrecs - 1;
  119.     while (low <= high) {
  120.         mid = low + (high - low) / 2;
  121.         lseek(f, (long) ((long) mid * (long) reclength), SEEK_SET);
  122.         read(f, midname, reclength);
  123.         midname[reclength] = EOS;
  124.         strlwr(midname);
  125.         if ((cond = strncmp(last_name_first, midname, namelen)) < 0)
  126.             high = mid - 1;
  127.         else {
  128.             if (cond > 0)
  129.                 low = mid + 1;
  130.             else {
  131.                 /* Return the address information */
  132.                 close(f);
  133.  
  134.                 /*
  135.                  * The offset of 30 is just a number that
  136.                  * should work properly
  137.                  */
  138.                 return (parsenode(midname + 30));
  139.             }
  140.         }
  141.     }
  142.     close(f);
  143.         return (tmp);
  144. }
  145.