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 #pragma ident "%Z%%M% %I% %E% SMI" 167c478bd9Sstevel@tonic-gate 177c478bd9Sstevel@tonic-gate #include <stdio.h> 187c478bd9Sstevel@tonic-gate #include <locale.h> 197c478bd9Sstevel@tonic-gate #include <assert.h> 207c478bd9Sstevel@tonic-gate 21*11a8fa6cSceastha extern void err(); 22*11a8fa6cSceastha extern int newkeys(); 23*11a8fa6cSceastha extern int recopy(); 24*11a8fa6cSceastha extern void whash(); 25*11a8fa6cSceastha 26*11a8fa6cSceastha int 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 */ 407c478bd9Sstevel@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; 507c478bd9Sstevel@tonic-gate int i, j, k; 517c478bd9Sstevel@tonic-gate long keys; 527c478bd9Sstevel@tonic-gate int iflong = 0; 537c478bd9Sstevel@tonic-gate char *sortdir; 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 587c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 597c478bd9Sstevel@tonic-gate #endif 607c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate 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 */ 667c478bd9Sstevel@tonic-gate nhash = atoi(argv[1]+2); 677c478bd9Sstevel@tonic-gate break; 687c478bd9Sstevel@tonic-gate case 'n': /* new, don't append */ 697c478bd9Sstevel@tonic-gate appflg = 0; 707c478bd9Sstevel@tonic-gate break; 717c478bd9Sstevel@tonic-gate case 'a': /* append to old file */ 727c478bd9Sstevel@tonic-gate appflg = 1; 737c478bd9Sstevel@tonic-gate break; 747c478bd9Sstevel@tonic-gate case 'v': /* verbose output */ 757c478bd9Sstevel@tonic-gate chatty = 1; 767c478bd9Sstevel@tonic-gate break; 777c478bd9Sstevel@tonic-gate case 'd': /* keep keys on file .id for check on searching */ 787c478bd9Sstevel@tonic-gate keepkey = 1; 797c478bd9Sstevel@tonic-gate break; 807c478bd9Sstevel@tonic-gate case 'p': /* pipe into sort (saves space, costs time) */ 817c478bd9Sstevel@tonic-gate 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]); 877c478bd9Sstevel@tonic-gate if (argv[1][2] == 'u') /* unlink */ 887c478bd9Sstevel@tonic-gate remove = argv[2]; 897c478bd9Sstevel@tonic-gate argc--; 907c478bd9Sstevel@tonic-gate argv++; 917c478bd9Sstevel@tonic-gate break; 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate argc--; 947c478bd9Sstevel@tonic-gate argv++; 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate strcpy(nma, argc >= 2 ? argv[1] : "Index"); 977c478bd9Sstevel@tonic-gate strcpy(nmb, nma); 987c478bd9Sstevel@tonic-gate strcpy(nmc, nma); 997c478bd9Sstevel@tonic-gate strcpy(nmd, nma); 1007c478bd9Sstevel@tonic-gate strcat(nma, ".ia"); 1017c478bd9Sstevel@tonic-gate strcat(nmb, ".ib"); 1027c478bd9Sstevel@tonic-gate strcat(nmc, ".ic"); 1037c478bd9Sstevel@tonic-gate strcat(nmd, ".id"); 1047c478bd9Sstevel@tonic-gate 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"); 1117c478bd9Sstevel@tonic-gate 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"); 1187c478bd9Sstevel@tonic-gate if (ftb == NULL) 1197c478bd9Sstevel@tonic-gate 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 1237c478bd9Sstevel@tonic-gate 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, 1467c478bd9Sstevel@tonic-gate 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); 1547c478bd9Sstevel@tonic-gate #ifndef D1 1557c478bd9Sstevel@tonic-gate unlink(tmpa); 1567c478bd9Sstevel@tonic-gate #endif 157*11a8fa6cSceastha if (appflg) { 1587c478bd9Sstevel@tonic-gate unlink(tmpb); 1597c478bd9Sstevel@tonic-gate unlink(tmpc); 1607c478bd9Sstevel@tonic-gate } 1617c478bd9Sstevel@tonic-gate if (chatty) 1627c478bd9Sstevel@tonic-gate printf(gettext("%ld key occurrences, %d hashes, %d docs\n"), 1637c478bd9Sstevel@tonic-gate keys, hashes, docs); 1647c478bd9Sstevel@tonic-gate 165*11a8fa6cSceastha return (0); 1667c478bd9Sstevel@tonic-gate } 167