home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / trn_12.zip / src / nghash.c < prev    next >
C/C++ Source or Header  |  1993-12-04  |  4KB  |  145 lines

  1. /* $Id: nghash.c,v 3.0 1992/02/01 03:09:32 davison Trn $
  2.  */
  3. /*
  4.  * This software is Copyright 1991 by Stan Barber.
  5.  *
  6.  * Permission is hereby granted to copy, reproduce, redistribute or otherwise
  7.  * use this software as long as: there is no monetary profit gained
  8.  * specifically from the use or reproduction of this software, it is not
  9.  * sold, rented, traded or otherwise marketed, and this copyright notice is
  10.  * included prominently in any copy made.
  11.  *
  12.  * The author make no claims as to the fitness or correctness of this software
  13.  * for any use whatsoever, and it is provided as is. Any use of this software
  14.  * is at the user's own risk.
  15.  */
  16.  
  17. #include "EXTERN.h"
  18. #include "common.h"
  19. #include "ndir.h"
  20. #include "rcstuff.h"
  21. #include "rn.h"
  22. #include "intrp.h"
  23. #include "final.h"
  24. #include "rcln.h"
  25. #include "util.h"
  26. #ifdef SERVER
  27. #include "server.h"
  28. #endif
  29. #include "ngdata.h"
  30. #include "hdbm.h"
  31.  
  32. static HASHTABLE *acthash;
  33. static char *actfile;
  34.  
  35. void
  36. ngdatansrv_init()        /* non-SERVER initialisation */
  37. {
  38.     register long offset = 0;
  39.     char *cp = ACTIVE;
  40.     struct stat actstat;
  41.  
  42.     actfp = fos2open(cp, "r");
  43.     if (actfp == Nullfp) {
  44.         cp = ACTIVE_OPT;
  45.         actfp = fos2open(cp,"r");
  46.         if (actfp == Nullfp) {
  47.         printf(cantopen,cp) ; FLUSH;
  48.         finalize(1);
  49.     }
  50.     }
  51.  
  52.  
  53.     /* rn was being a slug about rereading the active file over and
  54.      * over; try using a really big buffer to keep it in core. */
  55.     (void) fstat(fileno(actfp), &actstat);
  56.     actfile = safemalloc(actstat.st_size + 1);
  57.     rewind(actfp);
  58.     /*
  59.      * NOTE: this won't work on machines with ints too small to hold
  60.      * the size of the active file.
  61.      */
  62.     if (fread(actfile, 1, (int)actstat.st_size, actfp) != actstat.st_size) {
  63.     fprintf(stderr, "active file not read\n");
  64.     actfile[0] = '\0';
  65.     }
  66.     rewind(actfp);
  67.     actfile[actstat.st_size] = '\0';
  68.  
  69.     acthash = hdbmcreate((int)(actstat.st_size/40), (unsigned (*)())NULL);
  70.     if (acthash == NULL) {
  71.     fprintf(stderr, "can't hash %s\n", filexp(ACTIVE));
  72.     finalize(1);
  73.     }
  74.  
  75.     /* count entries */
  76.     rewind(actfp);
  77.     for (activeitems = 0; fgets(buf, LBUFLEN, actfp) != NULL; activeitems++) {
  78.     register char *p = index(buf, ' ');
  79.     HDBMDATUM key, data;
  80.  
  81.     /* key.dat_ptr must be allocated to make it permanent for hashstore */
  82.     if (p == NULL)
  83.         p = buf;
  84.     data.dat_ptr = key.dat_ptr = actfile + offset;
  85.     key.dat_len = p - buf;
  86.     data.dat_len = strlen(buf);
  87.     if (!hdbmstore(acthash, key, data)) {
  88.         fprintf(stderr, "can't install hash key %s\n", key.dat_ptr);
  89.         finalize(1);
  90.     }
  91.     offset += data.dat_len;
  92.     }
  93. }
  94.  
  95. ACT_POS
  96. findact(outbuf, nam, len, suggestion)
  97. register char *outbuf;
  98. register char *nam;
  99. register int len;
  100. register long suggestion;
  101. {
  102.     register ACT_POS retval;
  103.     extern int debug;
  104.  
  105.     /* see if we know the right place and can just return */
  106.     if (suggestion != 0 && fseek(actfp, suggestion, 0) >= 0
  107.      && fgets(outbuf, 80, actfp) != NULL && outbuf[len] == ' '
  108.      && strnEQ(outbuf, nam, len))
  109.     return suggestion;
  110.  
  111.     /* hmm, apparently not, gotta look it up. */
  112.     if (debug & DEB_SOFT_POINTERS)
  113.     printf("Missed, looking for %s in %sLen = %d\n",nam,outbuf,len); FLUSH;
  114.     /* can we just do a quick hashed lookup? */
  115.     if (acthash != NULL) {
  116.     HDBMDATUM key, data;
  117.  
  118.     outbuf[0] = '\0';
  119.     key.dat_ptr = nam;
  120.     key.dat_len = len;
  121.     data = hdbmfetch(acthash, key);
  122.     if (data.dat_ptr == NULL)
  123.         return -1;
  124.     else {
  125.         (void) bcopy(data.dat_ptr, outbuf, (int)data.dat_len);
  126.         outbuf[data.dat_len] = '\0';
  127.         return data.dat_ptr - actfile;
  128.     }
  129.     }
  130.  
  131.     /* apparently not.  gotta do it the slow way. */
  132.     (void) fseek(actfp, 0L, 0);
  133.     for (retval = 0; fgets(outbuf,80,actfp) != NULL; retval += strlen(outbuf))
  134.     if (outbuf[len] == ' ' && strnEQ(outbuf, nam, len))
  135.         break;
  136.     if (ferror(actfp)) {
  137.     perror("error on active file");
  138.     sig_catcher(0);
  139.     /* NOTREACHED */
  140.     } else if (feof(actfp))
  141.     return -1;    /* no such group */
  142.     return retval;
  143. }
  144.  
  145.