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 <assert.h>
197c478bd9Sstevel@tonic-gate
20*11a8fa6cSceastha static void putl(long, FILE *);
21*11a8fa6cSceastha
22*11a8fa6cSceastha void
whash(FILE * ft,FILE * fa,FILE * fb,int nhash,int iflong,long * ptotct,int * phused)23*11a8fa6cSceastha whash(FILE *ft, FILE *fa, FILE *fb, int nhash, int iflong,
24*11a8fa6cSceastha long *ptotct, int *phused)
257c478bd9Sstevel@tonic-gate {
267c478bd9Sstevel@tonic-gate char line[100];
277c478bd9Sstevel@tonic-gate int hash = 0, hused = 0;
287c478bd9Sstevel@tonic-gate long totct = 0L;
297c478bd9Sstevel@tonic-gate int ct = 0;
307c478bd9Sstevel@tonic-gate long point;
317c478bd9Sstevel@tonic-gate long opoint = -1;
327c478bd9Sstevel@tonic-gate int m;
337c478bd9Sstevel@tonic-gate int k;
347c478bd9Sstevel@tonic-gate long lp;
357c478bd9Sstevel@tonic-gate long *hpt;
367c478bd9Sstevel@tonic-gate int *hfreq = NULL;
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate hpt = (long *)calloc(nhash+1, sizeof (*hpt));
397c478bd9Sstevel@tonic-gate assert(hpt != NULL);
407c478bd9Sstevel@tonic-gate hfreq = (int *)calloc(nhash, sizeof (*hfreq));
417c478bd9Sstevel@tonic-gate assert(hfreq != NULL);
427c478bd9Sstevel@tonic-gate hpt[0] = 0;
437c478bd9Sstevel@tonic-gate lp = 0;
44*11a8fa6cSceastha while (fgets(line, 100, ft)) {
457c478bd9Sstevel@tonic-gate totct++;
467c478bd9Sstevel@tonic-gate sscanf(line, "%d %ld", &k, &point);
47*11a8fa6cSceastha if (hash < k) {
487c478bd9Sstevel@tonic-gate hused++;
497c478bd9Sstevel@tonic-gate if (iflong) putl(-1L, fb);
507c478bd9Sstevel@tonic-gate else putw(-1, fb);
517c478bd9Sstevel@tonic-gate hfreq[hash] = ct;
52*11a8fa6cSceastha while (hash < k) {
537c478bd9Sstevel@tonic-gate hpt[++hash] = lp;
547c478bd9Sstevel@tonic-gate hfreq[hash] = 0;
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate hpt[hash] = lp += iflong ? sizeof (long) : sizeof (int);
577c478bd9Sstevel@tonic-gate opoint = -1;
587c478bd9Sstevel@tonic-gate ct = 0;
597c478bd9Sstevel@tonic-gate }
60*11a8fa6cSceastha if (point != opoint) {
617c478bd9Sstevel@tonic-gate if (iflong)
627c478bd9Sstevel@tonic-gate putl(opoint = point, fb);
637c478bd9Sstevel@tonic-gate else
647c478bd9Sstevel@tonic-gate putw((int)(opoint = point), fb);
657c478bd9Sstevel@tonic-gate lp += iflong ? sizeof (long) : sizeof (int);
667c478bd9Sstevel@tonic-gate ct++;
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate if (iflong) putl(-1L, fb);
707c478bd9Sstevel@tonic-gate else putw(-1, fb);
717c478bd9Sstevel@tonic-gate while (hash < nhash)
727c478bd9Sstevel@tonic-gate hpt[++hash] = lp;
737c478bd9Sstevel@tonic-gate fwrite(&nhash, sizeof (nhash), 1, fa);
747c478bd9Sstevel@tonic-gate fwrite(&iflong, sizeof (iflong), 1, fa);
757c478bd9Sstevel@tonic-gate fwrite(hpt, sizeof (*hpt), nhash, fa);
767c478bd9Sstevel@tonic-gate fwrite(hfreq, sizeof (*hfreq), nhash, fa);
777c478bd9Sstevel@tonic-gate *ptotct = totct;
787c478bd9Sstevel@tonic-gate *phused = hused;
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate
81*11a8fa6cSceastha static void
putl(long ll,FILE * f)82*11a8fa6cSceastha putl(long ll, FILE *f)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate putw(ll, f);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate long
getl(FILE * f)88*11a8fa6cSceastha getl(FILE *f)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate return (getw(f));
917c478bd9Sstevel@tonic-gate }
92