1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
24*7c478bd9Sstevel@tonic-gate
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate #include <stdio.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
36*7c478bd9Sstevel@tonic-gate #if SHARE
37*7c478bd9Sstevel@tonic-gate #include <sys/ipc.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/shm.h>
39*7c478bd9Sstevel@tonic-gate #define ERR -1
40*7c478bd9Sstevel@tonic-gate #endif
41*7c478bd9Sstevel@tonic-gate #include "invlib.h"
42*7c478bd9Sstevel@tonic-gate #include "library.h"
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #define DEBUG 0 /* debugging code and realloc messages */
45*7c478bd9Sstevel@tonic-gate #define BLOCKSIZE 2 * BUFSIZ /* logical block size */
46*7c478bd9Sstevel@tonic-gate #define LINEMAX 1000 /* sorted posting line max size */
47*7c478bd9Sstevel@tonic-gate #define POSTINC 10000 /* posting buffer size increment */
48*7c478bd9Sstevel@tonic-gate #define SEP ' ' /* sorted posting field separator */
49*7c478bd9Sstevel@tonic-gate #define SETINC 100 /* posting set size increment */
50*7c478bd9Sstevel@tonic-gate #define STATS 0 /* print statistics */
51*7c478bd9Sstevel@tonic-gate #define SUPERINC 10000 /* super index size increment */
52*7c478bd9Sstevel@tonic-gate #define TERMMAX 512 /* term max size */
53*7c478bd9Sstevel@tonic-gate #define VERSION 1 /* inverted index format version */
54*7c478bd9Sstevel@tonic-gate #define ZIPFSIZE 200 /* zipf curve size */
55*7c478bd9Sstevel@tonic-gate #define FREAD "r" /* fopen for reading */
56*7c478bd9Sstevel@tonic-gate #define FREADP "r+" /* fopen for update */
57*7c478bd9Sstevel@tonic-gate #define FWRITE "w" /* fopen truncate or create for writing */
58*7c478bd9Sstevel@tonic-gate #define FWRITEP "w+" /* fopen truncate or create for update */
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate extern char *argv0; /* command name (must be set in main function) */
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate int invbreak;
63*7c478bd9Sstevel@tonic-gate
64*7c478bd9Sstevel@tonic-gate #if STATS
65*7c478bd9Sstevel@tonic-gate int showzipf; /* show postings per term distribution */
66*7c478bd9Sstevel@tonic-gate #endif
67*7c478bd9Sstevel@tonic-gate
68*7c478bd9Sstevel@tonic-gate static POSTING *item, *enditem, *item1 = NULL, *item2 = NULL;
69*7c478bd9Sstevel@tonic-gate static unsigned setsize1, setsize2;
70*7c478bd9Sstevel@tonic-gate static long numitems, totterm, zerolong;
71*7c478bd9Sstevel@tonic-gate static char *indexfile, *postingfile;
72*7c478bd9Sstevel@tonic-gate static FILE *outfile, *fpost;
73*7c478bd9Sstevel@tonic-gate static unsigned supersize = SUPERINC, supintsize;
74*7c478bd9Sstevel@tonic-gate static int numpost, numlogblk, amtused, nextpost,
75*7c478bd9Sstevel@tonic-gate lastinblk, numinvitems;
76*7c478bd9Sstevel@tonic-gate static POSTING *POST, *postptr;
77*7c478bd9Sstevel@tonic-gate static unsigned long *SUPINT, *supint, nextsupfing;
78*7c478bd9Sstevel@tonic-gate static char *SUPFING, *supfing;
79*7c478bd9Sstevel@tonic-gate static char thisterm[TERMMAX];
80*7c478bd9Sstevel@tonic-gate static union {
81*7c478bd9Sstevel@tonic-gate long invblk[BLOCKSIZE / sizeof (long)];
82*7c478bd9Sstevel@tonic-gate char chrblk[BLOCKSIZE];
83*7c478bd9Sstevel@tonic-gate } logicalblk;
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate #if DEBUG || STATS
86*7c478bd9Sstevel@tonic-gate static long totpost;
87*7c478bd9Sstevel@tonic-gate #endif
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate #if STATS
90*7c478bd9Sstevel@tonic-gate static int zipf[ZIPFSIZE + 1];
91*7c478bd9Sstevel@tonic-gate #endif
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate static void invcannotalloc(size_t n);
94*7c478bd9Sstevel@tonic-gate static void invcannotopen(char *file);
95*7c478bd9Sstevel@tonic-gate static void invcannotwrite(char *file);
96*7c478bd9Sstevel@tonic-gate static int invnewterm(void);
97*7c478bd9Sstevel@tonic-gate static int boolready(void);
98*7c478bd9Sstevel@tonic-gate
99*7c478bd9Sstevel@tonic-gate long
invmake(char * invname,char * invpost,FILE * infile)100*7c478bd9Sstevel@tonic-gate invmake(char *invname, char *invpost, FILE *infile)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate unsigned char *s;
103*7c478bd9Sstevel@tonic-gate long num;
104*7c478bd9Sstevel@tonic-gate int i;
105*7c478bd9Sstevel@tonic-gate long fileindex;
106*7c478bd9Sstevel@tonic-gate unsigned postsize = POSTINC * sizeof (POSTING);
107*7c478bd9Sstevel@tonic-gate unsigned long *intptr;
108*7c478bd9Sstevel@tonic-gate char line[LINEMAX];
109*7c478bd9Sstevel@tonic-gate long tlong;
110*7c478bd9Sstevel@tonic-gate PARAM param;
111*7c478bd9Sstevel@tonic-gate POSTING posting;
112*7c478bd9Sstevel@tonic-gate #if STATS
113*7c478bd9Sstevel@tonic-gate int j;
114*7c478bd9Sstevel@tonic-gate unsigned maxtermlen = 0;
115*7c478bd9Sstevel@tonic-gate #endif
116*7c478bd9Sstevel@tonic-gate /* output file */
117*7c478bd9Sstevel@tonic-gate if ((outfile = vpfopen(invname, FWRITEP)) == NULL) {
118*7c478bd9Sstevel@tonic-gate invcannotopen(invname);
119*7c478bd9Sstevel@tonic-gate return (0);
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate indexfile = invname;
122*7c478bd9Sstevel@tonic-gate (void) fseek(outfile, (long)BUFSIZ, 0);
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate /* posting file */
125*7c478bd9Sstevel@tonic-gate if ((fpost = vpfopen(invpost, FWRITE)) == NULL) {
126*7c478bd9Sstevel@tonic-gate invcannotopen(invpost);
127*7c478bd9Sstevel@tonic-gate return (0);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate postingfile = invpost;
130*7c478bd9Sstevel@tonic-gate nextpost = 0;
131*7c478bd9Sstevel@tonic-gate /* get space for the postings list */
132*7c478bd9Sstevel@tonic-gate if ((POST = (POSTING *)malloc(postsize)) == NULL) {
133*7c478bd9Sstevel@tonic-gate invcannotalloc(postsize);
134*7c478bd9Sstevel@tonic-gate return (0);
135*7c478bd9Sstevel@tonic-gate }
136*7c478bd9Sstevel@tonic-gate postptr = POST;
137*7c478bd9Sstevel@tonic-gate /* get space for the superfinger (superindex) */
138*7c478bd9Sstevel@tonic-gate if ((SUPFING = malloc(supersize)) == NULL) {
139*7c478bd9Sstevel@tonic-gate invcannotalloc(supersize);
140*7c478bd9Sstevel@tonic-gate return (0);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate supfing = SUPFING;
143*7c478bd9Sstevel@tonic-gate supintsize = supersize / 40;
144*7c478bd9Sstevel@tonic-gate /* also for the superfinger index */
145*7c478bd9Sstevel@tonic-gate if ((SUPINT = malloc(supintsize * sizeof (long))) == NULL) {
146*7c478bd9Sstevel@tonic-gate invcannotalloc(supintsize * sizeof (long));
147*7c478bd9Sstevel@tonic-gate return (0);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate supint = SUPINT;
150*7c478bd9Sstevel@tonic-gate supint++; /* leave first term open for a count */
151*7c478bd9Sstevel@tonic-gate /* initialize using an empty term */
152*7c478bd9Sstevel@tonic-gate (void) strcpy(thisterm, "");
153*7c478bd9Sstevel@tonic-gate *supint++ = 0;
154*7c478bd9Sstevel@tonic-gate *supfing++ = ' ';
155*7c478bd9Sstevel@tonic-gate *supfing++ = '\0';
156*7c478bd9Sstevel@tonic-gate nextsupfing = 2;
157*7c478bd9Sstevel@tonic-gate #if DEBUG || STATS
158*7c478bd9Sstevel@tonic-gate totpost = 0L;
159*7c478bd9Sstevel@tonic-gate #endif
160*7c478bd9Sstevel@tonic-gate totterm = 0L;
161*7c478bd9Sstevel@tonic-gate numpost = 1;
162*7c478bd9Sstevel@tonic-gate
163*7c478bd9Sstevel@tonic-gate /*
164*7c478bd9Sstevel@tonic-gate * set up as though a block had come and gone, i.e., set up for
165*7c478bd9Sstevel@tonic-gate * new block
166*7c478bd9Sstevel@tonic-gate */
167*7c478bd9Sstevel@tonic-gate amtused = 16; /* leave no space - init 3 words + one for luck */
168*7c478bd9Sstevel@tonic-gate numinvitems = 0;
169*7c478bd9Sstevel@tonic-gate numlogblk = 0;
170*7c478bd9Sstevel@tonic-gate lastinblk = BLOCKSIZE;
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate /* now loop as long as more to read (till eof) */
173*7c478bd9Sstevel@tonic-gate while (fgets(line, LINEMAX, infile) != NULL) {
174*7c478bd9Sstevel@tonic-gate #if DEBUG || STATS
175*7c478bd9Sstevel@tonic-gate ++totpost;
176*7c478bd9Sstevel@tonic-gate #endif
177*7c478bd9Sstevel@tonic-gate s = (unsigned char *) strchr(line, SEP);
178*7c478bd9Sstevel@tonic-gate if (s == NULL) /* where did this line come from ??? */
179*7c478bd9Sstevel@tonic-gate continue; /* workaround: just skip it */
180*7c478bd9Sstevel@tonic-gate *s = '\0';
181*7c478bd9Sstevel@tonic-gate #if STATS
182*7c478bd9Sstevel@tonic-gate if ((i = strlen(line)) > maxtermlen) {
183*7c478bd9Sstevel@tonic-gate maxtermlen = i;
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate #endif
186*7c478bd9Sstevel@tonic-gate #if DEBUG
187*7c478bd9Sstevel@tonic-gate (void) printf("%ld: %s ", totpost, line);
188*7c478bd9Sstevel@tonic-gate (void) fflush(stdout);
189*7c478bd9Sstevel@tonic-gate #endif
190*7c478bd9Sstevel@tonic-gate if (strcmp(thisterm, line) == 0) {
191*7c478bd9Sstevel@tonic-gate if (postptr + 10 > POST + postsize / sizeof (POSTING)) {
192*7c478bd9Sstevel@tonic-gate i = postptr - POST;
193*7c478bd9Sstevel@tonic-gate postsize += POSTINC * sizeof (POSTING);
194*7c478bd9Sstevel@tonic-gate if ((POST = realloc(POST, postsize)) == NULL) {
195*7c478bd9Sstevel@tonic-gate invcannotalloc(postsize);
196*7c478bd9Sstevel@tonic-gate return (0);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate postptr = i + POST;
199*7c478bd9Sstevel@tonic-gate #if DEBUG
200*7c478bd9Sstevel@tonic-gate (void) printf("reallocated post space to %u, "
201*7c478bd9Sstevel@tonic-gate "totpost=%ld\n", postsize, totpost);
202*7c478bd9Sstevel@tonic-gate #endif
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate numpost++;
205*7c478bd9Sstevel@tonic-gate } else {
206*7c478bd9Sstevel@tonic-gate /* have a new term */
207*7c478bd9Sstevel@tonic-gate if (!invnewterm()) {
208*7c478bd9Sstevel@tonic-gate return (0);
209*7c478bd9Sstevel@tonic-gate }
210*7c478bd9Sstevel@tonic-gate (void) strcpy(thisterm, line);
211*7c478bd9Sstevel@tonic-gate numpost = 1;
212*7c478bd9Sstevel@tonic-gate postptr = POST;
213*7c478bd9Sstevel@tonic-gate fileindex = 0;
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate /* get the new posting */
216*7c478bd9Sstevel@tonic-gate num = *++s - '!';
217*7c478bd9Sstevel@tonic-gate i = 1;
218*7c478bd9Sstevel@tonic-gate do {
219*7c478bd9Sstevel@tonic-gate num = BASE * num + *++s - '!';
220*7c478bd9Sstevel@tonic-gate } while (++i < PRECISION);
221*7c478bd9Sstevel@tonic-gate posting.lineoffset = num;
222*7c478bd9Sstevel@tonic-gate while (++fileindex < nsrcoffset && num > srcoffset[fileindex]) {
223*7c478bd9Sstevel@tonic-gate ;
224*7c478bd9Sstevel@tonic-gate }
225*7c478bd9Sstevel@tonic-gate posting.fileindex = --fileindex;
226*7c478bd9Sstevel@tonic-gate posting.type = *++s;
227*7c478bd9Sstevel@tonic-gate num = *++s - '!';
228*7c478bd9Sstevel@tonic-gate if (*s != '\n') {
229*7c478bd9Sstevel@tonic-gate num = *++s - '!';
230*7c478bd9Sstevel@tonic-gate while (*++s != '\n') {
231*7c478bd9Sstevel@tonic-gate num = BASE * num + *s - '!';
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate posting.fcnoffset = num;
234*7c478bd9Sstevel@tonic-gate } else {
235*7c478bd9Sstevel@tonic-gate posting.fcnoffset = 0;
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate *postptr++ = posting;
238*7c478bd9Sstevel@tonic-gate #if DEBUG
239*7c478bd9Sstevel@tonic-gate (void) printf("%ld %ld %ld %ld\n", posting.fileindex,
240*7c478bd9Sstevel@tonic-gate posting.fcnoffset, posting.lineoffset, posting.type);
241*7c478bd9Sstevel@tonic-gate (void) fflush(stdout);
242*7c478bd9Sstevel@tonic-gate #endif
243*7c478bd9Sstevel@tonic-gate }
244*7c478bd9Sstevel@tonic-gate if (!invnewterm()) {
245*7c478bd9Sstevel@tonic-gate return (0);
246*7c478bd9Sstevel@tonic-gate }
247*7c478bd9Sstevel@tonic-gate /* now clean up final block */
248*7c478bd9Sstevel@tonic-gate logicalblk.invblk[0] = numinvitems;
249*7c478bd9Sstevel@tonic-gate /* loops pointer around to start */
250*7c478bd9Sstevel@tonic-gate logicalblk.invblk[1] = 0;
251*7c478bd9Sstevel@tonic-gate logicalblk.invblk[2] = numlogblk - 1;
252*7c478bd9Sstevel@tonic-gate if (fwrite((char *)&logicalblk, BLOCKSIZE, 1, outfile) == 0) {
253*7c478bd9Sstevel@tonic-gate goto cannotwrite;
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate numlogblk++;
256*7c478bd9Sstevel@tonic-gate /* write out block to save space. what in it doesn't matter */
257*7c478bd9Sstevel@tonic-gate if (fwrite((char *)&logicalblk, BLOCKSIZE, 1, outfile) == 0) {
258*7c478bd9Sstevel@tonic-gate goto cannotwrite;
259*7c478bd9Sstevel@tonic-gate }
260*7c478bd9Sstevel@tonic-gate /* finish up the super finger */
261*7c478bd9Sstevel@tonic-gate *SUPINT = numlogblk;
262*7c478bd9Sstevel@tonic-gate /* add to the offsets the size of the offset pointers */
263*7c478bd9Sstevel@tonic-gate intptr = (SUPINT + 1);
264*7c478bd9Sstevel@tonic-gate i = (char *)supint - (char *)SUPINT;
265*7c478bd9Sstevel@tonic-gate while (intptr < supint)
266*7c478bd9Sstevel@tonic-gate *intptr++ += i;
267*7c478bd9Sstevel@tonic-gate /* write out the offsets (1 for the N at start) and the super finger */
268*7c478bd9Sstevel@tonic-gate if (fwrite((char *)SUPINT, sizeof (*SUPINT), numlogblk + 1,
269*7c478bd9Sstevel@tonic-gate outfile) == 0 ||
270*7c478bd9Sstevel@tonic-gate fwrite(SUPFING, 1, supfing - SUPFING, outfile) == 0) {
271*7c478bd9Sstevel@tonic-gate goto cannotwrite;
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate /* save the size for reference later */
274*7c478bd9Sstevel@tonic-gate nextsupfing = sizeof (long) + sizeof (long) * numlogblk +
275*7c478bd9Sstevel@tonic-gate (supfing - SUPFING);
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate * make sure the file ends at a logical block boundary. This is
278*7c478bd9Sstevel@tonic-gate * necessary for invinsert to correctly create extended blocks
279*7c478bd9Sstevel@tonic-gate */
280*7c478bd9Sstevel@tonic-gate i = nextsupfing % BLOCKSIZE;
281*7c478bd9Sstevel@tonic-gate /* write out junk to fill log blk */
282*7c478bd9Sstevel@tonic-gate if (fwrite(SUPFING, BLOCKSIZE - i, 1, outfile) == 0 ||
283*7c478bd9Sstevel@tonic-gate fflush(outfile) == EOF) {
284*7c478bd9Sstevel@tonic-gate /* rewind doesn't check for write failure */
285*7c478bd9Sstevel@tonic-gate goto cannotwrite;
286*7c478bd9Sstevel@tonic-gate }
287*7c478bd9Sstevel@tonic-gate /* write the control area */
288*7c478bd9Sstevel@tonic-gate rewind(outfile);
289*7c478bd9Sstevel@tonic-gate param.version = VERSION;
290*7c478bd9Sstevel@tonic-gate param.filestat = 0;
291*7c478bd9Sstevel@tonic-gate param.sizeblk = BLOCKSIZE;
292*7c478bd9Sstevel@tonic-gate param.startbyte = (numlogblk + 1) * BLOCKSIZE + BUFSIZ;
293*7c478bd9Sstevel@tonic-gate param.supsize = nextsupfing;
294*7c478bd9Sstevel@tonic-gate param.cntlsize = BUFSIZ;
295*7c478bd9Sstevel@tonic-gate param.share = 0;
296*7c478bd9Sstevel@tonic-gate if (fwrite((char *)¶m, sizeof (param), 1, outfile) == 0) {
297*7c478bd9Sstevel@tonic-gate goto cannotwrite;
298*7c478bd9Sstevel@tonic-gate }
299*7c478bd9Sstevel@tonic-gate for (i = 0; i < 10; i++) /* for future use */
300*7c478bd9Sstevel@tonic-gate if (fwrite((char *)&zerolong, sizeof (zerolong),
301*7c478bd9Sstevel@tonic-gate 1, outfile) == 0) {
302*7c478bd9Sstevel@tonic-gate goto cannotwrite;
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate
305*7c478bd9Sstevel@tonic-gate /* make first block loop backwards to last block */
306*7c478bd9Sstevel@tonic-gate if (fflush(outfile) == EOF) {
307*7c478bd9Sstevel@tonic-gate /* fseek doesn't check for write failure */
308*7c478bd9Sstevel@tonic-gate goto cannotwrite;
309*7c478bd9Sstevel@tonic-gate }
310*7c478bd9Sstevel@tonic-gate /* get to second word first block */
311*7c478bd9Sstevel@tonic-gate (void) fseek(outfile, (long)BUFSIZ + 8, 0);
312*7c478bd9Sstevel@tonic-gate tlong = numlogblk - 1;
313*7c478bd9Sstevel@tonic-gate if (fwrite((char *)&tlong, sizeof (tlong), 1, outfile) == 0 ||
314*7c478bd9Sstevel@tonic-gate fclose(outfile) == EOF) {
315*7c478bd9Sstevel@tonic-gate cannotwrite:
316*7c478bd9Sstevel@tonic-gate invcannotwrite(invname);
317*7c478bd9Sstevel@tonic-gate return (0);
318*7c478bd9Sstevel@tonic-gate }
319*7c478bd9Sstevel@tonic-gate if (fclose(fpost) == EOF) {
320*7c478bd9Sstevel@tonic-gate invcannotwrite(postingfile);
321*7c478bd9Sstevel@tonic-gate return (0);
322*7c478bd9Sstevel@tonic-gate }
323*7c478bd9Sstevel@tonic-gate --totterm; /* don't count null term */
324*7c478bd9Sstevel@tonic-gate #if STATS
325*7c478bd9Sstevel@tonic-gate (void) printf("logical blocks = %d, postings = %ld, terms = %ld, "
326*7c478bd9Sstevel@tonic-gate "max term length = %d\n", numlogblk, totpost, totterm, maxtermlen);
327*7c478bd9Sstevel@tonic-gate if (showzipf) {
328*7c478bd9Sstevel@tonic-gate (void) printf(
329*7c478bd9Sstevel@tonic-gate "\n************* ZIPF curve ****************\n");
330*7c478bd9Sstevel@tonic-gate for (j = ZIPFSIZE; j > 1; j--)
331*7c478bd9Sstevel@tonic-gate if (zipf[j])
332*7c478bd9Sstevel@tonic-gate break;
333*7c478bd9Sstevel@tonic-gate for (i = 1; i < j; ++i) {
334*7c478bd9Sstevel@tonic-gate (void) printf("%3d -%6d ", i, zipf[i]);
335*7c478bd9Sstevel@tonic-gate if (i % 6 == 0) (void) putchar('\n');
336*7c478bd9Sstevel@tonic-gate }
337*7c478bd9Sstevel@tonic-gate (void) printf(">%d-%6d\n", ZIPFSIZE, zipf[0]);
338*7c478bd9Sstevel@tonic-gate }
339*7c478bd9Sstevel@tonic-gate #endif
340*7c478bd9Sstevel@tonic-gate /* free all malloc'd memory */
341*7c478bd9Sstevel@tonic-gate free(POST);
342*7c478bd9Sstevel@tonic-gate free(SUPFING);
343*7c478bd9Sstevel@tonic-gate free(SUPINT);
344*7c478bd9Sstevel@tonic-gate return (totterm);
345*7c478bd9Sstevel@tonic-gate }
346*7c478bd9Sstevel@tonic-gate
347*7c478bd9Sstevel@tonic-gate /* add a term to the data base */
348*7c478bd9Sstevel@tonic-gate
349*7c478bd9Sstevel@tonic-gate static int
invnewterm(void)350*7c478bd9Sstevel@tonic-gate invnewterm(void)
351*7c478bd9Sstevel@tonic-gate {
352*7c478bd9Sstevel@tonic-gate int backupflag, i, j, maxback, holditems, gooditems, howfar;
353*7c478bd9Sstevel@tonic-gate int len, numwilluse, wdlen;
354*7c478bd9Sstevel@tonic-gate char *tptr, *tptr2, *tptr3;
355*7c478bd9Sstevel@tonic-gate union {
356*7c478bd9Sstevel@tonic-gate unsigned long packword[2];
357*7c478bd9Sstevel@tonic-gate ENTRY e;
358*7c478bd9Sstevel@tonic-gate } iteminfo;
359*7c478bd9Sstevel@tonic-gate
360*7c478bd9Sstevel@tonic-gate totterm++;
361*7c478bd9Sstevel@tonic-gate #if STATS
362*7c478bd9Sstevel@tonic-gate /* keep zipfian info on the distribution */
363*7c478bd9Sstevel@tonic-gate if (numpost <= ZIPFSIZE)
364*7c478bd9Sstevel@tonic-gate zipf[numpost]++;
365*7c478bd9Sstevel@tonic-gate else
366*7c478bd9Sstevel@tonic-gate zipf[0]++;
367*7c478bd9Sstevel@tonic-gate #endif
368*7c478bd9Sstevel@tonic-gate len = strlen(thisterm);
369*7c478bd9Sstevel@tonic-gate wdlen = (len + (sizeof (long) - 1)) / sizeof (long);
370*7c478bd9Sstevel@tonic-gate numwilluse = (wdlen + 3) * sizeof (long);
371*7c478bd9Sstevel@tonic-gate /* new block if at least 1 item in block */
372*7c478bd9Sstevel@tonic-gate if (numinvitems && numwilluse + amtused > BLOCKSIZE) {
373*7c478bd9Sstevel@tonic-gate /* set up new block */
374*7c478bd9Sstevel@tonic-gate if (supfing + 500 > SUPFING + supersize) {
375*7c478bd9Sstevel@tonic-gate i = supfing - SUPFING;
376*7c478bd9Sstevel@tonic-gate supersize += 20000;
377*7c478bd9Sstevel@tonic-gate if ((SUPFING = realloc(SUPFING, supersize)) == NULL) {
378*7c478bd9Sstevel@tonic-gate invcannotalloc(supersize);
379*7c478bd9Sstevel@tonic-gate return (0);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate supfing = i + SUPFING;
382*7c478bd9Sstevel@tonic-gate #if DEBUG
383*7c478bd9Sstevel@tonic-gate (void) printf("reallocated superfinger space to %d, "
384*7c478bd9Sstevel@tonic-gate "totpost=%ld\n", supersize, totpost);
385*7c478bd9Sstevel@tonic-gate #endif
386*7c478bd9Sstevel@tonic-gate }
387*7c478bd9Sstevel@tonic-gate /* check that room for the offset as well */
388*7c478bd9Sstevel@tonic-gate if ((numlogblk + 10) > supintsize) {
389*7c478bd9Sstevel@tonic-gate i = supint - SUPINT;
390*7c478bd9Sstevel@tonic-gate supintsize += SUPERINC;
391*7c478bd9Sstevel@tonic-gate if ((SUPINT = realloc((char *)SUPINT,
392*7c478bd9Sstevel@tonic-gate supintsize * sizeof (long))) == NULL) {
393*7c478bd9Sstevel@tonic-gate invcannotalloc(supintsize * sizeof (long));
394*7c478bd9Sstevel@tonic-gate return (0);
395*7c478bd9Sstevel@tonic-gate }
396*7c478bd9Sstevel@tonic-gate supint = i + SUPINT;
397*7c478bd9Sstevel@tonic-gate #if DEBUG
398*7c478bd9Sstevel@tonic-gate (void) printf("reallocated superfinger offset to %d, "
399*7c478bd9Sstevel@tonic-gate "totpost = %ld\n", supintsize * sizeof (long),
400*7c478bd9Sstevel@tonic-gate totpost);
401*7c478bd9Sstevel@tonic-gate #endif
402*7c478bd9Sstevel@tonic-gate }
403*7c478bd9Sstevel@tonic-gate /* See if backup is efficatious */
404*7c478bd9Sstevel@tonic-gate backupflag = 0;
405*7c478bd9Sstevel@tonic-gate maxback = strlen(thisterm) / 10;
406*7c478bd9Sstevel@tonic-gate holditems = numinvitems;
407*7c478bd9Sstevel@tonic-gate if (maxback > numinvitems)
408*7c478bd9Sstevel@tonic-gate maxback = numinvitems - 2;
409*7c478bd9Sstevel@tonic-gate howfar = 0;
410*7c478bd9Sstevel@tonic-gate while (--maxback > 0) {
411*7c478bd9Sstevel@tonic-gate howfar++;
412*7c478bd9Sstevel@tonic-gate iteminfo.packword[0] =
413*7c478bd9Sstevel@tonic-gate logicalblk.invblk[--holditems * 2 +
414*7c478bd9Sstevel@tonic-gate (sizeof (long) - 1)];
415*7c478bd9Sstevel@tonic-gate if ((i = iteminfo.e.size / 10) < maxback) {
416*7c478bd9Sstevel@tonic-gate maxback = i;
417*7c478bd9Sstevel@tonic-gate backupflag = howfar;
418*7c478bd9Sstevel@tonic-gate gooditems = holditems;
419*7c478bd9Sstevel@tonic-gate tptr2 = logicalblk.chrblk + iteminfo.e.offset;
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate }
422*7c478bd9Sstevel@tonic-gate /* see if backup will occur */
423*7c478bd9Sstevel@tonic-gate if (backupflag) {
424*7c478bd9Sstevel@tonic-gate numinvitems = gooditems;
425*7c478bd9Sstevel@tonic-gate }
426*7c478bd9Sstevel@tonic-gate logicalblk.invblk[0] = numinvitems;
427*7c478bd9Sstevel@tonic-gate /* set forward pointer pointing to next */
428*7c478bd9Sstevel@tonic-gate logicalblk.invblk[1] = numlogblk + 1;
429*7c478bd9Sstevel@tonic-gate /* set back pointer to last block */
430*7c478bd9Sstevel@tonic-gate logicalblk.invblk[2] = numlogblk - 1;
431*7c478bd9Sstevel@tonic-gate if (fwrite((char *)logicalblk.chrblk, 1,
432*7c478bd9Sstevel@tonic-gate BLOCKSIZE, outfile) == 0) {
433*7c478bd9Sstevel@tonic-gate invcannotwrite(indexfile);
434*7c478bd9Sstevel@tonic-gate return (0);
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate amtused = 16;
437*7c478bd9Sstevel@tonic-gate numlogblk++;
438*7c478bd9Sstevel@tonic-gate /* check if had to back up, if so do it */
439*7c478bd9Sstevel@tonic-gate if (backupflag) {
440*7c478bd9Sstevel@tonic-gate /* find out where the end of the new block is */
441*7c478bd9Sstevel@tonic-gate iteminfo.packword[0] =
442*7c478bd9Sstevel@tonic-gate logicalblk.invblk[numinvitems * 2 + 1];
443*7c478bd9Sstevel@tonic-gate tptr3 = logicalblk.chrblk + iteminfo.e.offset;
444*7c478bd9Sstevel@tonic-gate /* move the index for this block */
445*7c478bd9Sstevel@tonic-gate for (i = 3; i <= (backupflag * 2 + 2); i++) {
446*7c478bd9Sstevel@tonic-gate logicalblk.invblk[i] =
447*7c478bd9Sstevel@tonic-gate logicalblk.invblk[numinvitems * 2+i];
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate /* move the word into the super index */
450*7c478bd9Sstevel@tonic-gate iteminfo.packword[0] = logicalblk.invblk[3];
451*7c478bd9Sstevel@tonic-gate iteminfo.packword[1] = logicalblk.invblk[4];
452*7c478bd9Sstevel@tonic-gate tptr2 = logicalblk.chrblk + iteminfo.e.offset;
453*7c478bd9Sstevel@tonic-gate (void) strncpy(supfing, tptr2, (int)iteminfo.e.size);
454*7c478bd9Sstevel@tonic-gate *(supfing + iteminfo.e.size) = '\0';
455*7c478bd9Sstevel@tonic-gate #if DEBUG
456*7c478bd9Sstevel@tonic-gate (void) printf("backup %d at term=%s to term=%s\n",
457*7c478bd9Sstevel@tonic-gate backupflag, thisterm, supfing);
458*7c478bd9Sstevel@tonic-gate #endif
459*7c478bd9Sstevel@tonic-gate *supint++ = nextsupfing;
460*7c478bd9Sstevel@tonic-gate nextsupfing += strlen(supfing) + 1;
461*7c478bd9Sstevel@tonic-gate supfing += strlen(supfing) + 1;
462*7c478bd9Sstevel@tonic-gate /* now fix up the logical block */
463*7c478bd9Sstevel@tonic-gate tptr = logicalblk.chrblk + lastinblk;
464*7c478bd9Sstevel@tonic-gate lastinblk = BLOCKSIZE;
465*7c478bd9Sstevel@tonic-gate tptr2 = logicalblk.chrblk + lastinblk;
466*7c478bd9Sstevel@tonic-gate j = tptr3 - tptr;
467*7c478bd9Sstevel@tonic-gate while (tptr3 > tptr)
468*7c478bd9Sstevel@tonic-gate *--tptr2 = *--tptr3;
469*7c478bd9Sstevel@tonic-gate lastinblk -= j;
470*7c478bd9Sstevel@tonic-gate amtused += (8 * backupflag + j);
471*7c478bd9Sstevel@tonic-gate for (i = 3; i < (backupflag * 2 + 2); i += 2) {
472*7c478bd9Sstevel@tonic-gate iteminfo.packword[0] = logicalblk.invblk[i];
473*7c478bd9Sstevel@tonic-gate iteminfo.e.offset += (tptr2 - tptr3);
474*7c478bd9Sstevel@tonic-gate logicalblk.invblk[i] = iteminfo.packword[0];
475*7c478bd9Sstevel@tonic-gate }
476*7c478bd9Sstevel@tonic-gate numinvitems = backupflag;
477*7c478bd9Sstevel@tonic-gate } else { /* no backup needed */
478*7c478bd9Sstevel@tonic-gate numinvitems = 0;
479*7c478bd9Sstevel@tonic-gate lastinblk = BLOCKSIZE;
480*7c478bd9Sstevel@tonic-gate /* add new term to superindex */
481*7c478bd9Sstevel@tonic-gate (void) strcpy(supfing, thisterm);
482*7c478bd9Sstevel@tonic-gate supfing += strlen(thisterm) + 1;
483*7c478bd9Sstevel@tonic-gate *supint++ = nextsupfing;
484*7c478bd9Sstevel@tonic-gate nextsupfing += strlen(thisterm) + 1;
485*7c478bd9Sstevel@tonic-gate }
486*7c478bd9Sstevel@tonic-gate }
487*7c478bd9Sstevel@tonic-gate lastinblk -= (numwilluse - 8);
488*7c478bd9Sstevel@tonic-gate iteminfo.e.offset = lastinblk;
489*7c478bd9Sstevel@tonic-gate iteminfo.e.size = (char)len;
490*7c478bd9Sstevel@tonic-gate iteminfo.e.space = 0;
491*7c478bd9Sstevel@tonic-gate iteminfo.e.post = numpost;
492*7c478bd9Sstevel@tonic-gate (void) strncpy(logicalblk.chrblk + lastinblk, thisterm, len);
493*7c478bd9Sstevel@tonic-gate amtused += numwilluse;
494*7c478bd9Sstevel@tonic-gate logicalblk.invblk[(lastinblk/sizeof (long))+wdlen] = nextpost;
495*7c478bd9Sstevel@tonic-gate if ((i = postptr - POST) > 0) {
496*7c478bd9Sstevel@tonic-gate if (fwrite((char *)POST, sizeof (POSTING), i, fpost) == 0) {
497*7c478bd9Sstevel@tonic-gate invcannotwrite(postingfile);
498*7c478bd9Sstevel@tonic-gate return (0);
499*7c478bd9Sstevel@tonic-gate }
500*7c478bd9Sstevel@tonic-gate nextpost += i * sizeof (POSTING);
501*7c478bd9Sstevel@tonic-gate }
502*7c478bd9Sstevel@tonic-gate logicalblk.invblk[3+2*numinvitems++] = iteminfo.packword[0];
503*7c478bd9Sstevel@tonic-gate logicalblk.invblk[2+2*numinvitems] = iteminfo.packword[1];
504*7c478bd9Sstevel@tonic-gate return (1);
505*7c478bd9Sstevel@tonic-gate }
506*7c478bd9Sstevel@tonic-gate
507*7c478bd9Sstevel@tonic-gate static void
swap_ints(void * p,size_t sz)508*7c478bd9Sstevel@tonic-gate swap_ints(void *p, size_t sz)
509*7c478bd9Sstevel@tonic-gate {
510*7c478bd9Sstevel@tonic-gate uint32_t *s;
511*7c478bd9Sstevel@tonic-gate uint32_t *e = (uint32_t *)p + (sz / sizeof (uint32_t));
512*7c478bd9Sstevel@tonic-gate
513*7c478bd9Sstevel@tonic-gate for (s = p; s < e; s++)
514*7c478bd9Sstevel@tonic-gate *s = BSWAP_32(*s);
515*7c478bd9Sstevel@tonic-gate }
516*7c478bd9Sstevel@tonic-gate
517*7c478bd9Sstevel@tonic-gate static void
write_param(INVCONTROL * invcntl)518*7c478bd9Sstevel@tonic-gate write_param(INVCONTROL *invcntl)
519*7c478bd9Sstevel@tonic-gate {
520*7c478bd9Sstevel@tonic-gate if (invcntl->swap)
521*7c478bd9Sstevel@tonic-gate swap_ints(&invcntl->param, sizeof (invcntl->param));
522*7c478bd9Sstevel@tonic-gate
523*7c478bd9Sstevel@tonic-gate rewind(invcntl->invfile);
524*7c478bd9Sstevel@tonic-gate (void) fwrite((char *)&invcntl->param, sizeof (invcntl->param), 1,
525*7c478bd9Sstevel@tonic-gate invcntl->invfile);
526*7c478bd9Sstevel@tonic-gate
527*7c478bd9Sstevel@tonic-gate if (invcntl->swap)
528*7c478bd9Sstevel@tonic-gate swap_ints(&invcntl->param, sizeof (invcntl->param));
529*7c478bd9Sstevel@tonic-gate }
530*7c478bd9Sstevel@tonic-gate
531*7c478bd9Sstevel@tonic-gate static void
read_superfinger(INVCONTROL * invcntl)532*7c478bd9Sstevel@tonic-gate read_superfinger(INVCONTROL *invcntl)
533*7c478bd9Sstevel@tonic-gate {
534*7c478bd9Sstevel@tonic-gate size_t count;
535*7c478bd9Sstevel@tonic-gate
536*7c478bd9Sstevel@tonic-gate (void) fseek(invcntl->invfile, invcntl->param.startbyte, SEEK_SET);
537*7c478bd9Sstevel@tonic-gate (void) fread(invcntl->iindex, (int)invcntl->param.supsize,
538*7c478bd9Sstevel@tonic-gate 1, invcntl->invfile);
539*7c478bd9Sstevel@tonic-gate
540*7c478bd9Sstevel@tonic-gate if (invcntl->swap) {
541*7c478bd9Sstevel@tonic-gate /*
542*7c478bd9Sstevel@tonic-gate * The superfinger consists of a count, followed by
543*7c478bd9Sstevel@tonic-gate * count offsets, followed by a string table (which
544*7c478bd9Sstevel@tonic-gate * the offsets reference).
545*7c478bd9Sstevel@tonic-gate *
546*7c478bd9Sstevel@tonic-gate * We need to swap the count and the offsets.
547*7c478bd9Sstevel@tonic-gate */
548*7c478bd9Sstevel@tonic-gate count = 1 + BSWAP_32(*(uint32_t *)invcntl->iindex);
549*7c478bd9Sstevel@tonic-gate swap_ints(invcntl->iindex, count * sizeof (unsigned long));
550*7c478bd9Sstevel@tonic-gate }
551*7c478bd9Sstevel@tonic-gate }
552*7c478bd9Sstevel@tonic-gate
553*7c478bd9Sstevel@tonic-gate static void
read_logblock(INVCONTROL * invcntl,int block)554*7c478bd9Sstevel@tonic-gate read_logblock(INVCONTROL *invcntl, int block)
555*7c478bd9Sstevel@tonic-gate {
556*7c478bd9Sstevel@tonic-gate /* note always fetch it if the file is busy */
557*7c478bd9Sstevel@tonic-gate if ((block != invcntl->numblk) ||
558*7c478bd9Sstevel@tonic-gate (invcntl->param.filestat >= INVBUSY)) {
559*7c478bd9Sstevel@tonic-gate (void) fseek(invcntl->invfile,
560*7c478bd9Sstevel@tonic-gate (block * invcntl->param.sizeblk) + invcntl->param.cntlsize,
561*7c478bd9Sstevel@tonic-gate SEEK_SET);
562*7c478bd9Sstevel@tonic-gate invcntl->numblk = block;
563*7c478bd9Sstevel@tonic-gate (void) fread(invcntl->logblk, (int)invcntl->param.sizeblk,
564*7c478bd9Sstevel@tonic-gate 1, invcntl->invfile);
565*7c478bd9Sstevel@tonic-gate
566*7c478bd9Sstevel@tonic-gate if (invcntl->swap) {
567*7c478bd9Sstevel@tonic-gate size_t count;
568*7c478bd9Sstevel@tonic-gate ENTRY *ecur, *eend;
569*7c478bd9Sstevel@tonic-gate uint32_t *postptr;
570*7c478bd9Sstevel@tonic-gate
571*7c478bd9Sstevel@tonic-gate /*
572*7c478bd9Sstevel@tonic-gate * A logblock consists of a count, a next block id,
573*7c478bd9Sstevel@tonic-gate * and a previous block id, followed by count
574*7c478bd9Sstevel@tonic-gate * ENTRYs, followed by alternating strings and
575*7c478bd9Sstevel@tonic-gate * offsets.
576*7c478bd9Sstevel@tonic-gate */
577*7c478bd9Sstevel@tonic-gate swap_ints(invcntl->logblk, 3 * sizeof (unsigned long));
578*7c478bd9Sstevel@tonic-gate
579*7c478bd9Sstevel@tonic-gate count = *(uint32_t *)invcntl->logblk;
580*7c478bd9Sstevel@tonic-gate
581*7c478bd9Sstevel@tonic-gate ecur = (ENTRY *)((uint32_t *)invcntl->logblk + 3);
582*7c478bd9Sstevel@tonic-gate eend = ecur + count;
583*7c478bd9Sstevel@tonic-gate
584*7c478bd9Sstevel@tonic-gate for (; ecur < eend; ecur++) {
585*7c478bd9Sstevel@tonic-gate ecur->offset = BSWAP_16(ecur->offset);
586*7c478bd9Sstevel@tonic-gate ecur->post = BSWAP_32(ecur->post);
587*7c478bd9Sstevel@tonic-gate
588*7c478bd9Sstevel@tonic-gate /*
589*7c478bd9Sstevel@tonic-gate * After the string is the posting offset.
590*7c478bd9Sstevel@tonic-gate */
591*7c478bd9Sstevel@tonic-gate postptr = (uint32_t *)(invcntl->logblk +
592*7c478bd9Sstevel@tonic-gate ecur->offset +
593*7c478bd9Sstevel@tonic-gate P2ROUNDUP(ecur->size, sizeof (long)));
594*7c478bd9Sstevel@tonic-gate
595*7c478bd9Sstevel@tonic-gate *postptr = BSWAP_32(*postptr);
596*7c478bd9Sstevel@tonic-gate }
597*7c478bd9Sstevel@tonic-gate }
598*7c478bd9Sstevel@tonic-gate }
599*7c478bd9Sstevel@tonic-gate }
600*7c478bd9Sstevel@tonic-gate
601*7c478bd9Sstevel@tonic-gate void
read_next_posting(INVCONTROL * invcntl,POSTING * posting)602*7c478bd9Sstevel@tonic-gate read_next_posting(INVCONTROL *invcntl, POSTING *posting)
603*7c478bd9Sstevel@tonic-gate {
604*7c478bd9Sstevel@tonic-gate (void) fread((char *)posting, sizeof (*posting), 1, invcntl->postfile);
605*7c478bd9Sstevel@tonic-gate if (invcntl->swap) {
606*7c478bd9Sstevel@tonic-gate posting->lineoffset = BSWAP_32(posting->lineoffset);
607*7c478bd9Sstevel@tonic-gate posting->fcnoffset = BSWAP_32(posting->fcnoffset);
608*7c478bd9Sstevel@tonic-gate /*
609*7c478bd9Sstevel@tonic-gate * fileindex is a 24-bit field, so shift it before swapping
610*7c478bd9Sstevel@tonic-gate */
611*7c478bd9Sstevel@tonic-gate posting->fileindex = BSWAP_32(posting->fileindex << 8);
612*7c478bd9Sstevel@tonic-gate }
613*7c478bd9Sstevel@tonic-gate }
614*7c478bd9Sstevel@tonic-gate
615*7c478bd9Sstevel@tonic-gate int
invopen(INVCONTROL * invcntl,char * invname,char * invpost,int stat)616*7c478bd9Sstevel@tonic-gate invopen(INVCONTROL *invcntl, char *invname, char *invpost, int stat)
617*7c478bd9Sstevel@tonic-gate {
618*7c478bd9Sstevel@tonic-gate int read_index;
619*7c478bd9Sstevel@tonic-gate
620*7c478bd9Sstevel@tonic-gate if ((invcntl->invfile = vpfopen(invname,
621*7c478bd9Sstevel@tonic-gate ((stat == 0) ? FREAD : FREADP))) == NULL) {
622*7c478bd9Sstevel@tonic-gate invcannotopen(invname);
623*7c478bd9Sstevel@tonic-gate return (-1);
624*7c478bd9Sstevel@tonic-gate }
625*7c478bd9Sstevel@tonic-gate if (fread((char *)&invcntl->param, sizeof (invcntl->param), 1,
626*7c478bd9Sstevel@tonic-gate invcntl->invfile) == 0) {
627*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: empty inverted file\n", argv0);
628*7c478bd9Sstevel@tonic-gate goto closeinv;
629*7c478bd9Sstevel@tonic-gate }
630*7c478bd9Sstevel@tonic-gate if (invcntl->param.version != VERSION &&
631*7c478bd9Sstevel@tonic-gate BSWAP_32(invcntl->param.version) != VERSION) {
632*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
633*7c478bd9Sstevel@tonic-gate "%s: cannot read old index format; use -U option to "
634*7c478bd9Sstevel@tonic-gate "force database to rebuild\n", argv0);
635*7c478bd9Sstevel@tonic-gate goto closeinv;
636*7c478bd9Sstevel@tonic-gate }
637*7c478bd9Sstevel@tonic-gate invcntl->swap = (invcntl->param.version != VERSION);
638*7c478bd9Sstevel@tonic-gate if (invcntl->swap)
639*7c478bd9Sstevel@tonic-gate swap_ints(&invcntl->param, sizeof (invcntl->param));
640*7c478bd9Sstevel@tonic-gate
641*7c478bd9Sstevel@tonic-gate if (stat == 0 && invcntl->param.filestat == INVALONE) {
642*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: inverted file is locked\n", argv0);
643*7c478bd9Sstevel@tonic-gate goto closeinv;
644*7c478bd9Sstevel@tonic-gate }
645*7c478bd9Sstevel@tonic-gate if ((invcntl->postfile = vpfopen(invpost,
646*7c478bd9Sstevel@tonic-gate ((stat == 0) ? FREAD : FREADP))) == NULL) {
647*7c478bd9Sstevel@tonic-gate invcannotopen(invpost);
648*7c478bd9Sstevel@tonic-gate goto closeinv;
649*7c478bd9Sstevel@tonic-gate }
650*7c478bd9Sstevel@tonic-gate /* allocate core for a logical block */
651*7c478bd9Sstevel@tonic-gate if ((invcntl->logblk = malloc(invcntl->param.sizeblk)) == NULL) {
652*7c478bd9Sstevel@tonic-gate invcannotalloc((unsigned)invcntl->param.sizeblk);
653*7c478bd9Sstevel@tonic-gate goto closeboth;
654*7c478bd9Sstevel@tonic-gate }
655*7c478bd9Sstevel@tonic-gate /* allocate for and read in superfinger */
656*7c478bd9Sstevel@tonic-gate read_index = 1;
657*7c478bd9Sstevel@tonic-gate invcntl->iindex = NULL;
658*7c478bd9Sstevel@tonic-gate #if SHARE
659*7c478bd9Sstevel@tonic-gate if (invcntl->param.share == 1) {
660*7c478bd9Sstevel@tonic-gate key_t ftok(), shm_key;
661*7c478bd9Sstevel@tonic-gate struct shmid_ds shm_buf;
662*7c478bd9Sstevel@tonic-gate char *shmat();
663*7c478bd9Sstevel@tonic-gate int shm_id;
664*7c478bd9Sstevel@tonic-gate
665*7c478bd9Sstevel@tonic-gate /* see if the shared segment exists */
666*7c478bd9Sstevel@tonic-gate shm_key = ftok(invname, 2);
667*7c478bd9Sstevel@tonic-gate shm_id = shmget(shm_key, 0, 0);
668*7c478bd9Sstevel@tonic-gate /*
669*7c478bd9Sstevel@tonic-gate * Failure simply means (hopefully) that segment doesn't
670*7c478bd9Sstevel@tonic-gate * exist
671*7c478bd9Sstevel@tonic-gate */
672*7c478bd9Sstevel@tonic-gate if (shm_id == -1) {
673*7c478bd9Sstevel@tonic-gate /*
674*7c478bd9Sstevel@tonic-gate * Have to give general write permission due to AMdahl
675*7c478bd9Sstevel@tonic-gate * not having protected segments
676*7c478bd9Sstevel@tonic-gate */
677*7c478bd9Sstevel@tonic-gate shm_id = shmget(shm_key,
678*7c478bd9Sstevel@tonic-gate invcntl->param.supsize + sizeof (long),
679*7c478bd9Sstevel@tonic-gate IPC_CREAT | 0666);
680*7c478bd9Sstevel@tonic-gate if (shm_id == -1)
681*7c478bd9Sstevel@tonic-gate perror("Could not create shared "
682*7c478bd9Sstevel@tonic-gate "memory segment");
683*7c478bd9Sstevel@tonic-gate } else
684*7c478bd9Sstevel@tonic-gate read_index = 0;
685*7c478bd9Sstevel@tonic-gate
686*7c478bd9Sstevel@tonic-gate if (shm_id != -1) {
687*7c478bd9Sstevel@tonic-gate invcntl->iindex = shmat(shm_id, 0,
688*7c478bd9Sstevel@tonic-gate ((read_index) ? 0 : SHM_RDONLY));
689*7c478bd9Sstevel@tonic-gate if (invcntl->iindex == (char *)ERR) {
690*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
691*7c478bd9Sstevel@tonic-gate "%s: shared memory link failed\n", argv0);
692*7c478bd9Sstevel@tonic-gate invcntl->iindex = NULL;
693*7c478bd9Sstevel@tonic-gate read_index = 1;
694*7c478bd9Sstevel@tonic-gate }
695*7c478bd9Sstevel@tonic-gate }
696*7c478bd9Sstevel@tonic-gate }
697*7c478bd9Sstevel@tonic-gate #endif
698*7c478bd9Sstevel@tonic-gate if (invcntl->iindex == NULL)
699*7c478bd9Sstevel@tonic-gate invcntl->iindex = malloc(invcntl->param.supsize + 16);
700*7c478bd9Sstevel@tonic-gate if (invcntl->iindex == NULL) {
701*7c478bd9Sstevel@tonic-gate invcannotalloc((unsigned)invcntl->param.supsize);
702*7c478bd9Sstevel@tonic-gate free(invcntl->logblk);
703*7c478bd9Sstevel@tonic-gate goto closeboth;
704*7c478bd9Sstevel@tonic-gate }
705*7c478bd9Sstevel@tonic-gate if (read_index) {
706*7c478bd9Sstevel@tonic-gate read_superfinger(invcntl);
707*7c478bd9Sstevel@tonic-gate }
708*7c478bd9Sstevel@tonic-gate invcntl->numblk = -1;
709*7c478bd9Sstevel@tonic-gate if (boolready() == -1) {
710*7c478bd9Sstevel@tonic-gate closeboth:
711*7c478bd9Sstevel@tonic-gate (void) fclose(invcntl->postfile);
712*7c478bd9Sstevel@tonic-gate closeinv:
713*7c478bd9Sstevel@tonic-gate (void) fclose(invcntl->invfile);
714*7c478bd9Sstevel@tonic-gate return (-1);
715*7c478bd9Sstevel@tonic-gate }
716*7c478bd9Sstevel@tonic-gate /* write back out the control block if anything changed */
717*7c478bd9Sstevel@tonic-gate invcntl->param.filestat = stat;
718*7c478bd9Sstevel@tonic-gate if (stat > invcntl->param.filestat)
719*7c478bd9Sstevel@tonic-gate write_param(invcntl);
720*7c478bd9Sstevel@tonic-gate return (1);
721*7c478bd9Sstevel@tonic-gate }
722*7c478bd9Sstevel@tonic-gate
723*7c478bd9Sstevel@tonic-gate /* invclose must be called to wrap things up and deallocate core */
724*7c478bd9Sstevel@tonic-gate void
invclose(INVCONTROL * invcntl)725*7c478bd9Sstevel@tonic-gate invclose(INVCONTROL *invcntl)
726*7c478bd9Sstevel@tonic-gate {
727*7c478bd9Sstevel@tonic-gate /* write out the control block in case anything changed */
728*7c478bd9Sstevel@tonic-gate if (invcntl->param.filestat > 0) {
729*7c478bd9Sstevel@tonic-gate invcntl->param.filestat = 0;
730*7c478bd9Sstevel@tonic-gate write_param(invcntl);
731*7c478bd9Sstevel@tonic-gate }
732*7c478bd9Sstevel@tonic-gate (void) fclose(invcntl->invfile);
733*7c478bd9Sstevel@tonic-gate (void) fclose(invcntl->postfile);
734*7c478bd9Sstevel@tonic-gate #if SHARE
735*7c478bd9Sstevel@tonic-gate if (invcntl->param.share > 0) {
736*7c478bd9Sstevel@tonic-gate shmdt(invcntl->iindex);
737*7c478bd9Sstevel@tonic-gate invcntl->iindex = NULL;
738*7c478bd9Sstevel@tonic-gate }
739*7c478bd9Sstevel@tonic-gate #endif
740*7c478bd9Sstevel@tonic-gate if (invcntl->iindex != NULL)
741*7c478bd9Sstevel@tonic-gate free(invcntl->iindex);
742*7c478bd9Sstevel@tonic-gate free(invcntl->logblk);
743*7c478bd9Sstevel@tonic-gate }
744*7c478bd9Sstevel@tonic-gate
745*7c478bd9Sstevel@tonic-gate /* invstep steps the inverted file forward one item */
746*7c478bd9Sstevel@tonic-gate void
invstep(INVCONTROL * invcntl)747*7c478bd9Sstevel@tonic-gate invstep(INVCONTROL *invcntl)
748*7c478bd9Sstevel@tonic-gate {
749*7c478bd9Sstevel@tonic-gate if (invcntl->keypnt < (*(int *)invcntl->logblk - 1)) {
750*7c478bd9Sstevel@tonic-gate invcntl->keypnt++;
751*7c478bd9Sstevel@tonic-gate return;
752*7c478bd9Sstevel@tonic-gate }
753*7c478bd9Sstevel@tonic-gate
754*7c478bd9Sstevel@tonic-gate /* move forward a block else wrap */
755*7c478bd9Sstevel@tonic-gate read_logblock(invcntl, *(int *)(invcntl->logblk + sizeof (long)));
756*7c478bd9Sstevel@tonic-gate
757*7c478bd9Sstevel@tonic-gate invcntl->keypnt = 0;
758*7c478bd9Sstevel@tonic-gate }
759*7c478bd9Sstevel@tonic-gate
760*7c478bd9Sstevel@tonic-gate /* invforward moves forward one term in the inverted file */
761*7c478bd9Sstevel@tonic-gate int
invforward(INVCONTROL * invcntl)762*7c478bd9Sstevel@tonic-gate invforward(INVCONTROL *invcntl)
763*7c478bd9Sstevel@tonic-gate {
764*7c478bd9Sstevel@tonic-gate invstep(invcntl);
765*7c478bd9Sstevel@tonic-gate /* skip things with 0 postings */
766*7c478bd9Sstevel@tonic-gate while (((ENTRY *)(invcntl->logblk + 12) + invcntl->keypnt)->post == 0) {
767*7c478bd9Sstevel@tonic-gate invstep(invcntl);
768*7c478bd9Sstevel@tonic-gate }
769*7c478bd9Sstevel@tonic-gate /* Check for having wrapped - reached start of inverted file! */
770*7c478bd9Sstevel@tonic-gate if ((invcntl->numblk == 0) && (invcntl->keypnt == 0))
771*7c478bd9Sstevel@tonic-gate return (0);
772*7c478bd9Sstevel@tonic-gate return (1);
773*7c478bd9Sstevel@tonic-gate }
774*7c478bd9Sstevel@tonic-gate
775*7c478bd9Sstevel@tonic-gate /* invterm gets the present term from the present logical block */
776*7c478bd9Sstevel@tonic-gate int
invterm(INVCONTROL * invcntl,char * term)777*7c478bd9Sstevel@tonic-gate invterm(INVCONTROL *invcntl, char *term)
778*7c478bd9Sstevel@tonic-gate {
779*7c478bd9Sstevel@tonic-gate ENTRY * entryptr;
780*7c478bd9Sstevel@tonic-gate
781*7c478bd9Sstevel@tonic-gate entryptr = (ENTRY *)(invcntl->logblk + 12) + invcntl->keypnt;
782*7c478bd9Sstevel@tonic-gate (void) strncpy(term, entryptr->offset + invcntl->logblk,
783*7c478bd9Sstevel@tonic-gate (int)entryptr->size);
784*7c478bd9Sstevel@tonic-gate *(term + entryptr->size) = '\0';
785*7c478bd9Sstevel@tonic-gate return (entryptr->post);
786*7c478bd9Sstevel@tonic-gate }
787*7c478bd9Sstevel@tonic-gate
788*7c478bd9Sstevel@tonic-gate /* invfind searches for an individual item in the inverted file */
789*7c478bd9Sstevel@tonic-gate long
invfind(INVCONTROL * invcntl,char * searchterm)790*7c478bd9Sstevel@tonic-gate invfind(INVCONTROL *invcntl, char *searchterm)
791*7c478bd9Sstevel@tonic-gate {
792*7c478bd9Sstevel@tonic-gate int imid, ilow, ihigh;
793*7c478bd9Sstevel@tonic-gate long num;
794*7c478bd9Sstevel@tonic-gate int i;
795*7c478bd9Sstevel@tonic-gate unsigned long *intptr, *intptr2;
796*7c478bd9Sstevel@tonic-gate ENTRY *entryptr;
797*7c478bd9Sstevel@tonic-gate
798*7c478bd9Sstevel@tonic-gate /* make sure it is initialized via invready */
799*7c478bd9Sstevel@tonic-gate if (invcntl->invfile == 0)
800*7c478bd9Sstevel@tonic-gate return (-1L);
801*7c478bd9Sstevel@tonic-gate
802*7c478bd9Sstevel@tonic-gate /* now search for the appropriate finger block */
803*7c478bd9Sstevel@tonic-gate intptr = (unsigned long *)invcntl->iindex;
804*7c478bd9Sstevel@tonic-gate
805*7c478bd9Sstevel@tonic-gate ilow = 0;
806*7c478bd9Sstevel@tonic-gate ihigh = *intptr++ - 1;
807*7c478bd9Sstevel@tonic-gate while (ilow <= ihigh) {
808*7c478bd9Sstevel@tonic-gate imid = (ilow + ihigh) / 2;
809*7c478bd9Sstevel@tonic-gate intptr2 = intptr + imid;
810*7c478bd9Sstevel@tonic-gate i = strcmp(searchterm, (invcntl->iindex + *intptr2));
811*7c478bd9Sstevel@tonic-gate if (i < 0)
812*7c478bd9Sstevel@tonic-gate ihigh = imid - 1;
813*7c478bd9Sstevel@tonic-gate else if (i > 0)
814*7c478bd9Sstevel@tonic-gate ilow = ++imid;
815*7c478bd9Sstevel@tonic-gate else {
816*7c478bd9Sstevel@tonic-gate ilow = imid + 1;
817*7c478bd9Sstevel@tonic-gate break;
818*7c478bd9Sstevel@tonic-gate }
819*7c478bd9Sstevel@tonic-gate }
820*7c478bd9Sstevel@tonic-gate /* be careful about case where searchterm is after last in this block */
821*7c478bd9Sstevel@tonic-gate imid = (ilow) ? ilow - 1 : 0;
822*7c478bd9Sstevel@tonic-gate
823*7c478bd9Sstevel@tonic-gate /* fetch the appropriate logical block if not in core */
824*7c478bd9Sstevel@tonic-gate read_logblock(invcntl, imid);
825*7c478bd9Sstevel@tonic-gate
826*7c478bd9Sstevel@tonic-gate srch_ext:
827*7c478bd9Sstevel@tonic-gate /* now find the term in this block. tricky this */
828*7c478bd9Sstevel@tonic-gate intptr = (unsigned long *)invcntl->logblk;
829*7c478bd9Sstevel@tonic-gate
830*7c478bd9Sstevel@tonic-gate ilow = 0;
831*7c478bd9Sstevel@tonic-gate ihigh = *intptr - 1;
832*7c478bd9Sstevel@tonic-gate intptr += 3;
833*7c478bd9Sstevel@tonic-gate num = 0;
834*7c478bd9Sstevel@tonic-gate while (ilow <= ihigh) {
835*7c478bd9Sstevel@tonic-gate imid = (ilow + ihigh) / 2;
836*7c478bd9Sstevel@tonic-gate entryptr = (ENTRY *)intptr + imid;
837*7c478bd9Sstevel@tonic-gate i = strncmp(searchterm, (invcntl->logblk + entryptr->offset),
838*7c478bd9Sstevel@tonic-gate (int)entryptr->size);
839*7c478bd9Sstevel@tonic-gate if (i == 0)
840*7c478bd9Sstevel@tonic-gate i = strlen(searchterm) - entryptr->size;
841*7c478bd9Sstevel@tonic-gate if (i < 0)
842*7c478bd9Sstevel@tonic-gate ihigh = imid - 1;
843*7c478bd9Sstevel@tonic-gate else if (i > 0)
844*7c478bd9Sstevel@tonic-gate ilow = ++imid;
845*7c478bd9Sstevel@tonic-gate else {
846*7c478bd9Sstevel@tonic-gate num = entryptr->post;
847*7c478bd9Sstevel@tonic-gate break;
848*7c478bd9Sstevel@tonic-gate }
849*7c478bd9Sstevel@tonic-gate }
850*7c478bd9Sstevel@tonic-gate /* be careful about case where searchterm is after last in this block */
851*7c478bd9Sstevel@tonic-gate if (imid >= *(long *)invcntl->logblk) {
852*7c478bd9Sstevel@tonic-gate invcntl->keypnt = *(long *)invcntl->logblk;
853*7c478bd9Sstevel@tonic-gate invstep(invcntl);
854*7c478bd9Sstevel@tonic-gate /* note if this happens the term could be in extended block */
855*7c478bd9Sstevel@tonic-gate if (invcntl->param.startbyte <
856*7c478bd9Sstevel@tonic-gate invcntl->numblk * invcntl->param.sizeblk)
857*7c478bd9Sstevel@tonic-gate goto srch_ext;
858*7c478bd9Sstevel@tonic-gate } else
859*7c478bd9Sstevel@tonic-gate invcntl->keypnt = imid;
860*7c478bd9Sstevel@tonic-gate return (num);
861*7c478bd9Sstevel@tonic-gate }
862*7c478bd9Sstevel@tonic-gate
863*7c478bd9Sstevel@tonic-gate #if DEBUG
864*7c478bd9Sstevel@tonic-gate
865*7c478bd9Sstevel@tonic-gate /* invdump dumps the block the term parameter is in */
866*7c478bd9Sstevel@tonic-gate void
invdump(INVCONTROL * invcntl,char * term)867*7c478bd9Sstevel@tonic-gate invdump(INVCONTROL *invcntl, char *term)
868*7c478bd9Sstevel@tonic-gate {
869*7c478bd9Sstevel@tonic-gate long i, j, n, *longptr;
870*7c478bd9Sstevel@tonic-gate ENTRY * entryptr;
871*7c478bd9Sstevel@tonic-gate char temp[512], *ptr;
872*7c478bd9Sstevel@tonic-gate
873*7c478bd9Sstevel@tonic-gate /* dump superindex if term is "-" */
874*7c478bd9Sstevel@tonic-gate if (*term == '-') {
875*7c478bd9Sstevel@tonic-gate j = atoi(term + 1);
876*7c478bd9Sstevel@tonic-gate longptr = (long *)invcntl->iindex;
877*7c478bd9Sstevel@tonic-gate n = *longptr++;
878*7c478bd9Sstevel@tonic-gate (void) printf("Superindex dump, num blocks=%ld\n", n);
879*7c478bd9Sstevel@tonic-gate longptr += j;
880*7c478bd9Sstevel@tonic-gate while ((longptr <= ((long *)invcntl->iindex) + n) &&
881*7c478bd9Sstevel@tonic-gate invbreak == 0) {
882*7c478bd9Sstevel@tonic-gate (void) printf("%2ld %6ld %s\n", j++, *longptr,
883*7c478bd9Sstevel@tonic-gate invcntl->iindex + *longptr);
884*7c478bd9Sstevel@tonic-gate longptr++;
885*7c478bd9Sstevel@tonic-gate }
886*7c478bd9Sstevel@tonic-gate return;
887*7c478bd9Sstevel@tonic-gate } else if (*term == '#') {
888*7c478bd9Sstevel@tonic-gate j = atoi(term + 1);
889*7c478bd9Sstevel@tonic-gate /* fetch the appropriate logical block */
890*7c478bd9Sstevel@tonic-gate read_logblock(invcntl, j);
891*7c478bd9Sstevel@tonic-gate } else
892*7c478bd9Sstevel@tonic-gate i = abs((int)invfind(invcntl, term));
893*7c478bd9Sstevel@tonic-gate longptr = (long *)invcntl->logblk;
894*7c478bd9Sstevel@tonic-gate n = *longptr++;
895*7c478bd9Sstevel@tonic-gate (void) printf("Entry term to invdump=%s, postings=%ld, "
896*7c478bd9Sstevel@tonic-gate "forward ptr=%ld, back ptr=%ld\n", term, i, *(longptr),
897*7c478bd9Sstevel@tonic-gate *(longptr + 1));
898*7c478bd9Sstevel@tonic-gate entryptr = (ENTRY *)(invcntl->logblk + 12);
899*7c478bd9Sstevel@tonic-gate (void) printf("%ld terms in this block, block=%ld\n", n,
900*7c478bd9Sstevel@tonic-gate invcntl->numblk);
901*7c478bd9Sstevel@tonic-gate (void) printf("\tterm\t\t\tposts\tsize\toffset\tspace\t1st word\n");
902*7c478bd9Sstevel@tonic-gate for (j = 0; j < n && invbreak == 0; j++) {
903*7c478bd9Sstevel@tonic-gate ptr = invcntl->logblk + entryptr->offset;
904*7c478bd9Sstevel@tonic-gate (void) strncpy(temp, ptr, (int)entryptr->size);
905*7c478bd9Sstevel@tonic-gate temp[entryptr->size] = '\0';
906*7c478bd9Sstevel@tonic-gate ptr += (sizeof (long) *
907*7c478bd9Sstevel@tonic-gate (long)((entryptr->size +
908*7c478bd9Sstevel@tonic-gate (sizeof (long) - 1)) / sizeof (long)));
909*7c478bd9Sstevel@tonic-gate (void) printf("%2ld %-24s\t%5ld\t%3d\t%d\t%d\t%ld\n", j, temp,
910*7c478bd9Sstevel@tonic-gate entryptr->post, entryptr->size, entryptr->offset,
911*7c478bd9Sstevel@tonic-gate entryptr->space, *(long *)ptr);
912*7c478bd9Sstevel@tonic-gate entryptr++;
913*7c478bd9Sstevel@tonic-gate }
914*7c478bd9Sstevel@tonic-gate }
915*7c478bd9Sstevel@tonic-gate #endif
916*7c478bd9Sstevel@tonic-gate
917*7c478bd9Sstevel@tonic-gate static int
boolready(void)918*7c478bd9Sstevel@tonic-gate boolready(void)
919*7c478bd9Sstevel@tonic-gate {
920*7c478bd9Sstevel@tonic-gate numitems = 0;
921*7c478bd9Sstevel@tonic-gate if (item1 != NULL)
922*7c478bd9Sstevel@tonic-gate free(item1);
923*7c478bd9Sstevel@tonic-gate setsize1 = SETINC;
924*7c478bd9Sstevel@tonic-gate if ((item1 = (POSTING *)malloc(SETINC * sizeof (POSTING))) == NULL) {
925*7c478bd9Sstevel@tonic-gate invcannotalloc(SETINC);
926*7c478bd9Sstevel@tonic-gate return (-1);
927*7c478bd9Sstevel@tonic-gate }
928*7c478bd9Sstevel@tonic-gate if (item2 != NULL)
929*7c478bd9Sstevel@tonic-gate free(item2);
930*7c478bd9Sstevel@tonic-gate setsize2 = SETINC;
931*7c478bd9Sstevel@tonic-gate if ((item2 = (POSTING *)malloc(SETINC * sizeof (POSTING))) == NULL) {
932*7c478bd9Sstevel@tonic-gate invcannotalloc(SETINC);
933*7c478bd9Sstevel@tonic-gate return (-1);
934*7c478bd9Sstevel@tonic-gate }
935*7c478bd9Sstevel@tonic-gate item = item1;
936*7c478bd9Sstevel@tonic-gate enditem = item;
937*7c478bd9Sstevel@tonic-gate return (0);
938*7c478bd9Sstevel@tonic-gate }
939*7c478bd9Sstevel@tonic-gate
940*7c478bd9Sstevel@tonic-gate void
boolclear(void)941*7c478bd9Sstevel@tonic-gate boolclear(void)
942*7c478bd9Sstevel@tonic-gate {
943*7c478bd9Sstevel@tonic-gate numitems = 0;
944*7c478bd9Sstevel@tonic-gate item = item1;
945*7c478bd9Sstevel@tonic-gate enditem = item;
946*7c478bd9Sstevel@tonic-gate }
947*7c478bd9Sstevel@tonic-gate
948*7c478bd9Sstevel@tonic-gate POSTING *
boolfile(INVCONTROL * invcntl,long * num,int bool)949*7c478bd9Sstevel@tonic-gate boolfile(INVCONTROL *invcntl, long *num, int bool)
950*7c478bd9Sstevel@tonic-gate {
951*7c478bd9Sstevel@tonic-gate ENTRY *entryptr;
952*7c478bd9Sstevel@tonic-gate FILE *file;
953*7c478bd9Sstevel@tonic-gate char *ptr;
954*7c478bd9Sstevel@tonic-gate unsigned long *ptr2;
955*7c478bd9Sstevel@tonic-gate POSTING *newitem;
956*7c478bd9Sstevel@tonic-gate POSTING posting;
957*7c478bd9Sstevel@tonic-gate unsigned u;
958*7c478bd9Sstevel@tonic-gate POSTING *newsetp, *set1p;
959*7c478bd9Sstevel@tonic-gate long newsetc, set1c, set2c;
960*7c478bd9Sstevel@tonic-gate
961*7c478bd9Sstevel@tonic-gate entryptr = (ENTRY *) (invcntl->logblk + 12) + invcntl->keypnt;
962*7c478bd9Sstevel@tonic-gate ptr = invcntl->logblk + entryptr->offset;
963*7c478bd9Sstevel@tonic-gate ptr2 = ((unsigned long *)ptr) +
964*7c478bd9Sstevel@tonic-gate (entryptr->size + (sizeof (long) - 1)) / sizeof (long);
965*7c478bd9Sstevel@tonic-gate *num = entryptr->post;
966*7c478bd9Sstevel@tonic-gate switch (bool) {
967*7c478bd9Sstevel@tonic-gate case OR:
968*7c478bd9Sstevel@tonic-gate case NOT:
969*7c478bd9Sstevel@tonic-gate if (*num == 0) {
970*7c478bd9Sstevel@tonic-gate *num = numitems;
971*7c478bd9Sstevel@tonic-gate return (item);
972*7c478bd9Sstevel@tonic-gate }
973*7c478bd9Sstevel@tonic-gate }
974*7c478bd9Sstevel@tonic-gate /* make room for the new set */
975*7c478bd9Sstevel@tonic-gate u = 0;
976*7c478bd9Sstevel@tonic-gate switch (bool) {
977*7c478bd9Sstevel@tonic-gate case AND:
978*7c478bd9Sstevel@tonic-gate case NOT:
979*7c478bd9Sstevel@tonic-gate newsetp = set1p = item;
980*7c478bd9Sstevel@tonic-gate break;
981*7c478bd9Sstevel@tonic-gate
982*7c478bd9Sstevel@tonic-gate case OR:
983*7c478bd9Sstevel@tonic-gate u = enditem - item;
984*7c478bd9Sstevel@tonic-gate /* FALLTHROUGH */
985*7c478bd9Sstevel@tonic-gate case REVERSENOT:
986*7c478bd9Sstevel@tonic-gate u += *num;
987*7c478bd9Sstevel@tonic-gate if (item == item2) {
988*7c478bd9Sstevel@tonic-gate if (u > setsize1) {
989*7c478bd9Sstevel@tonic-gate u += SETINC;
990*7c478bd9Sstevel@tonic-gate if ((item1 = (POSTING *) realloc(item1,
991*7c478bd9Sstevel@tonic-gate u * sizeof (POSTING))) == NULL) {
992*7c478bd9Sstevel@tonic-gate goto cannotalloc;
993*7c478bd9Sstevel@tonic-gate }
994*7c478bd9Sstevel@tonic-gate setsize1 = u;
995*7c478bd9Sstevel@tonic-gate }
996*7c478bd9Sstevel@tonic-gate newitem = item1;
997*7c478bd9Sstevel@tonic-gate } else {
998*7c478bd9Sstevel@tonic-gate if (u > setsize2) {
999*7c478bd9Sstevel@tonic-gate u += SETINC;
1000*7c478bd9Sstevel@tonic-gate if ((item2 = (POSTING *)realloc(item2,
1001*7c478bd9Sstevel@tonic-gate u * sizeof (POSTING))) == NULL) {
1002*7c478bd9Sstevel@tonic-gate cannotalloc:
1003*7c478bd9Sstevel@tonic-gate invcannotalloc(u * sizeof (POSTING));
1004*7c478bd9Sstevel@tonic-gate (void) boolready();
1005*7c478bd9Sstevel@tonic-gate *num = -1;
1006*7c478bd9Sstevel@tonic-gate return (NULL);
1007*7c478bd9Sstevel@tonic-gate }
1008*7c478bd9Sstevel@tonic-gate setsize2 = u;
1009*7c478bd9Sstevel@tonic-gate }
1010*7c478bd9Sstevel@tonic-gate newitem = item2;
1011*7c478bd9Sstevel@tonic-gate }
1012*7c478bd9Sstevel@tonic-gate set1p = item;
1013*7c478bd9Sstevel@tonic-gate newsetp = newitem;
1014*7c478bd9Sstevel@tonic-gate }
1015*7c478bd9Sstevel@tonic-gate file = invcntl->postfile;
1016*7c478bd9Sstevel@tonic-gate (void) fseek(file, (long)*ptr2, SEEK_SET);
1017*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1018*7c478bd9Sstevel@tonic-gate newsetc = 0;
1019*7c478bd9Sstevel@tonic-gate switch (bool) {
1020*7c478bd9Sstevel@tonic-gate case OR:
1021*7c478bd9Sstevel@tonic-gate /* while something in both sets */
1022*7c478bd9Sstevel@tonic-gate set1p = item;
1023*7c478bd9Sstevel@tonic-gate newsetp = newitem;
1024*7c478bd9Sstevel@tonic-gate for (set1c = 0, set2c = 0;
1025*7c478bd9Sstevel@tonic-gate set1c < numitems && set2c < *num; newsetc++) {
1026*7c478bd9Sstevel@tonic-gate if (set1p->lineoffset < posting.lineoffset) {
1027*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1028*7c478bd9Sstevel@tonic-gate set1c++;
1029*7c478bd9Sstevel@tonic-gate } else if (set1p->lineoffset > posting.lineoffset) {
1030*7c478bd9Sstevel@tonic-gate *newsetp++ = posting;
1031*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1032*7c478bd9Sstevel@tonic-gate set2c++;
1033*7c478bd9Sstevel@tonic-gate } else if (set1p->type < posting.type) {
1034*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1035*7c478bd9Sstevel@tonic-gate set1c++;
1036*7c478bd9Sstevel@tonic-gate } else if (set1p->type > posting.type) {
1037*7c478bd9Sstevel@tonic-gate *newsetp++ = posting;
1038*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1039*7c478bd9Sstevel@tonic-gate set2c++;
1040*7c478bd9Sstevel@tonic-gate } else { /* identical postings */
1041*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1042*7c478bd9Sstevel@tonic-gate set1c++;
1043*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1044*7c478bd9Sstevel@tonic-gate set2c++;
1045*7c478bd9Sstevel@tonic-gate }
1046*7c478bd9Sstevel@tonic-gate }
1047*7c478bd9Sstevel@tonic-gate /* find out what ran out and move the rest in */
1048*7c478bd9Sstevel@tonic-gate if (set1c < numitems) {
1049*7c478bd9Sstevel@tonic-gate newsetc += numitems - set1c;
1050*7c478bd9Sstevel@tonic-gate while (set1c++ < numitems) {
1051*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1052*7c478bd9Sstevel@tonic-gate }
1053*7c478bd9Sstevel@tonic-gate } else {
1054*7c478bd9Sstevel@tonic-gate while (set2c++ < *num) {
1055*7c478bd9Sstevel@tonic-gate *newsetp++ = posting;
1056*7c478bd9Sstevel@tonic-gate newsetc++;
1057*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1058*7c478bd9Sstevel@tonic-gate }
1059*7c478bd9Sstevel@tonic-gate }
1060*7c478bd9Sstevel@tonic-gate item = newitem;
1061*7c478bd9Sstevel@tonic-gate break; /* end of OR */
1062*7c478bd9Sstevel@tonic-gate #if 0
1063*7c478bd9Sstevel@tonic-gate case AND:
1064*7c478bd9Sstevel@tonic-gate set1c = 0;
1065*7c478bd9Sstevel@tonic-gate set2c = 0;
1066*7c478bd9Sstevel@tonic-gate while (set1c < numitems && set2c < *num) {
1067*7c478bd9Sstevel@tonic-gate if (set1p->lineoffset < posting.lineoffset) {
1068*7c478bd9Sstevel@tonic-gate set1p++;
1069*7c478bd9Sstevel@tonic-gate set1c++;
1070*7c478bd9Sstevel@tonic-gate } else if (set1p->lineoffset > posting.lineoffset) {
1071*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1072*7c478bd9Sstevel@tonic-gate set2c++;
1073*7c478bd9Sstevel@tonic-gate } else if (set1p->type < posting.type) {
1074*7c478bd9Sstevel@tonic-gate *set1p++;
1075*7c478bd9Sstevel@tonic-gate set1c++;
1076*7c478bd9Sstevel@tonic-gate } else if (set1p->type > posting.type) {
1077*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1078*7c478bd9Sstevel@tonic-gate set2c++;
1079*7c478bd9Sstevel@tonic-gate } else { /* identical postings */
1080*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1081*7c478bd9Sstevel@tonic-gate newsetc++;
1082*7c478bd9Sstevel@tonic-gate set1c++;
1083*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1084*7c478bd9Sstevel@tonic-gate set2c++;
1085*7c478bd9Sstevel@tonic-gate }
1086*7c478bd9Sstevel@tonic-gate }
1087*7c478bd9Sstevel@tonic-gate break; /* end of AND */
1088*7c478bd9Sstevel@tonic-gate
1089*7c478bd9Sstevel@tonic-gate case NOT:
1090*7c478bd9Sstevel@tonic-gate set1c = 0;
1091*7c478bd9Sstevel@tonic-gate set2c = 0;
1092*7c478bd9Sstevel@tonic-gate while (set1c < numitems && set2c < *num) {
1093*7c478bd9Sstevel@tonic-gate if (set1p->lineoffset < posting.lineoffset) {
1094*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1095*7c478bd9Sstevel@tonic-gate newsetc++;
1096*7c478bd9Sstevel@tonic-gate set1c++;
1097*7c478bd9Sstevel@tonic-gate } else if (set1p->lineoffset > posting.lineoffset) {
1098*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1099*7c478bd9Sstevel@tonic-gate set2c++;
1100*7c478bd9Sstevel@tonic-gate } else if (set1p->type < posting.type) {
1101*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1102*7c478bd9Sstevel@tonic-gate newsetc++;
1103*7c478bd9Sstevel@tonic-gate set1c++;
1104*7c478bd9Sstevel@tonic-gate } else if (set1p->type > posting.type) {
1105*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1106*7c478bd9Sstevel@tonic-gate set2c++;
1107*7c478bd9Sstevel@tonic-gate } else { /* identical postings */
1108*7c478bd9Sstevel@tonic-gate set1c++;
1109*7c478bd9Sstevel@tonic-gate set1p++;
1110*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1111*7c478bd9Sstevel@tonic-gate set2c++;
1112*7c478bd9Sstevel@tonic-gate }
1113*7c478bd9Sstevel@tonic-gate }
1114*7c478bd9Sstevel@tonic-gate newsetc += numitems - set1c;
1115*7c478bd9Sstevel@tonic-gate while (set1c++ < numitems) {
1116*7c478bd9Sstevel@tonic-gate *newsetp++ = *set1p++;
1117*7c478bd9Sstevel@tonic-gate }
1118*7c478bd9Sstevel@tonic-gate break; /* end of NOT */
1119*7c478bd9Sstevel@tonic-gate
1120*7c478bd9Sstevel@tonic-gate case REVERSENOT: /* core NOT incoming set */
1121*7c478bd9Sstevel@tonic-gate set1c = 0;
1122*7c478bd9Sstevel@tonic-gate set2c = 0;
1123*7c478bd9Sstevel@tonic-gate while (set1c < numitems && set2c < *num) {
1124*7c478bd9Sstevel@tonic-gate if (set1p->lineoffset < posting.lineoffset) {
1125*7c478bd9Sstevel@tonic-gate set1p++;
1126*7c478bd9Sstevel@tonic-gate set1c++;
1127*7c478bd9Sstevel@tonic-gate } else if (set1p->lineoffset > posting.lineoffset) {
1128*7c478bd9Sstevel@tonic-gate *newsetp++ = posting;
1129*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1130*7c478bd9Sstevel@tonic-gate set2c++;
1131*7c478bd9Sstevel@tonic-gate } else if (set1p->type < posting.type) {
1132*7c478bd9Sstevel@tonic-gate set1p++;
1133*7c478bd9Sstevel@tonic-gate set1c++;
1134*7c478bd9Sstevel@tonic-gate } else if (set1p->type > posting.type) {
1135*7c478bd9Sstevel@tonic-gate *newsetp++ = posting;
1136*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1137*7c478bd9Sstevel@tonic-gate set2c++;
1138*7c478bd9Sstevel@tonic-gate } else { /* identical postings */
1139*7c478bd9Sstevel@tonic-gate set1c++;
1140*7c478bd9Sstevel@tonic-gate set1p++;
1141*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1142*7c478bd9Sstevel@tonic-gate set2c++;
1143*7c478bd9Sstevel@tonic-gate }
1144*7c478bd9Sstevel@tonic-gate }
1145*7c478bd9Sstevel@tonic-gate while (set2c++ < *num) {
1146*7c478bd9Sstevel@tonic-gate *newsetp++ = posting;
1147*7c478bd9Sstevel@tonic-gate newsetc++;
1148*7c478bd9Sstevel@tonic-gate read_next_posting(invcntl, &posting);
1149*7c478bd9Sstevel@tonic-gate }
1150*7c478bd9Sstevel@tonic-gate item = newitem;
1151*7c478bd9Sstevel@tonic-gate break; /* end of REVERSENOT */
1152*7c478bd9Sstevel@tonic-gate #endif
1153*7c478bd9Sstevel@tonic-gate }
1154*7c478bd9Sstevel@tonic-gate numitems = newsetc;
1155*7c478bd9Sstevel@tonic-gate *num = newsetc;
1156*7c478bd9Sstevel@tonic-gate enditem = (POSTING *)newsetp;
1157*7c478bd9Sstevel@tonic-gate return ((POSTING *)item);
1158*7c478bd9Sstevel@tonic-gate }
1159*7c478bd9Sstevel@tonic-gate
1160*7c478bd9Sstevel@tonic-gate #if 0
1161*7c478bd9Sstevel@tonic-gate POSTING *
1162*7c478bd9Sstevel@tonic-gate boolsave(int clear)
1163*7c478bd9Sstevel@tonic-gate {
1164*7c478bd9Sstevel@tonic-gate int i;
1165*7c478bd9Sstevel@tonic-gate POSTING *ptr;
1166*7c478bd9Sstevel@tonic-gate POSTING *oldstuff, *newstuff;
1167*7c478bd9Sstevel@tonic-gate
1168*7c478bd9Sstevel@tonic-gate if (numitems == 0) {
1169*7c478bd9Sstevel@tonic-gate if (clear)
1170*7c478bd9Sstevel@tonic-gate boolclear();
1171*7c478bd9Sstevel@tonic-gate return (NULL);
1172*7c478bd9Sstevel@tonic-gate }
1173*7c478bd9Sstevel@tonic-gate /*
1174*7c478bd9Sstevel@tonic-gate * if clear then give them what we have and use (void)
1175*7c478bd9Sstevel@tonic-gate * boolready to realloc
1176*7c478bd9Sstevel@tonic-gate */
1177*7c478bd9Sstevel@tonic-gate if (clear) {
1178*7c478bd9Sstevel@tonic-gate ptr = item;
1179*7c478bd9Sstevel@tonic-gate /* free up the space we didn't give them */
1180*7c478bd9Sstevel@tonic-gate if (item == item1)
1181*7c478bd9Sstevel@tonic-gate item1 = NULL;
1182*7c478bd9Sstevel@tonic-gate else
1183*7c478bd9Sstevel@tonic-gate item2 = NULL;
1184*7c478bd9Sstevel@tonic-gate (void) boolready();
1185*7c478bd9Sstevel@tonic-gate return (ptr);
1186*7c478bd9Sstevel@tonic-gate }
1187*7c478bd9Sstevel@tonic-gate i = (enditem - item) * sizeof (POSTING) + 100;
1188*7c478bd9Sstevel@tonic-gate if ((ptr = (POSTING *)malloc(i))r == NULL) {
1189*7c478bd9Sstevel@tonic-gate invcannotalloc(i);
1190*7c478bd9Sstevel@tonic-gate return (ptr);
1191*7c478bd9Sstevel@tonic-gate }
1192*7c478bd9Sstevel@tonic-gate /* move present set into place */
1193*7c478bd9Sstevel@tonic-gate oldstuff = item;
1194*7c478bd9Sstevel@tonic-gate newstuff = ptr;
1195*7c478bd9Sstevel@tonic-gate while (oldstuff < enditem)
1196*7c478bd9Sstevel@tonic-gate *newstuff++ = *oldstuff++;
1197*7c478bd9Sstevel@tonic-gate return (ptr);
1198*7c478bd9Sstevel@tonic-gate }
1199*7c478bd9Sstevel@tonic-gate #endif
1200*7c478bd9Sstevel@tonic-gate
1201*7c478bd9Sstevel@tonic-gate static void
invcannotalloc(size_t n)1202*7c478bd9Sstevel@tonic-gate invcannotalloc(size_t n)
1203*7c478bd9Sstevel@tonic-gate {
1204*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot allocate %u bytes\n", argv0, n);
1205*7c478bd9Sstevel@tonic-gate }
1206*7c478bd9Sstevel@tonic-gate
1207*7c478bd9Sstevel@tonic-gate static void
invcannotopen(char * file)1208*7c478bd9Sstevel@tonic-gate invcannotopen(char *file)
1209*7c478bd9Sstevel@tonic-gate {
1210*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: cannot open file %s\n", argv0, file);
1211*7c478bd9Sstevel@tonic-gate }
1212*7c478bd9Sstevel@tonic-gate
1213*7c478bd9Sstevel@tonic-gate static void
invcannotwrite(char * file)1214*7c478bd9Sstevel@tonic-gate invcannotwrite(char *file)
1215*7c478bd9Sstevel@tonic-gate {
1216*7c478bd9Sstevel@tonic-gate (void) perror(argv0); /* must be first to preserve errno */
1217*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: write to file %s failed\n", argv0, file);
1218*7c478bd9Sstevel@tonic-gate }
1219