1: #ifndef lint
   2: static char sccsid[] = "@(#)db_save.c	4.3 (Berkeley) 6/4/86";
   3: #endif
   4: 
   5: /*
   6:  * Copyright (c) 1986 Regents of the University of California
   7:  *	All Rights Reserved
   8:  */
   9: 
  10: /*
  11:  * Buffer allocation and deallocation routines.
  12:  */
  13: 
  14: #include <sys/types.h>
  15: #include <stdio.h>
  16: #include <syslog.h>
  17: #include "db.h"
  18: 
  19: #ifdef DEBUG
  20: extern int debug;
  21: extern FILE *ddt;
  22: #endif
  23: 
  24: extern char *malloc();
  25: extern char *strcpy();
  26: 
  27: /*
  28:  * Allocate a name buffer & save name.
  29:  */
  30: struct namebuf *
  31: savename(name)
  32:     char *name;
  33: {
  34:     register struct namebuf *np;
  35: 
  36:     np = (struct namebuf *) malloc(sizeof(struct namebuf));
  37:     if (np == NULL) {
  38:         syslog(LOG_ERR, "savename: %m");
  39:         exit(1);
  40:     }
  41:     np->n_dname = savestr(name);
  42:     np->n_next = NULL;
  43:     np->n_data = NULL;
  44:     np->n_hash = NULL;
  45:     return (np);
  46: }
  47: 
  48: /*
  49:  * Allocate a data buffer & save data.
  50:  */
  51: struct databuf *
  52: savedata(class, type, ttl, data, size)
  53:     int class, type;
  54:     u_long ttl;
  55:     char *data;
  56:     int size;
  57: {
  58:     register struct databuf *dp;
  59: 
  60:     dp = (struct databuf *) malloc((unsigned)DATASIZE(size));
  61:     if (dp == NULL) {
  62:         syslog(LOG_ERR, "savedata: %m");
  63:         exit(1);
  64:     }
  65:     dp->d_next = NULL;
  66:     dp->d_type = type;
  67:     dp->d_class = class;
  68:     dp->d_ttl = ttl;
  69:     dp->d_size = size;
  70:     bcopy(data, dp->d_data, dp->d_size);
  71:     return (dp);
  72: }
  73: 
  74: int hashsizes[] = { /* hashtable sizes */
  75:     11,
  76:     113,
  77:     977,
  78:     4073,
  79:     16001,
  80:     0
  81: };
  82: 
  83: /*
  84:  * Allocate a data buffer & save data.
  85:  */
  86: struct hashbuf *
  87: savehash(oldhtp)
  88:     struct hashbuf *oldhtp;
  89: {
  90:     register struct hashbuf *htp;
  91:     register int n;
  92:     int newsize;
  93: 
  94:     if (oldhtp == NULL)
  95:         newsize = hashsizes[0];
  96:     else {
  97:         for (n = 0; newsize = hashsizes[n++]; )
  98:             if (oldhtp->h_size == newsize) {
  99:                 newsize = hashsizes[n];
 100:                 break;
 101:             }
 102:         if (newsize == 0)
 103:             newsize = oldhtp->h_size * 2 + 1;
 104:     }
 105:     htp = (struct hashbuf *) malloc((unsigned)HASHSIZE(newsize));
 106:     if (htp == NULL) {
 107:         syslog(LOG_ERR, "savehash: %m");
 108:         exit(1);
 109:     }
 110:     htp->h_size = newsize;
 111:     bzero((char *) htp->h_tab, newsize * sizeof(struct hashbuf *));
 112:     if (oldhtp == NULL) {
 113:         htp->h_cnt = 0;
 114:         return (htp);
 115:     }
 116: #ifdef DEBUG
 117:     if (debug > 3)
 118:         fprintf(ddt,"savehash(%#x) cnt=%d, sz=%d, newsz=%d\n",
 119:             oldhtp, oldhtp->h_cnt, oldhtp->h_size, newsize);
 120: #endif
 121:     htp->h_cnt = oldhtp->h_cnt;
 122:     for (n = 0; n < oldhtp->h_size; n++) {
 123:         register struct namebuf *np;
 124:         struct namebuf *nnp;
 125:         register unsigned hval;
 126:         register char *cp;
 127: 
 128:         for (np = oldhtp->h_tab[n]; np != NULL; np = nnp) {
 129:             hval = 0;
 130:             for (cp = np->n_dname; *cp; ) {
 131:                 hval <<= HASHSHIFT;
 132:                 hval += *cp++ & HASHMASK;
 133:             }
 134:             hval %= htp->h_size;
 135:             nnp = np->n_next;
 136:             np->n_next = htp->h_tab[hval];
 137:             htp->h_tab[hval] = np;
 138:         }
 139:     }
 140:     free((char *) oldhtp);
 141:     return (htp);
 142: }
 143: 
 144: /*
 145:  * Allocate an inverse query buffer.
 146:  */
 147: struct invbuf *
 148: saveinv()
 149: {
 150:     register struct invbuf *ip;
 151: 
 152:     ip = (struct invbuf *) malloc(sizeof(struct invbuf));
 153:     if (ip == NULL) {
 154:         syslog(LOG_ERR, "saveinv: %m");
 155:         exit(1);
 156:     }
 157:     ip->i_next = NULL;
 158:     bzero((char *)ip->i_dname, sizeof(ip->i_dname));
 159:     return (ip);
 160: }
 161: 
 162: /*
 163:  * Make a copy of a string and return a pointer to it.
 164:  */
 165: char *
 166: savestr(str)
 167:     char *str;
 168: {
 169:     char *cp;
 170: 
 171:     cp = malloc((unsigned)strlen(str) + 1);
 172:     if (cp == NULL) {
 173:         syslog(LOG_ERR, "savestr: %m");
 174:         exit(1);
 175:     }
 176:     (void) strcpy(cp, str);
 177:     return (cp);
 178: }

Defined functions

saveinv defined in line 147; used 2 times
savename defined in line 30; used 2 times
savestr defined in line 165; used 6 times

Defined variables

hashsizes defined in line 74; used 3 times
sccsid defined in line 2; never used
Last modified: 1986-06-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 1142
Valid CSS Valid XHTML 1.0 Strict