xref: /titanic_53/usr/src/cmd/refer/inv1.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate #include <stdio.h>
19*7c478bd9Sstevel@tonic-gate #include <locale.h>
20*7c478bd9Sstevel@tonic-gate #include <assert.h>
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate main(argc, argv)
23*7c478bd9Sstevel@tonic-gate char *argv[];
24*7c478bd9Sstevel@tonic-gate {
25*7c478bd9Sstevel@tonic-gate 	/* Make inverted file indexes.  Reads a stream from mkey which
26*7c478bd9Sstevel@tonic-gate 	 * gives record pointer items and keys.  Generates set of files
27*7c478bd9Sstevel@tonic-gate 	 *	a. NHASH pointers to file b.
28*7c478bd9Sstevel@tonic-gate 	 *	b. lists of record numbers.
29*7c478bd9Sstevel@tonic-gate 	 *	c. record pointer items.
30*7c478bd9Sstevel@tonic-gate 	 *
31*7c478bd9Sstevel@tonic-gate 	 *  these files are named xxx.ia, xxx.ib, xxx.ic;
32*7c478bd9Sstevel@tonic-gate 	 *  where xxx is taken from arg1.
33*7c478bd9Sstevel@tonic-gate 	 *  If the files exist they are updated.
34*7c478bd9Sstevel@tonic-gate 	 */
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate 	FILE *fa, *fb, *fc, *fta, *ftb, *ftc;
37*7c478bd9Sstevel@tonic-gate 	FILE *fd = NULL;
38*7c478bd9Sstevel@tonic-gate 	int nhash = 256;
39*7c478bd9Sstevel@tonic-gate 	int appflg = 1;
40*7c478bd9Sstevel@tonic-gate 	int keepkey = 0, pipein = 0;
41*7c478bd9Sstevel@tonic-gate 	char nma[100], nmb[100], nmc[100], com[100], nmd[100];
42*7c478bd9Sstevel@tonic-gate 	char tmpa[20], tmpb[20], tmpc[20];
43*7c478bd9Sstevel@tonic-gate 	char *remove = NULL;
44*7c478bd9Sstevel@tonic-gate 	int chatty = 0, docs, hashes, fp[2], fr, fw, pfork, pwait, status;
45*7c478bd9Sstevel@tonic-gate 	int i,j,k;
46*7c478bd9Sstevel@tonic-gate 	long keys;
47*7c478bd9Sstevel@tonic-gate 	int iflong =0;
48*7c478bd9Sstevel@tonic-gate 	char *sortdir;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
53*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	sortdir = (access("/crp/tmp", 06)==0) ? "/crp/tmp" : "/usr/tmp";
58*7c478bd9Sstevel@tonic-gate 	while (argc>1 && argv[1][0] == '-')
59*7c478bd9Sstevel@tonic-gate 	{
60*7c478bd9Sstevel@tonic-gate 		switch(argv[1][1])
61*7c478bd9Sstevel@tonic-gate 		{
62*7c478bd9Sstevel@tonic-gate 		case 'h': /* size of hash table */
63*7c478bd9Sstevel@tonic-gate 			nhash = atoi (argv[1]+2);
64*7c478bd9Sstevel@tonic-gate 			break;
65*7c478bd9Sstevel@tonic-gate 		case 'n': /* new, don't append */
66*7c478bd9Sstevel@tonic-gate 			appflg=0;
67*7c478bd9Sstevel@tonic-gate 			break;
68*7c478bd9Sstevel@tonic-gate 		case 'a': /* append to old file */
69*7c478bd9Sstevel@tonic-gate 			appflg=1;
70*7c478bd9Sstevel@tonic-gate 			break;
71*7c478bd9Sstevel@tonic-gate 		case 'v': /* verbose output */
72*7c478bd9Sstevel@tonic-gate 			chatty=1;
73*7c478bd9Sstevel@tonic-gate 			break;
74*7c478bd9Sstevel@tonic-gate 		case 'd': /* keep keys on file .id for check on searching */
75*7c478bd9Sstevel@tonic-gate 			keepkey=1;
76*7c478bd9Sstevel@tonic-gate 			break;
77*7c478bd9Sstevel@tonic-gate 		case 'p': /* pipe into sort (saves space, costs time)*/
78*7c478bd9Sstevel@tonic-gate 			pipein = 1;
79*7c478bd9Sstevel@tonic-gate 			break;
80*7c478bd9Sstevel@tonic-gate 		case 'i': /* input is on file, not stdin */
81*7c478bd9Sstevel@tonic-gate 			close(0);
82*7c478bd9Sstevel@tonic-gate 			if (open(argv[2], 0) != 0)
83*7c478bd9Sstevel@tonic-gate 				err(gettext("Can't read input %s"), argv[2]);
84*7c478bd9Sstevel@tonic-gate 			if (argv[1][2]=='u') /* unlink */
85*7c478bd9Sstevel@tonic-gate 				remove = argv[2];
86*7c478bd9Sstevel@tonic-gate 			argc--;
87*7c478bd9Sstevel@tonic-gate 			argv++;
88*7c478bd9Sstevel@tonic-gate 			break;
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 		argc--;
91*7c478bd9Sstevel@tonic-gate 		argv++;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 	strcpy (nma, argc >= 2 ? argv[1] : "Index");
94*7c478bd9Sstevel@tonic-gate 	strcpy (nmb, nma);
95*7c478bd9Sstevel@tonic-gate 	strcpy (nmc, nma);
96*7c478bd9Sstevel@tonic-gate 	strcpy (nmd, nma);
97*7c478bd9Sstevel@tonic-gate 	strcat (nma, ".ia");
98*7c478bd9Sstevel@tonic-gate 	strcat (nmb, ".ib");
99*7c478bd9Sstevel@tonic-gate 	strcat (nmc, ".ic");
100*7c478bd9Sstevel@tonic-gate 	strcat (nmd, ".id");
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	sprintf(tmpa, "junk%di", getpid());
103*7c478bd9Sstevel@tonic-gate 	if (pipein)
104*7c478bd9Sstevel@tonic-gate 	{
105*7c478bd9Sstevel@tonic-gate 		sprintf(com, "/usr/bin/sort -T %s -o %s", sortdir, tmpa);
106*7c478bd9Sstevel@tonic-gate 		fta = popen(com, "w");
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 	else /* use tmp file */
109*7c478bd9Sstevel@tonic-gate 	{
110*7c478bd9Sstevel@tonic-gate 		fta = fopen(tmpa, "w");
111*7c478bd9Sstevel@tonic-gate 		assert (fta != NULL);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	fb = 0;
114*7c478bd9Sstevel@tonic-gate 	if (appflg )
115*7c478bd9Sstevel@tonic-gate 	{
116*7c478bd9Sstevel@tonic-gate 		if (fb = fopen(nmb, "r"))
117*7c478bd9Sstevel@tonic-gate 		{
118*7c478bd9Sstevel@tonic-gate 			sprintf(tmpb, "junk%dj", getpid());
119*7c478bd9Sstevel@tonic-gate 			ftb = fopen(tmpb, "w");
120*7c478bd9Sstevel@tonic-gate 			if (ftb==NULL)
121*7c478bd9Sstevel@tonic-gate 				err(gettext("Can't get scratch file %s"),tmpb);
122*7c478bd9Sstevel@tonic-gate 			nhash = recopy(ftb, fb, fopen(nma, "r"));
123*7c478bd9Sstevel@tonic-gate 			fclose(ftb);
124*7c478bd9Sstevel@tonic-gate 		}
125*7c478bd9Sstevel@tonic-gate 		else
126*7c478bd9Sstevel@tonic-gate 			appflg=0;
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 	fc = fopen(nmc,  appflg ? "a" : "w");
129*7c478bd9Sstevel@tonic-gate 	if (keepkey)
130*7c478bd9Sstevel@tonic-gate 		fd = keepkey ? fopen(nmd, "w") : 0;
131*7c478bd9Sstevel@tonic-gate 	docs = newkeys(fta, stdin, fc, nhash, fd, &iflong);
132*7c478bd9Sstevel@tonic-gate 	fclose(stdin);
133*7c478bd9Sstevel@tonic-gate 	if (remove != NULL)
134*7c478bd9Sstevel@tonic-gate 		unlink(remove);
135*7c478bd9Sstevel@tonic-gate 	fclose(fta);
136*7c478bd9Sstevel@tonic-gate 	if (pipein)
137*7c478bd9Sstevel@tonic-gate 	{
138*7c478bd9Sstevel@tonic-gate 		pclose(fta);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	else
141*7c478bd9Sstevel@tonic-gate 	{
142*7c478bd9Sstevel@tonic-gate 		sprintf(com, "sort -T %s %s -o %s", sortdir, tmpa, tmpa);
143*7c478bd9Sstevel@tonic-gate 		system(com);
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	if (appflg)
146*7c478bd9Sstevel@tonic-gate 	{
147*7c478bd9Sstevel@tonic-gate 		sprintf(tmpc, "junk%dk", getpid());
148*7c478bd9Sstevel@tonic-gate 		sprintf(com, "mv %s %s", tmpa, tmpc);
149*7c478bd9Sstevel@tonic-gate 		system(com);
150*7c478bd9Sstevel@tonic-gate 		sprintf(com, "sort -T %s  -m %s %s -o %s", sortdir,
151*7c478bd9Sstevel@tonic-gate 		tmpb, tmpc, tmpa);
152*7c478bd9Sstevel@tonic-gate 		system(com);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 	fta = fopen(tmpa, "r");
155*7c478bd9Sstevel@tonic-gate 	fa = fopen(nma, "w");
156*7c478bd9Sstevel@tonic-gate 	fb = fopen(nmb, "w");
157*7c478bd9Sstevel@tonic-gate 	whash(fta, fa, fb, nhash, iflong, &keys, &hashes);
158*7c478bd9Sstevel@tonic-gate 	fclose(fta);
159*7c478bd9Sstevel@tonic-gate # ifndef D1
160*7c478bd9Sstevel@tonic-gate 	unlink(tmpa);
161*7c478bd9Sstevel@tonic-gate # endif
162*7c478bd9Sstevel@tonic-gate 	if (appflg)
163*7c478bd9Sstevel@tonic-gate 	{
164*7c478bd9Sstevel@tonic-gate 		unlink(tmpb);
165*7c478bd9Sstevel@tonic-gate 		unlink(tmpc);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 	if (chatty)
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		printf (gettext("%ld key occurrences,  %d hashes, %d docs\n"),
170*7c478bd9Sstevel@tonic-gate 		keys, hashes, docs);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	exit(0);
173*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
174*7c478bd9Sstevel@tonic-gate }
175