1*11a8fa6cSceastha /*
2*11a8fa6cSceastha * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3*11a8fa6cSceastha * Use is subject to license terms.
4*11a8fa6cSceastha */
5*11a8fa6cSceastha
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
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 */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include <stdio.h>
167c478bd9Sstevel@tonic-gate #include <locale.h>
177c478bd9Sstevel@tonic-gate #include <assert.h>
187c478bd9Sstevel@tonic-gate
19*11a8fa6cSceastha extern void err();
20*11a8fa6cSceastha extern int newkeys();
21*11a8fa6cSceastha extern int recopy();
22*11a8fa6cSceastha extern void whash();
23*11a8fa6cSceastha
24*11a8fa6cSceastha int
main(int argc,char * argv[])25*11a8fa6cSceastha main(int argc, char *argv[])
267c478bd9Sstevel@tonic-gate {
27*11a8fa6cSceastha /*
28*11a8fa6cSceastha * Make inverted file indexes. Reads a stream from mkey which
297c478bd9Sstevel@tonic-gate * gives record pointer items and keys. Generates set of files
307c478bd9Sstevel@tonic-gate * a. NHASH pointers to file b.
317c478bd9Sstevel@tonic-gate * b. lists of record numbers.
327c478bd9Sstevel@tonic-gate * c. record pointer items.
337c478bd9Sstevel@tonic-gate *
347c478bd9Sstevel@tonic-gate * these files are named xxx.ia, xxx.ib, xxx.ic;
357c478bd9Sstevel@tonic-gate * where xxx is taken from arg1.
367c478bd9Sstevel@tonic-gate * If the files exist they are updated.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate FILE *fa, *fb, *fc, *fta, *ftb, *ftc;
407c478bd9Sstevel@tonic-gate FILE *fd = NULL;
417c478bd9Sstevel@tonic-gate int nhash = 256;
427c478bd9Sstevel@tonic-gate int appflg = 1;
437c478bd9Sstevel@tonic-gate int keepkey = 0, pipein = 0;
447c478bd9Sstevel@tonic-gate char nma[100], nmb[100], nmc[100], com[100], nmd[100];
457c478bd9Sstevel@tonic-gate char tmpa[20], tmpb[20], tmpc[20];
467c478bd9Sstevel@tonic-gate char *remove = NULL;
477c478bd9Sstevel@tonic-gate int chatty = 0, docs, hashes, fp[2], fr, fw, pfork, pwait, status;
487c478bd9Sstevel@tonic-gate int i, j, k;
497c478bd9Sstevel@tonic-gate long keys;
507c478bd9Sstevel@tonic-gate int iflong = 0;
517c478bd9Sstevel@tonic-gate char *sortdir;
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
567c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
577c478bd9Sstevel@tonic-gate #endif
587c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate sortdir = (access("/crp/tmp", 06) == 0) ? "/crp/tmp" : "/usr/tmp";
61*11a8fa6cSceastha while (argc > 1 && argv[1][0] == '-') {
62*11a8fa6cSceastha switch (argv[1][1]) {
637c478bd9Sstevel@tonic-gate case 'h': /* size of hash table */
647c478bd9Sstevel@tonic-gate nhash = atoi(argv[1]+2);
657c478bd9Sstevel@tonic-gate break;
667c478bd9Sstevel@tonic-gate case 'n': /* new, don't append */
677c478bd9Sstevel@tonic-gate appflg = 0;
687c478bd9Sstevel@tonic-gate break;
697c478bd9Sstevel@tonic-gate case 'a': /* append to old file */
707c478bd9Sstevel@tonic-gate appflg = 1;
717c478bd9Sstevel@tonic-gate break;
727c478bd9Sstevel@tonic-gate case 'v': /* verbose output */
737c478bd9Sstevel@tonic-gate chatty = 1;
747c478bd9Sstevel@tonic-gate break;
757c478bd9Sstevel@tonic-gate case 'd': /* keep keys on file .id for check on searching */
767c478bd9Sstevel@tonic-gate keepkey = 1;
777c478bd9Sstevel@tonic-gate break;
787c478bd9Sstevel@tonic-gate case 'p': /* pipe into sort (saves space, costs time) */
797c478bd9Sstevel@tonic-gate pipein = 1;
807c478bd9Sstevel@tonic-gate break;
817c478bd9Sstevel@tonic-gate case 'i': /* input is on file, not stdin */
827c478bd9Sstevel@tonic-gate close(0);
837c478bd9Sstevel@tonic-gate if (open(argv[2], 0) != 0)
847c478bd9Sstevel@tonic-gate err(gettext("Can't read input %s"), argv[2]);
857c478bd9Sstevel@tonic-gate if (argv[1][2] == 'u') /* unlink */
867c478bd9Sstevel@tonic-gate remove = argv[2];
877c478bd9Sstevel@tonic-gate argc--;
887c478bd9Sstevel@tonic-gate argv++;
897c478bd9Sstevel@tonic-gate break;
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate argc--;
927c478bd9Sstevel@tonic-gate argv++;
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate strcpy(nma, argc >= 2 ? argv[1] : "Index");
957c478bd9Sstevel@tonic-gate strcpy(nmb, nma);
967c478bd9Sstevel@tonic-gate strcpy(nmc, nma);
977c478bd9Sstevel@tonic-gate strcpy(nmd, nma);
987c478bd9Sstevel@tonic-gate strcat(nma, ".ia");
997c478bd9Sstevel@tonic-gate strcat(nmb, ".ib");
1007c478bd9Sstevel@tonic-gate strcat(nmc, ".ic");
1017c478bd9Sstevel@tonic-gate strcat(nmd, ".id");
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate sprintf(tmpa, "junk%di", getpid());
104*11a8fa6cSceastha if (pipein) {
1057c478bd9Sstevel@tonic-gate sprintf(com, "/usr/bin/sort -T %s -o %s", sortdir, tmpa);
1067c478bd9Sstevel@tonic-gate fta = popen(com, "w");
107*11a8fa6cSceastha } else { /* use tmp file */
1087c478bd9Sstevel@tonic-gate fta = fopen(tmpa, "w");
1097c478bd9Sstevel@tonic-gate assert(fta != NULL);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate fb = 0;
112*11a8fa6cSceastha if (appflg) {
113*11a8fa6cSceastha if (fb = fopen(nmb, "r")) {
1147c478bd9Sstevel@tonic-gate sprintf(tmpb, "junk%dj", getpid());
1157c478bd9Sstevel@tonic-gate ftb = fopen(tmpb, "w");
1167c478bd9Sstevel@tonic-gate if (ftb == NULL)
1177c478bd9Sstevel@tonic-gate err(gettext("Can't get scratch file %s"), tmpb);
1187c478bd9Sstevel@tonic-gate nhash = recopy(ftb, fb, fopen(nma, "r"));
1197c478bd9Sstevel@tonic-gate fclose(ftb);
120*11a8fa6cSceastha } else
1217c478bd9Sstevel@tonic-gate appflg = 0;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate fc = fopen(nmc, appflg ? "a" : "w");
1247c478bd9Sstevel@tonic-gate if (keepkey)
1257c478bd9Sstevel@tonic-gate fd = keepkey ? fopen(nmd, "w") : 0;
1267c478bd9Sstevel@tonic-gate docs = newkeys(fta, stdin, fc, nhash, fd, &iflong);
1277c478bd9Sstevel@tonic-gate fclose(stdin);
1287c478bd9Sstevel@tonic-gate if (remove != NULL)
1297c478bd9Sstevel@tonic-gate unlink(remove);
1307c478bd9Sstevel@tonic-gate fclose(fta);
131*11a8fa6cSceastha if (pipein) {
1327c478bd9Sstevel@tonic-gate pclose(fta);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate else
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate sprintf(com, "sort -T %s %s -o %s", sortdir, tmpa, tmpa);
1377c478bd9Sstevel@tonic-gate system(com);
1387c478bd9Sstevel@tonic-gate }
139*11a8fa6cSceastha if (appflg) {
1407c478bd9Sstevel@tonic-gate sprintf(tmpc, "junk%dk", getpid());
1417c478bd9Sstevel@tonic-gate sprintf(com, "mv %s %s", tmpa, tmpc);
1427c478bd9Sstevel@tonic-gate system(com);
1437c478bd9Sstevel@tonic-gate sprintf(com, "sort -T %s -m %s %s -o %s", sortdir,
1447c478bd9Sstevel@tonic-gate tmpb, tmpc, tmpa);
1457c478bd9Sstevel@tonic-gate system(com);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate fta = fopen(tmpa, "r");
1487c478bd9Sstevel@tonic-gate fa = fopen(nma, "w");
1497c478bd9Sstevel@tonic-gate fb = fopen(nmb, "w");
1507c478bd9Sstevel@tonic-gate whash(fta, fa, fb, nhash, iflong, &keys, &hashes);
1517c478bd9Sstevel@tonic-gate fclose(fta);
1527c478bd9Sstevel@tonic-gate #ifndef D1
1537c478bd9Sstevel@tonic-gate unlink(tmpa);
1547c478bd9Sstevel@tonic-gate #endif
155*11a8fa6cSceastha if (appflg) {
1567c478bd9Sstevel@tonic-gate unlink(tmpb);
1577c478bd9Sstevel@tonic-gate unlink(tmpc);
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate if (chatty)
1607c478bd9Sstevel@tonic-gate printf(gettext("%ld key occurrences, %d hashes, %d docs\n"),
1617c478bd9Sstevel@tonic-gate keys, hashes, docs);
1627c478bd9Sstevel@tonic-gate
163*11a8fa6cSceastha return (0);
1647c478bd9Sstevel@tonic-gate }
165