home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / MSGDP206.SZH / USERLIST.C < prev    next >
C/C++ Source or Header  |  1990-07-02  |  5KB  |  146 lines

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