2001-02-19 23:50:17 +03:00
|
|
|
/* $NetBSD: files.c,v 1.16 2001/02/19 20:50:17 jdolecek Exp $ */
|
2000-10-07 22:37:09 +04:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Peter McIlroy.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sort.h"
|
|
|
|
#include "fsort.h"
|
|
|
|
|
2000-10-07 22:37:09 +04:00
|
|
|
#ifndef lint
|
2001-02-19 23:50:17 +03:00
|
|
|
__RCSID("$NetBSD: files.c,v 1.16 2001/02/19 20:50:17 jdolecek Exp $");
|
2000-10-07 22:37:09 +04:00
|
|
|
__SCCSID("@(#)files.c 8.1 (Berkeley) 6/6/93");
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2001-02-19 23:50:17 +03:00
|
|
|
static int seq __P((FILE *, DBT *, DBT *));
|
2000-10-17 01:53:19 +04:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
/*
|
|
|
|
* this is the subroutine for file management for fsort().
|
|
|
|
* It keeps the buffers for all temporary files.
|
|
|
|
*/
|
|
|
|
int
|
2001-01-11 17:05:24 +03:00
|
|
|
getnext(binno, infl0, filelist, nfiles, pos, end, dummy)
|
|
|
|
int binno, infl0;
|
|
|
|
struct filelist *filelist;
|
|
|
|
int nfiles;
|
2001-02-19 23:50:17 +03:00
|
|
|
RECHEADER *pos;
|
2000-10-16 00:46:33 +04:00
|
|
|
u_char *end;
|
2000-10-07 20:39:34 +04:00
|
|
|
struct field *dummy;
|
|
|
|
{
|
2000-10-16 00:46:33 +04:00
|
|
|
int i;
|
|
|
|
u_char *hp;
|
2000-10-17 19:22:57 +04:00
|
|
|
static size_t nleft = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
static int cnt = 0, flag = -1;
|
|
|
|
static u_char maxb = 0;
|
2000-10-08 00:37:06 +04:00
|
|
|
static FILE *fp;
|
2000-10-07 20:39:34 +04:00
|
|
|
|
|
|
|
if (nleft == 0) {
|
|
|
|
if (binno < 0) /* reset files. */ {
|
|
|
|
for (i = 0; i < nfiles; i++) {
|
2001-01-11 17:05:24 +03:00
|
|
|
rewind(fstack[infl0 + i].fp);
|
|
|
|
fstack[infl0 + i].max_o = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
flag = -1;
|
|
|
|
nleft = cnt = 0;
|
2001-02-19 23:50:17 +03:00
|
|
|
return (-1);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2001-01-11 17:05:24 +03:00
|
|
|
maxb = fstack[infl0].maxb;
|
2000-10-07 20:39:34 +04:00
|
|
|
for (; nleft == 0; cnt++) {
|
|
|
|
if (cnt >= nfiles) {
|
|
|
|
cnt = 0;
|
|
|
|
return (EOF);
|
|
|
|
}
|
2001-01-11 17:05:24 +03:00
|
|
|
fp = fstack[infl0 + cnt].fp;
|
2000-10-17 19:22:57 +04:00
|
|
|
fread(&nleft, sizeof(nleft), 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
if (binno < maxb)
|
2001-01-11 17:05:24 +03:00
|
|
|
fstack[infl0+cnt].max_o
|
2000-10-07 20:39:34 +04:00
|
|
|
+= sizeof(nleft) + nleft;
|
|
|
|
else if (binno == maxb) {
|
2001-01-11 17:05:24 +03:00
|
|
|
if (binno != fstack[infl0].lastb) {
|
|
|
|
fseek(fp, fstack[infl0+
|
2000-10-07 20:39:34 +04:00
|
|
|
cnt].max_o, SEEK_SET);
|
2000-10-08 00:37:06 +04:00
|
|
|
fread(&nleft, sizeof(nleft), 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
if (nleft == 0)
|
2000-10-08 00:37:06 +04:00
|
|
|
fclose(fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
} else if (binno == maxb + 1) { /* skip a bin */
|
2000-10-08 00:37:06 +04:00
|
|
|
fseek(fp, nleft, SEEK_CUR);
|
|
|
|
fread(&nleft, sizeof(nleft), 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
flag = cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((u_char *) pos > end - sizeof(TRECHEADER))
|
|
|
|
return (BUFFEND);
|
2000-10-17 19:22:57 +04:00
|
|
|
fread(pos, sizeof(TRECHEADER), 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
if (end - pos->data < pos->length) {
|
2001-02-19 23:50:17 +03:00
|
|
|
hp = ((u_char *)pos) + sizeof(TRECHEADER);
|
2000-10-07 20:39:34 +04:00
|
|
|
for (i = sizeof(TRECHEADER); i ; i--)
|
2000-10-08 00:37:06 +04:00
|
|
|
ungetc(*--hp, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
return (BUFFEND);
|
|
|
|
}
|
2000-10-08 00:37:06 +04:00
|
|
|
fread(pos->data, pos->length, 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
nleft -= pos->length + sizeof(TRECHEADER);
|
2001-01-11 17:05:24 +03:00
|
|
|
if (nleft == 0 && binno == fstack[infl0].maxb)
|
2000-10-08 00:37:06 +04:00
|
|
|
fclose(fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is called when there is no special key. It's only called
|
|
|
|
* in the first fsort pass.
|
|
|
|
*/
|
|
|
|
int
|
2001-01-11 17:05:24 +03:00
|
|
|
makeline(flno, top, filelist, nfiles, buffer, bufend, dummy2)
|
|
|
|
int flno, top;
|
|
|
|
struct filelist *filelist;
|
|
|
|
int nfiles;
|
2001-02-19 23:50:17 +03:00
|
|
|
RECHEADER *buffer;
|
2000-10-07 20:39:34 +04:00
|
|
|
u_char *bufend;
|
|
|
|
struct field *dummy2;
|
|
|
|
{
|
2001-01-19 00:02:47 +03:00
|
|
|
static u_char *obufend;
|
|
|
|
static size_t osz;
|
2000-10-17 01:53:19 +04:00
|
|
|
char *pos;
|
2000-10-07 20:39:34 +04:00
|
|
|
static int fileno = 0, overflow = 0;
|
2000-10-08 00:37:06 +04:00
|
|
|
static FILE *fp = 0;
|
2000-10-16 00:46:33 +04:00
|
|
|
int c;
|
2000-10-07 20:39:34 +04:00
|
|
|
|
|
|
|
pos = (char *) buffer->data;
|
|
|
|
if (overflow) {
|
2001-01-19 00:02:47 +03:00
|
|
|
/*
|
|
|
|
* Buffer shortage is solved by either of two ways:
|
|
|
|
* o flush previous buffered data and start using the
|
|
|
|
* buffer from start (see fsort())
|
|
|
|
* o realloc buffer and bump bufend
|
|
|
|
*
|
|
|
|
* The former is preferred, realloc is only done when
|
|
|
|
* there is exactly one item in buffer which does not fit.
|
|
|
|
*/
|
|
|
|
if (bufend == obufend)
|
|
|
|
memmove(pos, bufend - osz, osz);
|
|
|
|
|
|
|
|
pos += osz;
|
2000-10-07 20:39:34 +04:00
|
|
|
overflow = 0;
|
|
|
|
}
|
|
|
|
for (;;) {
|
2001-02-19 23:50:17 +03:00
|
|
|
if (flno >= 0 && (fp = fstack[flno].fp) == NULL)
|
|
|
|
return (EOF);
|
|
|
|
else if (fp == NULL) {
|
|
|
|
if (fileno >= nfiles)
|
2000-10-07 20:39:34 +04:00
|
|
|
return (EOF);
|
2001-01-11 17:05:24 +03:00
|
|
|
if (!(fp = fopen(filelist->names[fileno], "r")))
|
|
|
|
err(2, "%s", filelist->names[fileno]);
|
2001-02-19 23:50:17 +03:00
|
|
|
fileno++;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2000-10-17 01:53:19 +04:00
|
|
|
while ((pos < (char *)bufend) && ((c = getc(fp)) != EOF)) {
|
2000-10-07 20:39:34 +04:00
|
|
|
if ((*pos++ = c) == REC_D) {
|
|
|
|
buffer->offset = 0;
|
|
|
|
buffer->length = pos - (char *) buffer->data;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2000-10-17 01:53:19 +04:00
|
|
|
if (pos >= (char *)bufend) {
|
|
|
|
if (buffer->data < bufend) {
|
2000-10-07 20:39:34 +04:00
|
|
|
overflow = 1;
|
2001-01-19 00:02:47 +03:00
|
|
|
obufend = bufend;
|
|
|
|
osz = (pos - (char *) buffer->data);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
return (BUFFEND);
|
|
|
|
} else if (c == EOF) {
|
|
|
|
if (buffer->data != (u_char *) pos) {
|
|
|
|
*pos++ = REC_D;
|
|
|
|
buffer->offset = 0;
|
|
|
|
buffer->length = pos - (char *) buffer->data;
|
2001-02-19 23:50:17 +03:00
|
|
|
return (0);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2000-10-08 00:37:06 +04:00
|
|
|
FCLOSE(fp);
|
|
|
|
fp = 0;
|
2001-02-19 23:50:17 +03:00
|
|
|
if (flno >= 0)
|
|
|
|
fstack[flno].fp = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
} else {
|
2000-10-17 01:53:19 +04:00
|
|
|
|
|
|
|
warnx("makeline: line too long: ignoring '%.100s...'", buffer->data);
|
|
|
|
|
2001-02-19 23:50:17 +03:00
|
|
|
/* Consume the rest of line from input */
|
|
|
|
while((c = getc(fp)) != REC_D && c != EOF)
|
|
|
|
;
|
2000-10-17 01:53:19 +04:00
|
|
|
|
|
|
|
buffer->offset = 0;
|
|
|
|
buffer->length = 0;
|
2001-02-19 23:50:17 +03:00
|
|
|
|
|
|
|
return (BUFFEND);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This generates keys. It's only called in the first fsort pass
|
|
|
|
*/
|
|
|
|
int
|
2001-01-11 17:05:24 +03:00
|
|
|
makekey(flno, top, filelist, nfiles, buffer, bufend, ftbl)
|
|
|
|
int flno, top;
|
|
|
|
struct filelist *filelist;
|
|
|
|
int nfiles;
|
2001-02-19 23:50:17 +03:00
|
|
|
RECHEADER *buffer;
|
2000-10-07 20:39:34 +04:00
|
|
|
u_char *bufend;
|
|
|
|
struct field *ftbl;
|
|
|
|
{
|
|
|
|
static int fileno = 0;
|
|
|
|
static FILE *dbdesc = 0;
|
|
|
|
static DBT dbkey[1], line[1];
|
|
|
|
static int overflow = 0;
|
|
|
|
int c;
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
if (overflow) {
|
2001-02-19 23:50:17 +03:00
|
|
|
overflow = enterkey(buffer, line, bufend - (u_char *)buffer,
|
|
|
|
ftbl);
|
2000-10-17 01:53:19 +04:00
|
|
|
if (overflow)
|
|
|
|
return (BUFFEND);
|
|
|
|
else
|
|
|
|
return (0);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
for (;;) {
|
|
|
|
if (flno >= 0) {
|
2000-10-08 00:37:06 +04:00
|
|
|
if (!(dbdesc = fstack[flno].fp))
|
2001-02-19 23:50:17 +03:00
|
|
|
return (EOF);
|
2000-10-07 20:39:34 +04:00
|
|
|
} else if (!dbdesc) {
|
|
|
|
if (fileno >= nfiles)
|
|
|
|
return (EOF);
|
2001-01-11 17:05:24 +03:00
|
|
|
dbdesc = fopen(filelist->names[fileno], "r");
|
2000-10-07 20:39:34 +04:00
|
|
|
if (!dbdesc)
|
2001-01-11 17:05:24 +03:00
|
|
|
err(2, "%s", filelist->names[fileno]);
|
2001-02-19 23:50:17 +03:00
|
|
|
fileno++;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2000-10-17 01:53:19 +04:00
|
|
|
if (!(c = seq(dbdesc, line, dbkey))) {
|
|
|
|
if ((signed)line->size > bufend - buffer->data) {
|
2000-10-07 20:39:34 +04:00
|
|
|
overflow = 1;
|
2000-10-17 01:53:19 +04:00
|
|
|
} else {
|
2000-10-07 20:39:34 +04:00
|
|
|
overflow = enterkey(buffer, line,
|
|
|
|
bufend - (u_char *) buffer, ftbl);
|
2000-10-17 01:53:19 +04:00
|
|
|
}
|
2000-10-07 20:39:34 +04:00
|
|
|
if (overflow)
|
|
|
|
return (BUFFEND);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (c == EOF) {
|
|
|
|
FCLOSE(dbdesc);
|
|
|
|
dbdesc = 0;
|
2001-02-19 23:50:17 +03:00
|
|
|
if (flno >= 0)
|
|
|
|
fstack[flno].fp = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
} else {
|
|
|
|
((char *) line->data)[60] = '\000';
|
2000-10-17 01:53:19 +04:00
|
|
|
warnx("makekey: line too long: ignoring %.100s...",
|
2000-10-07 20:39:34 +04:00
|
|
|
(char *)line->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-10-08 00:37:06 +04:00
|
|
|
* get a key/line pair from fp
|
2000-10-07 20:39:34 +04:00
|
|
|
*/
|
2000-10-17 01:53:19 +04:00
|
|
|
static int
|
2000-10-08 00:37:06 +04:00
|
|
|
seq(fp, line, key)
|
|
|
|
FILE *fp;
|
2000-10-07 20:39:34 +04:00
|
|
|
DBT *key, *line;
|
|
|
|
{
|
|
|
|
static char *buf, flag = 1;
|
2000-10-16 00:46:33 +04:00
|
|
|
char *end, *pos;
|
|
|
|
int c;
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
if (flag) {
|
|
|
|
flag = 0;
|
|
|
|
buf = (char *) linebuf;
|
2000-10-17 01:53:19 +04:00
|
|
|
end = buf + linebuf_size;
|
2000-10-07 20:39:34 +04:00
|
|
|
line->data = buf;
|
|
|
|
}
|
|
|
|
pos = buf;
|
2000-10-08 00:37:06 +04:00
|
|
|
while ((c = getc(fp)) != EOF) {
|
2000-10-07 20:39:34 +04:00
|
|
|
if ((*pos++ = c) == REC_D) {
|
|
|
|
line->size = pos - buf;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (pos == end) {
|
2000-10-17 01:53:19 +04:00
|
|
|
linebuf_size *= 2;
|
|
|
|
linebuf = realloc(linebuf, linebuf_size);
|
|
|
|
if (!linebuf)
|
2001-02-19 23:50:17 +03:00
|
|
|
err(2, "realloc of linebuf to %lu bytes failed",
|
|
|
|
(unsigned long)linebuf_size);
|
2000-10-17 01:53:19 +04:00
|
|
|
|
|
|
|
end = linebuf + linebuf_size;
|
|
|
|
pos = linebuf + (pos - buf);
|
|
|
|
line->data = buf = (char *)linebuf;
|
|
|
|
continue;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pos != buf) {
|
|
|
|
*pos++ = REC_D;
|
|
|
|
line->size = pos - buf;
|
|
|
|
return (0);
|
|
|
|
} else
|
|
|
|
return (EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a key/line pair to a temporary file
|
|
|
|
*/
|
|
|
|
void
|
2000-10-08 00:37:06 +04:00
|
|
|
putrec(rec, fp)
|
2001-02-19 23:50:17 +03:00
|
|
|
const RECHEADER *rec;
|
2000-10-16 00:46:33 +04:00
|
|
|
FILE *fp;
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2000-10-08 00:37:06 +04:00
|
|
|
EWRITE(rec, 1, rec->length + sizeof(TRECHEADER), fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a line to output
|
|
|
|
*/
|
|
|
|
void
|
2000-10-08 00:37:06 +04:00
|
|
|
putline(rec, fp)
|
2001-02-19 23:50:17 +03:00
|
|
|
const RECHEADER *rec;
|
2000-10-16 00:46:33 +04:00
|
|
|
FILE *fp;
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2000-10-08 00:37:06 +04:00
|
|
|
EWRITE(rec->data+rec->offset, 1, rec->length - rec->offset, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get a record from a temporary file. (Used by merge sort.)
|
|
|
|
*/
|
|
|
|
int
|
2001-01-11 17:05:24 +03:00
|
|
|
geteasy(flno, top, filelist, nfiles, rec, end, dummy2)
|
|
|
|
int flno, top;
|
|
|
|
struct filelist *filelist;
|
|
|
|
int nfiles;
|
2001-02-19 23:50:17 +03:00
|
|
|
RECHEADER *rec;
|
2000-10-16 00:46:33 +04:00
|
|
|
u_char *end;
|
2000-10-07 20:39:34 +04:00
|
|
|
struct field *dummy2;
|
|
|
|
{
|
|
|
|
int i;
|
2000-10-08 00:37:06 +04:00
|
|
|
FILE *fp;
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2000-10-08 00:37:06 +04:00
|
|
|
fp = fstack[flno].fp;
|
2000-10-07 20:39:34 +04:00
|
|
|
if ((u_char *) rec > end - sizeof(TRECHEADER))
|
|
|
|
return (BUFFEND);
|
2000-10-08 00:37:06 +04:00
|
|
|
if (!fread(rec, 1, sizeof(TRECHEADER), fp)) {
|
|
|
|
fclose(fp);
|
|
|
|
fstack[flno].fp = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
return (EOF);
|
|
|
|
}
|
|
|
|
if (end - rec->data < rec->length) {
|
|
|
|
for (i = sizeof(TRECHEADER) - 1; i >= 0; i--)
|
2000-10-08 00:37:06 +04:00
|
|
|
ungetc(*((char *) rec + i), fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
return (BUFFEND);
|
|
|
|
}
|
2000-10-08 00:37:06 +04:00
|
|
|
fread(rec->data, rec->length, 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
return (0);
|
|
|
|
}
|