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