memset(keylist, 0, MAXNUM * sizeof(u_char *));
fsort(binno, depth, infiles, nfiles, outfp, ftbl)	register int binno, depth;	register union f_handle infiles;	register int nfiles;	FILE *outfp;	register struct field *ftbl;{	register u_char *bufend, **keypos, *tmpbuf;	u_char *weights;	int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;	int c, nelem;	int sizes [NBINS+1];	union f_handle tfiles, mstart = {MAXFCT-16};	register int (*get)(int, union f_handle, int, RECHEADER *,		u_char *, struct field *);	register RECHEADER *crec;	struct field tfield[2];	FILE *prevfp, *tailfp[FSORTMAX+1];	memset(tailfp, 0, sizeof(tailfp));	prevfp = outfp;	memset(tfield, 0, sizeof(tfield));	if (ftbl[0].flags & R)		tfield[0].weights = Rascii;	else		tfield[0].weights = ascii;	tfield[0].icol.num = 1;	weights = ftbl[0].weights;	if (!buffer) {		buffer = malloc(BUFSIZE + 1);		keylist = malloc(MAXNUM * sizeof(u_char *));		if (!SINGL_FLD)			linebuf = malloc(MAXLLEN);	}	bufend = buffer + BUFSIZE;	if (binno >= 0) {		tfiles.top = infiles.top + nfiles;		get = getnext;	} else {		tfiles.top = 0;		if (SINGL_FLD)			get = makeline;		else			get = makekey;	}					for (;;) {		memset(sizes, 0, sizeof(sizes));		c = ntfiles = 0;		if (binno == weights[REC_D] &&		    !(SINGL_FLD && ftbl[0].flags & F)) {	/* pop */			rd_append(weights[REC_D],			    infiles, nfiles, prevfp, buffer, bufend);			break;		} else if (binno == weights[REC_D]) {			depth = 0;		/* start over on flat weights */			ftbl = tfield;			weights = ftbl[0].weights;		}		while (c != EOF) {			keypos = keylist;			nelem = 0;			crec = (RECHEADER *) buffer;			while((c = get(binno, infiles, nfiles, crec, bufend,			    ftbl)) == 0) {				*keypos++ = crec->data + depth;				if (++nelem == MAXNUM) {					c = BUFFEND;					break;				}				crec =(RECHEADER *)	((char *) crec +				SALIGN(crec->length) + sizeof(TRECHEADER));			}			if (c == BUFFEND || ntfiles || mfct) {	/* push */				if (panic >= PANIC) {					fstack[MAXFCT-16+mfct].fp = ftmp();					if (radixsort((const u_char **)keylist,					    nelem, weights, REC_D))						err(2, NULL);					append(keylist, nelem, depth, fstack[					 MAXFCT-16+mfct].fp, putrec, ftbl);					mfct++;					/* reduce number of open files */					if (mfct == 16 ||(c == EOF && ntfiles)) {						tmpbuf = malloc(bufend -						    crec->data);						memmove(tmpbuf, crec->data,						    bufend - crec->data);						fstack[tfiles.top + ntfiles].fp						    = ftmp();						fmerge(0, mstart, mfct, geteasy,						  fstack[tfiles.top+ntfiles].fp,						  putrec, ftbl);						ntfiles++;						mfct = 0;						memmove(crec->data, tmpbuf,						    bufend - crec->data);						free(tmpbuf);					}				} else {					fstack[tfiles.top + ntfiles].fp= ftmp();					onepass(keylist, depth, nelem, sizes,					weights, fstack[tfiles.top+ntfiles].fp);					ntfiles++;				}			}		}		get = getnext;		if (!ntfiles && !mfct) {	/* everything in memory--pop */			if (nelem > 1 && radixsort((const u_char **)keylist,			    nelem, weights, REC_D))				err(2, NULL);			append(keylist, nelem, depth, outfp, putline, ftbl);			break;					/* pop */		}		if (panic >= PANIC) {			if (!ntfiles)				fmerge(0, mstart, mfct, geteasy,				    outfp, putline, ftbl);			else				fmerge(0, tfiles, ntfiles, geteasy,				    outfp, putline, ftbl);			break;						}		total = maxb = lastb = 0;	/* find if one bin dominates */		for (i = 0; i < NBINS; i++)		  if (sizes[i]) {			if (sizes[i] > sizes[maxb])				maxb = i;			lastb = i;			total += sizes[i];		}		if (sizes[maxb] < max((total / 2) , BUFSIZE))			maxb = lastb;	/* otherwise pop after last bin */		fstack[tfiles.top].lastb = lastb;		fstack[tfiles.top].maxb = maxb;			/* start refining next level. */		get(-1, tfiles, ntfiles, crec, bufend, 0);	/* rewind */		for (i = 0; i < maxb; i++) {			if (!sizes[i])	/* bin empty; step ahead file offset */				get(i, tfiles, ntfiles, crec, bufend, 0);			else				fsort(i, depth+1, tfiles, ntfiles, outfp, ftbl);		}		if (lastb != maxb) {			if (prevfp != outfp)				tailfp[panic] = prevfp;			prevfp = ftmp();			for (i = maxb+1; i <= lastb; i++)				if (!sizes[i])					get(i, tfiles, ntfiles, crec, bufend,0);				else					fsort(i, depth+1, tfiles, ntfiles,					    prevfp, ftbl);		}		/* sort biggest (or last) bin at this level */		depth++;		panic++;		binno = maxb;		infiles.top = tfiles.top;	/* getnext will free tfiles, */		nfiles = ntfiles;		/* so overwrite them */	}	if (prevfp != outfp) {		concat(outfp, prevfp);		fclose(prevfp);	}	for (i = panic; i >= 0; --i)		if (tailfp[i]) {			concat(outfp, tailfp[i]);			fclose(tailfp[i]);		}}