17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 22*61961e0fSrobinson 237c478bd9Sstevel@tonic-gate /* 24*61961e0fSrobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 297c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley 337c478bd9Sstevel@tonic-gate * under license from the Regents of the University of 347c478bd9Sstevel@tonic-gate * California. 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include <rpcsvc/dbm.h> 407c478bd9Sstevel@tonic-gate #include <sys/types.h> 417c478bd9Sstevel@tonic-gate #include <sys/stat.h> 427c478bd9Sstevel@tonic-gate #include <string.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <stdlib.h> 457c478bd9Sstevel@tonic-gate #include <fcntl.h> 467c478bd9Sstevel@tonic-gate #include <stdio.h> 477c478bd9Sstevel@tonic-gate #include <errno.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #if defined(sparc) 507c478bd9Sstevel@tonic-gate #define _FSTAT _fstat 517c478bd9Sstevel@tonic-gate extern int _fstat(int, struct stat *); 527c478bd9Sstevel@tonic-gate #else /* !sparc */ 537c478bd9Sstevel@tonic-gate #define _FSTAT fstat 547c478bd9Sstevel@tonic-gate #endif /* sparc */ 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate void dbm_access(long); 587c478bd9Sstevel@tonic-gate void delitem(char *, int); 597c478bd9Sstevel@tonic-gate void chkblk(char *); 607c478bd9Sstevel@tonic-gate int additem(char *, datum); 617c478bd9Sstevel@tonic-gate int getbit(void); 627c478bd9Sstevel@tonic-gate int setbit(void); 637c478bd9Sstevel@tonic-gate int cmpdatum(datum, datum); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate int 66*61961e0fSrobinson dbminit(char *file) 677c478bd9Sstevel@tonic-gate { 687c478bd9Sstevel@tonic-gate struct stat statb; 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate dbrdonly = 0; 717c478bd9Sstevel@tonic-gate if (strlcpy(pagbuf, file, sizeof (pagbuf)) >= sizeof (pagbuf) || 727c478bd9Sstevel@tonic-gate strlcat(pagbuf, ".pag", sizeof (pagbuf)) >= sizeof (pagbuf)) { 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * file.pag does not fit into pagbuf. 757c478bd9Sstevel@tonic-gate * fails with ENAMETOOLONG. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate errno = ENAMETOOLONG; 787c478bd9Sstevel@tonic-gate return (-1); 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate pagf = open(pagbuf, 2); 817c478bd9Sstevel@tonic-gate if (pagf < 0) { 827c478bd9Sstevel@tonic-gate pagf = open(pagbuf, 0); 837c478bd9Sstevel@tonic-gate dbrdonly = 1; 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * We know this won't overflow so it is safe to ignore the 877c478bd9Sstevel@tonic-gate * return value; we use strl* to prevent false hits in 887c478bd9Sstevel@tonic-gate * code sweeps. 897c478bd9Sstevel@tonic-gate */ 907c478bd9Sstevel@tonic-gate (void) strlcpy(pagbuf, file, sizeof (pagbuf)); 917c478bd9Sstevel@tonic-gate (void) strlcat(pagbuf, ".dir", sizeof (pagbuf)); 927c478bd9Sstevel@tonic-gate dirf = open(pagbuf, 2); 937c478bd9Sstevel@tonic-gate if (dirf < 0) { 947c478bd9Sstevel@tonic-gate dirf = open(pagbuf, 0); 957c478bd9Sstevel@tonic-gate dbrdonly = 1; 967c478bd9Sstevel@tonic-gate } 97*61961e0fSrobinson if (pagf < 0 || dirf < 0) 987c478bd9Sstevel@tonic-gate return (-1); 99*61961e0fSrobinson (void) _FSTAT(dirf, &statb); 1007c478bd9Sstevel@tonic-gate maxbno = statb.st_size*BYTESIZ-1; 1017c478bd9Sstevel@tonic-gate return (0); 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate static long oldb1 = -1; 1057c478bd9Sstevel@tonic-gate static long oldb2 = -1; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* Avoid using cached data for subsequent accesses. */ 1087c478bd9Sstevel@tonic-gate int 109*61961e0fSrobinson dbmflush(void) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate oldb1 = -1; 1127c478bd9Sstevel@tonic-gate oldb2 = -1; 1137c478bd9Sstevel@tonic-gate return (0); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* Clean up after ourself. */ 1177c478bd9Sstevel@tonic-gate int 118*61961e0fSrobinson dbmclose(void) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate (void) close(pagf); 1217c478bd9Sstevel@tonic-gate (void) close(dirf); 1227c478bd9Sstevel@tonic-gate bitno = 0; 1237c478bd9Sstevel@tonic-gate maxbno = 0; 1247c478bd9Sstevel@tonic-gate blkno = 0; 1257c478bd9Sstevel@tonic-gate hmask = 0; 1267c478bd9Sstevel@tonic-gate oldb1 = -1; 1277c478bd9Sstevel@tonic-gate oldb2 = -1; 1287c478bd9Sstevel@tonic-gate return (0); 1297c478bd9Sstevel@tonic-gate } 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate long 132*61961e0fSrobinson forder(datum key) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate long hash; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate hash = calchash(key); 1377c478bd9Sstevel@tonic-gate for (hmask = 0; ; hmask = (hmask<<1) + 1) { 1387c478bd9Sstevel@tonic-gate blkno = hash & hmask; 1397c478bd9Sstevel@tonic-gate bitno = blkno + hmask; 1407c478bd9Sstevel@tonic-gate if (getbit() == 0) 1417c478bd9Sstevel@tonic-gate break; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate return (blkno); 1447c478bd9Sstevel@tonic-gate } 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate datum 147*61961e0fSrobinson fetch(datum key) 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate int i; 1507c478bd9Sstevel@tonic-gate datum item; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate dbm_access(calchash(key)); 1537c478bd9Sstevel@tonic-gate for (i = 0; ; i += 2) { 1547c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 1557c478bd9Sstevel@tonic-gate if (item.dptr == NULL) { 1567c478bd9Sstevel@tonic-gate return (item); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate if (cmpdatum(key, item) == 0) { 1597c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i+1); 1607c478bd9Sstevel@tonic-gate if (item.dptr == NULL) 1617c478bd9Sstevel@tonic-gate (void) printf("items not in pairs\n"); 1627c478bd9Sstevel@tonic-gate return (item); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate int 168*61961e0fSrobinson delete(datum key) 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate int i; 1717c478bd9Sstevel@tonic-gate datum item; 1727c478bd9Sstevel@tonic-gate 173*61961e0fSrobinson if (dbrdonly) 1747c478bd9Sstevel@tonic-gate return (-1); 1757c478bd9Sstevel@tonic-gate dbm_access(calchash(key)); 1767c478bd9Sstevel@tonic-gate for (i = 0; ; i += 2) { 1777c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 178*61961e0fSrobinson if (item.dptr == NULL) 1797c478bd9Sstevel@tonic-gate return (-1); 1807c478bd9Sstevel@tonic-gate if (cmpdatum(key, item) == 0) { 1817c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 1827c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 1837c478bd9Sstevel@tonic-gate break; 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate } 1867c478bd9Sstevel@tonic-gate (void) lseek(pagf, blkno*PBLKSIZ, 0); 1877c478bd9Sstevel@tonic-gate (void) write(pagf, pagbuf, PBLKSIZ); 1887c478bd9Sstevel@tonic-gate return (0); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate int 192*61961e0fSrobinson store(datum key, datum dat) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate int i; 1957c478bd9Sstevel@tonic-gate datum item; 1967c478bd9Sstevel@tonic-gate char ovfbuf[PBLKSIZ]; 1977c478bd9Sstevel@tonic-gate 198*61961e0fSrobinson if (dbrdonly) 1997c478bd9Sstevel@tonic-gate return (-1); 2007c478bd9Sstevel@tonic-gate loop: 2017c478bd9Sstevel@tonic-gate dbm_access(calchash(key)); 2027c478bd9Sstevel@tonic-gate for (i = 0; ; i += 2) { 2037c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 2047c478bd9Sstevel@tonic-gate if (item.dptr == NULL) 2057c478bd9Sstevel@tonic-gate break; 2067c478bd9Sstevel@tonic-gate if (cmpdatum(key, item) == 0) { 2077c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 2087c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 2097c478bd9Sstevel@tonic-gate break; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate i = additem(pagbuf, key); 2137c478bd9Sstevel@tonic-gate if (i < 0) 2147c478bd9Sstevel@tonic-gate goto split; 2157c478bd9Sstevel@tonic-gate if (additem(pagbuf, dat) < 0) { 2167c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 2177c478bd9Sstevel@tonic-gate goto split; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate (void) lseek(pagf, blkno*PBLKSIZ, 0); 2207c478bd9Sstevel@tonic-gate (void) write(pagf, pagbuf, PBLKSIZ); 2217c478bd9Sstevel@tonic-gate return (0); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate split: 2247c478bd9Sstevel@tonic-gate if (key.dsize + dat.dsize + 3 * sizeof (short) >= PBLKSIZ) { 2257c478bd9Sstevel@tonic-gate (void) printf("entry too big\n"); 2267c478bd9Sstevel@tonic-gate return (-1); 2277c478bd9Sstevel@tonic-gate } 228*61961e0fSrobinson (void) memset(&ovfbuf, 0, PBLKSIZ); 2297c478bd9Sstevel@tonic-gate for (i = 0; ; ) { 2307c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 2317c478bd9Sstevel@tonic-gate if (item.dptr == NULL) 2327c478bd9Sstevel@tonic-gate break; 2337c478bd9Sstevel@tonic-gate if (calchash(item) & (hmask+1)) { 2347c478bd9Sstevel@tonic-gate (void) additem(ovfbuf, item); 2357c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 2367c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 2377c478bd9Sstevel@tonic-gate if (item.dptr == NULL) { 2387c478bd9Sstevel@tonic-gate (void) printf("split not paired\n"); 2397c478bd9Sstevel@tonic-gate break; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate (void) additem(ovfbuf, item); 2427c478bd9Sstevel@tonic-gate delitem(pagbuf, i); 2437c478bd9Sstevel@tonic-gate continue; 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate i += 2; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate (void) lseek(pagf, blkno*PBLKSIZ, 0); 2487c478bd9Sstevel@tonic-gate if (write(pagf, pagbuf, PBLKSIZ) < 0) { 2497c478bd9Sstevel@tonic-gate return (-1); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate (void) lseek(pagf, (blkno+hmask+1)*PBLKSIZ, 0); 2527c478bd9Sstevel@tonic-gate if (write(pagf, ovfbuf, PBLKSIZ) < 0) { 2537c478bd9Sstevel@tonic-gate return (-1); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate if (setbit() < 0) { 2567c478bd9Sstevel@tonic-gate return (-1); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate goto loop; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate datum 262*61961e0fSrobinson firstkey(void) 2637c478bd9Sstevel@tonic-gate { 264*61961e0fSrobinson return (firsthash(0L)); 2657c478bd9Sstevel@tonic-gate } 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate datum 268*61961e0fSrobinson nextkey(datum key) 2697c478bd9Sstevel@tonic-gate { 2707c478bd9Sstevel@tonic-gate int i; 2717c478bd9Sstevel@tonic-gate datum item, bitem; 2727c478bd9Sstevel@tonic-gate long hash; 2737c478bd9Sstevel@tonic-gate int f; 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate #ifdef lint 2767c478bd9Sstevel@tonic-gate bitem.dptr = NULL; 2777c478bd9Sstevel@tonic-gate bitem.dsize = 0; 2787c478bd9Sstevel@tonic-gate #endif /* lint */ 2797c478bd9Sstevel@tonic-gate hash = calchash(key); 2807c478bd9Sstevel@tonic-gate dbm_access(hash); 2817c478bd9Sstevel@tonic-gate f = 1; 2827c478bd9Sstevel@tonic-gate for (i = 0; ; i += 2) { 2837c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 2847c478bd9Sstevel@tonic-gate if (item.dptr == NULL) 2857c478bd9Sstevel@tonic-gate break; 2867c478bd9Sstevel@tonic-gate if (cmpdatum(key, item) <= 0) 2877c478bd9Sstevel@tonic-gate continue; 2887c478bd9Sstevel@tonic-gate if (f || cmpdatum(bitem, item) < 0) { 2897c478bd9Sstevel@tonic-gate bitem = item; 2907c478bd9Sstevel@tonic-gate f = 0; 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate } 293*61961e0fSrobinson if (f == 0) 2947c478bd9Sstevel@tonic-gate return (bitem); 2957c478bd9Sstevel@tonic-gate hash = hashinc(hash); 296*61961e0fSrobinson if (hash == 0) 2977c478bd9Sstevel@tonic-gate return (item); 298*61961e0fSrobinson return (firsthash(hash)); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate datum 302*61961e0fSrobinson firsthash(long hash) 3037c478bd9Sstevel@tonic-gate { 3047c478bd9Sstevel@tonic-gate int i; 3057c478bd9Sstevel@tonic-gate datum item, bitem; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate loop: 3087c478bd9Sstevel@tonic-gate dbm_access(hash); 3097c478bd9Sstevel@tonic-gate bitem = makdatum(pagbuf, 0); 3107c478bd9Sstevel@tonic-gate for (i = 2; ; i += 2) { 3117c478bd9Sstevel@tonic-gate item = makdatum(pagbuf, i); 3127c478bd9Sstevel@tonic-gate if (item.dptr == NULL) 3137c478bd9Sstevel@tonic-gate break; 3147c478bd9Sstevel@tonic-gate if (cmpdatum(bitem, item) < 0) 3157c478bd9Sstevel@tonic-gate bitem = item; 3167c478bd9Sstevel@tonic-gate } 317*61961e0fSrobinson if (bitem.dptr != NULL) 3187c478bd9Sstevel@tonic-gate return (bitem); 3197c478bd9Sstevel@tonic-gate hash = hashinc(hash); 320*61961e0fSrobinson if (hash == 0) 3217c478bd9Sstevel@tonic-gate return (item); 3227c478bd9Sstevel@tonic-gate goto loop; 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate void 326*61961e0fSrobinson dbm_access(long hash) 3277c478bd9Sstevel@tonic-gate { 3287c478bd9Sstevel@tonic-gate ssize_t readsize; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate for (hmask = 0; ; hmask = (hmask<<1) + 1) { 3317c478bd9Sstevel@tonic-gate blkno = hash & hmask; 3327c478bd9Sstevel@tonic-gate bitno = blkno + hmask; 3337c478bd9Sstevel@tonic-gate if (getbit() == 0) 3347c478bd9Sstevel@tonic-gate break; 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate if (blkno != oldb1) { 3377c478bd9Sstevel@tonic-gate (void) lseek(pagf, blkno*PBLKSIZ, 0); 3387c478bd9Sstevel@tonic-gate readsize = read(pagf, pagbuf, PBLKSIZ); 3397c478bd9Sstevel@tonic-gate if (readsize != PBLKSIZ) { 340*61961e0fSrobinson if (readsize < 0) 341*61961e0fSrobinson readsize = 0; 342*61961e0fSrobinson (void) memset((&pagbuf+readsize), 0, PBLKSIZ-readsize); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate chkblk(pagbuf); 3457c478bd9Sstevel@tonic-gate oldb1 = blkno; 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate int 3507c478bd9Sstevel@tonic-gate getbit(void) 3517c478bd9Sstevel@tonic-gate { 3527c478bd9Sstevel@tonic-gate long bn; 3537c478bd9Sstevel@tonic-gate ssize_t readsize; 3547c478bd9Sstevel@tonic-gate long b, i, n; 3557c478bd9Sstevel@tonic-gate 356*61961e0fSrobinson if (bitno > maxbno) 3577c478bd9Sstevel@tonic-gate return (0); 3587c478bd9Sstevel@tonic-gate n = bitno % BYTESIZ; 3597c478bd9Sstevel@tonic-gate bn = bitno / BYTESIZ; 3607c478bd9Sstevel@tonic-gate i = bn % DBLKSIZ; 3617c478bd9Sstevel@tonic-gate b = bn / DBLKSIZ; 3627c478bd9Sstevel@tonic-gate if (b != oldb2) { 3637c478bd9Sstevel@tonic-gate (void) lseek(dirf, (long)b*DBLKSIZ, 0); 3647c478bd9Sstevel@tonic-gate readsize = read(dirf, dirbuf, DBLKSIZ); 3657c478bd9Sstevel@tonic-gate if (readsize != DBLKSIZ) { 366*61961e0fSrobinson if (readsize < 0) 367*61961e0fSrobinson readsize = 0; 368*61961e0fSrobinson (void) memset(&dirbuf+readsize, 0, DBLKSIZ-readsize); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate oldb2 = b; 3717c478bd9Sstevel@tonic-gate } 372*61961e0fSrobinson if (dirbuf[i] & (1<<n)) 3737c478bd9Sstevel@tonic-gate return (1); 3747c478bd9Sstevel@tonic-gate return (0); 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate int 3787c478bd9Sstevel@tonic-gate setbit(void) 3797c478bd9Sstevel@tonic-gate { 3807c478bd9Sstevel@tonic-gate long bn; 3817c478bd9Sstevel@tonic-gate long i, n, b; 3827c478bd9Sstevel@tonic-gate 383*61961e0fSrobinson if (dbrdonly) 3847c478bd9Sstevel@tonic-gate return (-1); 3857c478bd9Sstevel@tonic-gate if (bitno > maxbno) { 3867c478bd9Sstevel@tonic-gate maxbno = bitno; 3877c478bd9Sstevel@tonic-gate (void) getbit(); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate n = bitno % BYTESIZ; 3907c478bd9Sstevel@tonic-gate bn = bitno / BYTESIZ; 3917c478bd9Sstevel@tonic-gate i = bn % DBLKSIZ; 3927c478bd9Sstevel@tonic-gate b = bn / DBLKSIZ; 3937c478bd9Sstevel@tonic-gate dirbuf[i] |= 1<<n; 3947c478bd9Sstevel@tonic-gate (void) lseek(dirf, (long)b*DBLKSIZ, 0); 395*61961e0fSrobinson if (write(dirf, dirbuf, DBLKSIZ) < 0) 3967c478bd9Sstevel@tonic-gate return (-1); 3977c478bd9Sstevel@tonic-gate return (0); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate datum 4017c478bd9Sstevel@tonic-gate makdatum(char buf[PBLKSIZ], int n) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate short *sp; 4047c478bd9Sstevel@tonic-gate int t; 4057c478bd9Sstevel@tonic-gate datum item; 4067c478bd9Sstevel@tonic-gate 407*61961e0fSrobinson /* LINTED pointer cast */ 4087c478bd9Sstevel@tonic-gate sp = (short *)buf; 4097c478bd9Sstevel@tonic-gate if (n < 0 || n >= sp[0]) 4107c478bd9Sstevel@tonic-gate goto null; 4117c478bd9Sstevel@tonic-gate t = PBLKSIZ; 4127c478bd9Sstevel@tonic-gate if (n > 0) 4137c478bd9Sstevel@tonic-gate t = sp[n+1-1]; 4147c478bd9Sstevel@tonic-gate item.dptr = buf+sp[n+1]; 4157c478bd9Sstevel@tonic-gate item.dsize = t - sp[n+1]; 4167c478bd9Sstevel@tonic-gate return (item); 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate null: 4197c478bd9Sstevel@tonic-gate item.dptr = NULL; 4207c478bd9Sstevel@tonic-gate item.dsize = 0; 4217c478bd9Sstevel@tonic-gate return (item); 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate int 425*61961e0fSrobinson cmpdatum(datum d1, datum d2) 4267c478bd9Sstevel@tonic-gate { 4277c478bd9Sstevel@tonic-gate int n; 4287c478bd9Sstevel@tonic-gate char *p1, *p2; 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate n = d1.dsize; 431*61961e0fSrobinson if (n != d2.dsize) 4327c478bd9Sstevel@tonic-gate return (n - d2.dsize); 433*61961e0fSrobinson if (n == 0) 4347c478bd9Sstevel@tonic-gate return (0); 4357c478bd9Sstevel@tonic-gate p1 = d1.dptr; 4367c478bd9Sstevel@tonic-gate p2 = d2.dptr; 4377c478bd9Sstevel@tonic-gate do 438*61961e0fSrobinson if (*p1++ != *p2++) 4397c478bd9Sstevel@tonic-gate return (*--p1 - *--p2); 4407c478bd9Sstevel@tonic-gate while (--n); 4417c478bd9Sstevel@tonic-gate return (0); 4427c478bd9Sstevel@tonic-gate } 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate int hitab[16] 4457c478bd9Sstevel@tonic-gate /* 4467c478bd9Sstevel@tonic-gate * ken's 4477c478bd9Sstevel@tonic-gate * { 4487c478bd9Sstevel@tonic-gate * 055, 043, 036, 054, 063, 014, 004, 005, 4497c478bd9Sstevel@tonic-gate * 010, 064, 077, 000, 035, 027, 025, 071, 4507c478bd9Sstevel@tonic-gate * }; 4517c478bd9Sstevel@tonic-gate */ 4527c478bd9Sstevel@tonic-gate = { 61, 57, 53, 49, 45, 41, 37, 33, 4537c478bd9Sstevel@tonic-gate 29, 25, 21, 17, 13, 9, 5, 1, 4547c478bd9Sstevel@tonic-gate }; 4557c478bd9Sstevel@tonic-gate long hltab[64] 4567c478bd9Sstevel@tonic-gate = { 4577c478bd9Sstevel@tonic-gate 06100151277L, 06106161736L, 06452611562L, 05001724107L, 4587c478bd9Sstevel@tonic-gate 02614772546L, 04120731531L, 04665262210L, 07347467531L, 4597c478bd9Sstevel@tonic-gate 06735253126L, 06042345173L, 03072226605L, 01464164730L, 4607c478bd9Sstevel@tonic-gate 03247435524L, 07652510057L, 01546775256L, 05714532133L, 4617c478bd9Sstevel@tonic-gate 06173260402L, 07517101630L, 02431460343L, 01743245566L, 4627c478bd9Sstevel@tonic-gate 00261675137L, 02433103631L, 03421772437L, 04447707466L, 4637c478bd9Sstevel@tonic-gate 04435620103L, 03757017115L, 03641531772L, 06767633246L, 4647c478bd9Sstevel@tonic-gate 02673230344L, 00260612216L, 04133454451L, 00615531516L, 4657c478bd9Sstevel@tonic-gate 06137717526L, 02574116560L, 02304023373L, 07061702261L, 4667c478bd9Sstevel@tonic-gate 05153031405L, 05322056705L, 07401116734L, 06552375715L, 4677c478bd9Sstevel@tonic-gate 06165233473L, 05311063631L, 01212221723L, 01052267235L, 4687c478bd9Sstevel@tonic-gate 06000615237L, 01075222665L, 06330216006L, 04402355630L, 4697c478bd9Sstevel@tonic-gate 01451177262L, 02000133436L, 06025467062L, 07121076461L, 4707c478bd9Sstevel@tonic-gate 03123433522L, 01010635225L, 01716177066L, 05161746527L, 4717c478bd9Sstevel@tonic-gate 01736635071L, 06243505026L, 03637211610L, 01756474365L, 4727c478bd9Sstevel@tonic-gate 04723077174L, 03642763134L, 05750130273L, 03655541561L, 4737c478bd9Sstevel@tonic-gate }; 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate long 476*61961e0fSrobinson hashinc(long hash) 4777c478bd9Sstevel@tonic-gate { 4787c478bd9Sstevel@tonic-gate long bit; 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate hash &= hmask; 4817c478bd9Sstevel@tonic-gate bit = hmask+1; 4827c478bd9Sstevel@tonic-gate for (; ; ) { 4837c478bd9Sstevel@tonic-gate bit >>= 1; 484*61961e0fSrobinson if (bit == 0) 4857c478bd9Sstevel@tonic-gate return (0L); 486*61961e0fSrobinson if ((hash&bit) == 0) 4877c478bd9Sstevel@tonic-gate return (hash|bit); 4887c478bd9Sstevel@tonic-gate hash &= ~bit; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 4927c478bd9Sstevel@tonic-gate long 493*61961e0fSrobinson calchash(datum item) 4947c478bd9Sstevel@tonic-gate { 4957c478bd9Sstevel@tonic-gate int i, j, f; 4967c478bd9Sstevel@tonic-gate long hashl; 4977c478bd9Sstevel@tonic-gate int hashi; 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate hashl = 0; 5007c478bd9Sstevel@tonic-gate hashi = 0; 5017c478bd9Sstevel@tonic-gate for (i = 0; i < item.dsize; i++) { 5027c478bd9Sstevel@tonic-gate f = item.dptr[i]; 5037c478bd9Sstevel@tonic-gate for (j = 0; j < BYTESIZ; j += 4) { 5047c478bd9Sstevel@tonic-gate hashi += hitab[f&017]; 5057c478bd9Sstevel@tonic-gate hashl += hltab[hashi&63]; 5067c478bd9Sstevel@tonic-gate f >>= 4; 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate } 5097c478bd9Sstevel@tonic-gate return (hashl); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate void 513*61961e0fSrobinson delitem(char buf[PBLKSIZ], int n) 5147c478bd9Sstevel@tonic-gate { 5157c478bd9Sstevel@tonic-gate short *sp; 5167c478bd9Sstevel@tonic-gate int i1, i2, i3; 5177c478bd9Sstevel@tonic-gate 518*61961e0fSrobinson /* LINTED pointer cast */ 5197c478bd9Sstevel@tonic-gate sp = (short *)buf; 5207c478bd9Sstevel@tonic-gate if (n < 0 || n >= sp[0]) 5217c478bd9Sstevel@tonic-gate goto bad; 5227c478bd9Sstevel@tonic-gate i1 = sp[n+1]; 5237c478bd9Sstevel@tonic-gate i2 = PBLKSIZ; 5247c478bd9Sstevel@tonic-gate if (n > 0) 5257c478bd9Sstevel@tonic-gate i2 = sp[n+1-1]; 5267c478bd9Sstevel@tonic-gate i3 = sp[sp[0]+1-1]; 5277c478bd9Sstevel@tonic-gate if (i2 > i1) 5287c478bd9Sstevel@tonic-gate while (i1 > i3) { 5297c478bd9Sstevel@tonic-gate i1--; 5307c478bd9Sstevel@tonic-gate i2--; 5317c478bd9Sstevel@tonic-gate buf[i2] = buf[i1]; 5327c478bd9Sstevel@tonic-gate buf[i1] = 0; 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate i2 -= i1; 5357c478bd9Sstevel@tonic-gate for (i1 = n + 1; i1 < sp[0]; i1++) 5367c478bd9Sstevel@tonic-gate sp[i1+1-1] = sp[i1+1] + i2; 5377c478bd9Sstevel@tonic-gate sp[0]--; 5387c478bd9Sstevel@tonic-gate sp[sp[0]+1] = 0; 5397c478bd9Sstevel@tonic-gate return; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate bad: 5427c478bd9Sstevel@tonic-gate (void) printf("bad delitem\n"); 5437c478bd9Sstevel@tonic-gate abort(); 5447c478bd9Sstevel@tonic-gate } 5457c478bd9Sstevel@tonic-gate 5467c478bd9Sstevel@tonic-gate int 547*61961e0fSrobinson additem(char buf[PBLKSIZ], datum item) 5487c478bd9Sstevel@tonic-gate { 5497c478bd9Sstevel@tonic-gate short *sp; 5507c478bd9Sstevel@tonic-gate int i1, i2; 5517c478bd9Sstevel@tonic-gate 552*61961e0fSrobinson /* LINTED pointer cast */ 5537c478bd9Sstevel@tonic-gate sp = (short *)buf; 5547c478bd9Sstevel@tonic-gate i1 = PBLKSIZ; 5557c478bd9Sstevel@tonic-gate if (sp[0] > 0) 5567c478bd9Sstevel@tonic-gate i1 = sp[sp[0]+1-1]; 5577c478bd9Sstevel@tonic-gate i1 -= item.dsize; 5587c478bd9Sstevel@tonic-gate i2 = (sp[0]+2) * (int)sizeof (short); 559*61961e0fSrobinson if (i1 <= i2) 5607c478bd9Sstevel@tonic-gate return (-1); 5617c478bd9Sstevel@tonic-gate sp[sp[0]+1] = (short)i1; 5627c478bd9Sstevel@tonic-gate for (i2 = 0; i2 < item.dsize; i2++) { 5637c478bd9Sstevel@tonic-gate buf[i1] = item.dptr[i2]; 5647c478bd9Sstevel@tonic-gate i1++; 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate sp[0]++; 5677c478bd9Sstevel@tonic-gate return (sp[0]-1); 5687c478bd9Sstevel@tonic-gate } 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate void 571*61961e0fSrobinson chkblk(char buf[PBLKSIZ]) 5727c478bd9Sstevel@tonic-gate { 5737c478bd9Sstevel@tonic-gate short *sp; 5747c478bd9Sstevel@tonic-gate int t, i; 5757c478bd9Sstevel@tonic-gate 576*61961e0fSrobinson /* LINTED pointer cast */ 5777c478bd9Sstevel@tonic-gate sp = (short *)buf; 5787c478bd9Sstevel@tonic-gate t = PBLKSIZ; 5797c478bd9Sstevel@tonic-gate for (i = 0; i < sp[0]; i++) { 5807c478bd9Sstevel@tonic-gate if (sp[i+1] > t) 5817c478bd9Sstevel@tonic-gate goto bad; 5827c478bd9Sstevel@tonic-gate t = sp[i+1]; 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate if (t < (sp[0]+1) * sizeof (short)) 5857c478bd9Sstevel@tonic-gate goto bad; 5867c478bd9Sstevel@tonic-gate return; 5877c478bd9Sstevel@tonic-gate 5887c478bd9Sstevel@tonic-gate bad: 5897c478bd9Sstevel@tonic-gate (void) printf("bad block\n"); 5907c478bd9Sstevel@tonic-gate abort(); 591*61961e0fSrobinson (void) memset(&buf, 0, PBLKSIZ); 5927c478bd9Sstevel@tonic-gate } 593