xref: /illumos-gate/usr/src/cmd/refer/inv1.c (revision 11a8fa6c)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
15*11a8fa6cSceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <locale.h>
197c478bd9Sstevel@tonic-gate #include <assert.h>
21*11a8fa6cSceastha extern void err();
22*11a8fa6cSceastha extern int newkeys();
23*11a8fa6cSceastha extern int recopy();
24*11a8fa6cSceastha extern void whash();
26*11a8fa6cSceastha int
main(int argc,char * argv[])27*11a8fa6cSceastha main(int argc, char *argv[])
287c478bd9Sstevel@tonic-gate {
29*11a8fa6cSceastha 	/*
30*11a8fa6cSceastha 	 * Make inverted file indexes.  Reads a stream from mkey which
317c478bd9Sstevel@tonic-gate 	 * gives record pointer items and keys.  Generates set of files
327c478bd9Sstevel@tonic-gate 	 *	a. NHASH pointers to file b.
337c478bd9Sstevel@tonic-gate 	 *	b. lists of record numbers.
347c478bd9Sstevel@tonic-gate 	 *	c. record pointer items.
357c478bd9Sstevel@tonic-gate 	 *
367c478bd9Sstevel@tonic-gate 	 *  these files are named xxx.ia, xxx.ib, xxx.ic;
377c478bd9Sstevel@tonic-gate 	 *  where xxx is taken from arg1.
387c478bd9Sstevel@tonic-gate 	 *  If the files exist they are updated.
397c478bd9Sstevel@tonic-gate 	 */
417c478bd9Sstevel@tonic-gate 	FILE *fa, *fb, *fc, *fta, *ftb, *ftc;
427c478bd9Sstevel@tonic-gate 	FILE *fd = NULL;
437c478bd9Sstevel@tonic-gate 	int nhash = 256;
447c478bd9Sstevel@tonic-gate 	int appflg = 1;
457c478bd9Sstevel@tonic-gate 	int keepkey = 0, pipein = 0;
467c478bd9Sstevel@tonic-gate 	char nma[100], nmb[100], nmc[100], com[100], nmd[100];
477c478bd9Sstevel@tonic-gate 	char tmpa[20], tmpb[20], tmpc[20];
487c478bd9Sstevel@tonic-gate 	char *remove = NULL;
497c478bd9Sstevel@tonic-gate 	int chatty = 0, docs, hashes, fp[2], fr, fw, pfork, pwait, status;
50*11a8fa6cSceastha 	int i, j, k;
517c478bd9Sstevel@tonic-gate 	long keys;
52*11a8fa6cSceastha 	int iflong = 0;
537c478bd9Sstevel@tonic-gate 	char *sortdir;
557c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
577c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
58*11a8fa6cSceastha #define	TEXT_DOMAIN "SYS_TEST"
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
62*11a8fa6cSceastha 	sortdir = (access("/crp/tmp", 06) == 0) ? "/crp/tmp" : "/usr/tmp";
63*11a8fa6cSceastha 	while (argc > 1 && argv[1][0] == '-') {
64*11a8fa6cSceastha 		switch (argv[1][1]) {
657c478bd9Sstevel@tonic-gate 		case 'h': /* size of hash table */
66*11a8fa6cSceastha 			nhash = atoi(argv[1]+2);
677c478bd9Sstevel@tonic-gate 			break;
687c478bd9Sstevel@tonic-gate 		case 'n': /* new, don't append */
69*11a8fa6cSceastha 			appflg = 0;
707c478bd9Sstevel@tonic-gate 			break;
717c478bd9Sstevel@tonic-gate 		case 'a': /* append to old file */
72*11a8fa6cSceastha 			appflg = 1;
737c478bd9Sstevel@tonic-gate 			break;
747c478bd9Sstevel@tonic-gate 		case 'v': /* verbose output */
75*11a8fa6cSceastha 			chatty = 1;
767c478bd9Sstevel@tonic-gate 			break;
777c478bd9Sstevel@tonic-gate 		case 'd': /* keep keys on file .id for check on searching */
78*11a8fa6cSceastha 			keepkey = 1;
797c478bd9Sstevel@tonic-gate 			break;
80*11a8fa6cSceastha 		case 'p': /* pipe into sort (saves space, costs time) */
81*11a8fa6cSceastha 			pipein = 1;
827c478bd9Sstevel@tonic-gate 			break;
837c478bd9Sstevel@tonic-gate 		case 'i': /* input is on file, not stdin */
847c478bd9Sstevel@tonic-gate 			close(0);
857c478bd9Sstevel@tonic-gate 			if (open(argv[2], 0) != 0)
867c478bd9Sstevel@tonic-gate 				err(gettext("Can't read input %s"), argv[2]);
87*11a8fa6cSceastha 			if (argv[1][2] == 'u') /* unlink */
887c478bd9Sstevel@tonic-gate 				remove = argv[2];
89*11a8fa6cSceastha 			argc--;
907c478bd9Sstevel@tonic-gate 			argv++;
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 		argc--;
947c478bd9Sstevel@tonic-gate 		argv++;
957c478bd9Sstevel@tonic-gate 	}
96*11a8fa6cSceastha 	strcpy(nma, argc >= 2 ? argv[1] : "Index");
97*11a8fa6cSceastha 	strcpy(nmb, nma);
98*11a8fa6cSceastha 	strcpy(nmc, nma);
99*11a8fa6cSceastha 	strcpy(nmd, nma);
100*11a8fa6cSceastha 	strcat(nma, ".ia");
101*11a8fa6cSceastha 	strcat(nmb, ".ib");
102*11a8fa6cSceastha 	strcat(nmc, ".ic");
103*11a8fa6cSceastha 	strcat(nmd, ".id");
1057c478bd9Sstevel@tonic-gate 	sprintf(tmpa, "junk%di", getpid());
106*11a8fa6cSceastha 	if (pipein) {
1077c478bd9Sstevel@tonic-gate 		sprintf(com, "/usr/bin/sort -T %s -o %s", sortdir, tmpa);
1087c478bd9Sstevel@tonic-gate 		fta = popen(com, "w");
109*11a8fa6cSceastha 	} else {	/* use tmp file */
1107c478bd9Sstevel@tonic-gate 		fta = fopen(tmpa, "w");
111*11a8fa6cSceastha 		assert(fta != NULL);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	fb = 0;
114*11a8fa6cSceastha 	if (appflg) {
115*11a8fa6cSceastha 		if (fb = fopen(nmb, "r")) {
1167c478bd9Sstevel@tonic-gate 			sprintf(tmpb, "junk%dj", getpid());
1177c478bd9Sstevel@tonic-gate 			ftb = fopen(tmpb, "w");
118*11a8fa6cSceastha 			if (ftb == NULL)
119*11a8fa6cSceastha 				err(gettext("Can't get scratch file %s"), tmpb);
1207c478bd9Sstevel@tonic-gate 			nhash = recopy(ftb, fb, fopen(nma, "r"));
1217c478bd9Sstevel@tonic-gate 			fclose(ftb);
122*11a8fa6cSceastha 		} else
123*11a8fa6cSceastha 			appflg = 0;
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	fc = fopen(nmc,  appflg ? "a" : "w");
1267c478bd9Sstevel@tonic-gate 	if (keepkey)
1277c478bd9Sstevel@tonic-gate 		fd = keepkey ? fopen(nmd, "w") : 0;
1287c478bd9Sstevel@tonic-gate 	docs = newkeys(fta, stdin, fc, nhash, fd, &iflong);
1297c478bd9Sstevel@tonic-gate 	fclose(stdin);
1307c478bd9Sstevel@tonic-gate 	if (remove != NULL)
1317c478bd9Sstevel@tonic-gate 		unlink(remove);
1327c478bd9Sstevel@tonic-gate 	fclose(fta);
133*11a8fa6cSceastha 	if (pipein) {
1347c478bd9Sstevel@tonic-gate 		pclose(fta);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	else
1377c478bd9Sstevel@tonic-gate 	{
1387c478bd9Sstevel@tonic-gate 		sprintf(com, "sort -T %s %s -o %s", sortdir, tmpa, tmpa);
1397c478bd9Sstevel@tonic-gate 		system(com);
1407c478bd9Sstevel@tonic-gate 	}
141*11a8fa6cSceastha 	if (appflg) {
1427c478bd9Sstevel@tonic-gate 		sprintf(tmpc, "junk%dk", getpid());
1437c478bd9Sstevel@tonic-gate 		sprintf(com, "mv %s %s", tmpa, tmpc);
1447c478bd9Sstevel@tonic-gate 		system(com);
1457c478bd9Sstevel@tonic-gate 		sprintf(com, "sort -T %s  -m %s %s -o %s", sortdir,
146*11a8fa6cSceastha 		    tmpb, tmpc, tmpa);
1477c478bd9Sstevel@tonic-gate 		system(com);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	fta = fopen(tmpa, "r");
1507c478bd9Sstevel@tonic-gate 	fa = fopen(nma, "w");
1517c478bd9Sstevel@tonic-gate 	fb = fopen(nmb, "w");
1527c478bd9Sstevel@tonic-gate 	whash(fta, fa, fb, nhash, iflong, &keys, &hashes);
1537c478bd9Sstevel@tonic-gate 	fclose(fta);
154*11a8fa6cSceastha #ifndef D1
1557c478bd9Sstevel@tonic-gate 	unlink(tmpa);
156*11a8fa6cSceastha #endif
157*11a8fa6cSceastha 	if (appflg) {
1587c478bd9Sstevel@tonic-gate 		unlink(tmpb);
1597c478bd9Sstevel@tonic-gate 		unlink(tmpc);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 	if (chatty)
162*11a8fa6cSceastha 		printf(gettext("%ld key occurrences,  %d hashes, %d docs\n"),
163*11a8fa6cSceastha 		    keys, hashes, docs);
165*11a8fa6cSceastha 	return (0);
1667c478bd9Sstevel@tonic-gate }