2009-08-22 14:53:28 +04:00
|
|
|
/* $NetBSD: files.c,v 1.35 2009/08/22 10:53:28 dsl Exp $ */
|
2003-08-07 15:32:34 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Ben Harris and Jaromir Dolecek.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
|
|
|
|
*/
|
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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2000-10-07 20:39:34 +04:00
|
|
|
* 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
|
2009-08-22 14:53:28 +04:00
|
|
|
__RCSID("$NetBSD: files.c,v 1.35 2009/08/22 10:53:28 dsl 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>
|
|
|
|
|
2009-08-15 22:40:01 +04:00
|
|
|
static ssize_t seq(FILE *, u_char **);
|
2000-10-17 01:53:19 +04:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
/*
|
|
|
|
* this is called when there is no special key. It's only called
|
|
|
|
* in the first fsort pass.
|
|
|
|
*/
|
|
|
|
int
|
2009-08-15 13:48:46 +04:00
|
|
|
makeline(int flno, int top, struct filelist *filelist, int nfiles,
|
|
|
|
RECHEADER *recbuf, u_char *bufend, struct field *dummy2)
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2001-01-19 00:02:47 +03:00
|
|
|
static u_char *obufend;
|
|
|
|
static size_t osz;
|
2009-08-15 18:31:48 +04:00
|
|
|
u_char *pos;
|
2001-05-15 15:18:23 +04:00
|
|
|
static int filenum = 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
|
|
|
|
2005-06-07 13:51:34 +04:00
|
|
|
c = 0; /* XXXGCC -Wuninitialized [pmppc] */
|
|
|
|
|
2009-08-15 18:31:48 +04:00
|
|
|
pos = recbuf->data;
|
2000-10-07 20:39:34 +04:00
|
|
|
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;
|
|
|
|
}
|
2009-08-15 22:40:01 +04:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
for (;;) {
|
2001-02-19 23:50:17 +03:00
|
|
|
if (flno >= 0 && (fp = fstack[flno].fp) == NULL)
|
|
|
|
return (EOF);
|
|
|
|
else if (fp == NULL) {
|
2001-05-15 15:18:23 +04:00
|
|
|
if (filenum >= nfiles)
|
2000-10-07 20:39:34 +04:00
|
|
|
return (EOF);
|
2001-05-15 15:18:23 +04:00
|
|
|
if (!(fp = fopen(filelist->names[filenum], "r")))
|
|
|
|
err(2, "%s", filelist->names[filenum]);
|
|
|
|
filenum++;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2009-08-15 18:31:48 +04:00
|
|
|
while ((pos < bufend) && ((c = getc(fp)) != EOF)) {
|
|
|
|
*pos++ = c;
|
|
|
|
if (c == REC_D) {
|
2001-05-15 15:18:23 +04:00
|
|
|
recbuf->offset = 0;
|
2009-08-15 18:31:48 +04:00
|
|
|
recbuf->length = pos - recbuf->data;
|
2000-10-07 20:39:34 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2009-08-15 18:31:48 +04:00
|
|
|
if (pos >= bufend) {
|
2001-05-15 15:18:23 +04:00
|
|
|
if (recbuf->data < bufend) {
|
2000-10-07 20:39:34 +04:00
|
|
|
overflow = 1;
|
2001-01-19 00:02:47 +03:00
|
|
|
obufend = bufend;
|
2009-08-15 18:31:48 +04:00
|
|
|
osz = (pos - recbuf->data);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
return (BUFFEND);
|
|
|
|
} else if (c == EOF) {
|
2009-08-15 18:31:48 +04:00
|
|
|
if (recbuf->data != pos) {
|
2000-10-07 20:39:34 +04:00
|
|
|
*pos++ = REC_D;
|
2001-05-15 15:18:23 +04:00
|
|
|
recbuf->offset = 0;
|
2009-08-15 18:31:48 +04:00
|
|
|
recbuf->length = pos - recbuf->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
|
|
|
|
2001-05-15 15:18:23 +04:00
|
|
|
warnx("makeline: line too long: ignoring '%.100s...'", recbuf->data);
|
2000-10-17 01:53:19 +04:00
|
|
|
|
2001-02-19 23:50:17 +03:00
|
|
|
/* Consume the rest of line from input */
|
2003-10-18 07:03:20 +04:00
|
|
|
while ((c = getc(fp)) != REC_D && c != EOF)
|
2001-02-19 23:50:17 +03:00
|
|
|
;
|
2000-10-17 01:53:19 +04:00
|
|
|
|
2001-05-15 15:18:23 +04:00
|
|
|
recbuf->offset = 0;
|
|
|
|
recbuf->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
|
2009-08-15 13:48:46 +04:00
|
|
|
makekey(int flno, int top, struct filelist *filelist, int nfiles,
|
|
|
|
RECHEADER *recbuf, u_char *bufend, struct field *ftbl)
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2001-05-15 15:18:23 +04:00
|
|
|
static int filenum = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
static FILE *dbdesc = 0;
|
2009-08-15 22:40:01 +04:00
|
|
|
static u_char *line_data;
|
|
|
|
static ssize_t line_size;
|
2000-10-07 20:39:34 +04:00
|
|
|
static int overflow = 0;
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2009-08-15 22:40:01 +04:00
|
|
|
/* We get re-entered after returning BUFFEND - save old data */
|
2000-10-07 20:39:34 +04:00
|
|
|
if (overflow) {
|
2009-08-15 22:40:01 +04:00
|
|
|
overflow = enterkey(recbuf, bufend, line_data, line_size, ftbl);
|
|
|
|
return overflow ? BUFFEND : 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2009-08-15 22:40:01 +04:00
|
|
|
/* Loop through files until we find a line of input */
|
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) {
|
2001-05-15 15:18:23 +04:00
|
|
|
if (filenum >= nfiles)
|
2000-10-07 20:39:34 +04:00
|
|
|
return (EOF);
|
2001-05-15 15:18:23 +04:00
|
|
|
dbdesc = fopen(filelist->names[filenum], "r");
|
2000-10-07 20:39:34 +04:00
|
|
|
if (!dbdesc)
|
2001-05-15 15:18:23 +04:00
|
|
|
err(2, "%s", filelist->names[filenum]);
|
|
|
|
filenum++;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2009-08-15 22:40:01 +04:00
|
|
|
line_size = seq(dbdesc, &line_data);
|
|
|
|
if (line_size != 0)
|
|
|
|
/* Got a line */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* End of file ... */
|
|
|
|
FCLOSE(dbdesc);
|
|
|
|
dbdesc = 0;
|
|
|
|
if (flno >= 0)
|
|
|
|
fstack[flno].fp = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line_size > bufend - recbuf->data) {
|
|
|
|
overflow = 1;
|
|
|
|
} else {
|
|
|
|
overflow = enterkey(recbuf, bufend, line_data, line_size, ftbl);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2009-08-15 22:40:01 +04:00
|
|
|
return overflow ? BUFFEND : 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-08-15 22:40:01 +04:00
|
|
|
* get a line of input from fp
|
2000-10-07 20:39:34 +04:00
|
|
|
*/
|
2009-08-15 22:40:01 +04:00
|
|
|
static ssize_t
|
|
|
|
seq(FILE *fp, u_char **line)
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2009-08-15 20:50:29 +04:00
|
|
|
static u_char *buf;
|
|
|
|
static size_t buf_size = DEFLLEN;
|
2006-05-11 23:16:42 +04:00
|
|
|
u_char *end, *pos;
|
2000-10-16 00:46:33 +04:00
|
|
|
int c;
|
2009-08-15 20:50:29 +04:00
|
|
|
u_char *new_buf;
|
2001-02-19 23:50:17 +03:00
|
|
|
|
2009-08-15 20:50:29 +04:00
|
|
|
if (!buf) {
|
2004-02-15 14:52:12 +03:00
|
|
|
/* one-time initialization */
|
2009-08-15 20:50:29 +04:00
|
|
|
buf = malloc(buf_size);
|
|
|
|
if (!buf)
|
|
|
|
err(2, "malloc of linebuf for %zu bytes failed",
|
|
|
|
buf_size);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2009-08-15 20:50:29 +04:00
|
|
|
|
|
|
|
end = buf + buf_size;
|
2000-10-07 20:39:34 +04:00
|
|
|
pos = buf;
|
2000-10-08 00:37:06 +04:00
|
|
|
while ((c = getc(fp)) != EOF) {
|
2009-08-15 22:40:01 +04:00
|
|
|
*pos++ = c;
|
|
|
|
if (c == REC_D) {
|
|
|
|
*line = buf;
|
|
|
|
return pos - buf;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
if (pos == end) {
|
2009-08-15 20:50:29 +04:00
|
|
|
/* Long line - double size of buffer */
|
2009-08-15 22:40:01 +04:00
|
|
|
/* XXX: Check here for stupidly long lines */
|
2009-08-15 20:50:29 +04:00
|
|
|
buf_size *= 2;
|
|
|
|
new_buf = realloc(buf, buf_size);
|
|
|
|
if (!new_buf)
|
|
|
|
err(2, "realloc of linebuf to %zu bytes failed",
|
|
|
|
buf_size);
|
2000-10-17 01:53:19 +04:00
|
|
|
|
2009-08-15 20:50:29 +04:00
|
|
|
end = new_buf + buf_size;
|
|
|
|
pos = new_buf + (pos - buf);
|
|
|
|
buf = new_buf;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
}
|
2009-08-15 20:50:29 +04:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
if (pos != buf) {
|
2009-08-15 20:50:29 +04:00
|
|
|
/* EOF part way through line - add line terminator */
|
2000-10-07 20:39:34 +04:00
|
|
|
*pos++ = REC_D;
|
2009-08-15 22:40:01 +04:00
|
|
|
*line = buf;
|
|
|
|
return pos - buf;
|
2009-08-15 20:50:29 +04:00
|
|
|
}
|
|
|
|
|
2009-08-15 22:40:01 +04:00
|
|
|
return 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a key/line pair to a temporary file
|
|
|
|
*/
|
|
|
|
void
|
2009-08-15 13:48:46 +04:00
|
|
|
putrec(const RECHEADER *rec, FILE *fp)
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2009-08-16 23:53:43 +04:00
|
|
|
EWRITE(rec, 1, rec->length + REC_DATA_OFFSET, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write a line to output
|
|
|
|
*/
|
|
|
|
void
|
2009-08-15 13:48:46 +04:00
|
|
|
putline(const RECHEADER *rec, 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
|
|
|
}
|
|
|
|
|
2009-08-22 14:53:28 +04:00
|
|
|
/*
|
|
|
|
* write dump of key to output (for -Dk)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
putkeydump(const RECHEADER *rec, FILE *fp)
|
|
|
|
{
|
|
|
|
EWRITE(rec, 1, rec->offset + REC_DATA_OFFSET, fp);
|
|
|
|
}
|
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
/*
|
|
|
|
* get a record from a temporary file. (Used by merge sort.)
|
|
|
|
*/
|
|
|
|
int
|
2009-08-15 13:48:46 +04:00
|
|
|
geteasy(int flno, int top, struct filelist *filelist, int nfiles,
|
|
|
|
RECHEADER *rec, u_char *end, struct field *dummy2)
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
|
|
|
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;
|
2009-08-16 23:53:43 +04:00
|
|
|
if ((u_char *) rec > end - REC_DATA_OFFSET)
|
2000-10-07 20:39:34 +04:00
|
|
|
return (BUFFEND);
|
2009-08-16 23:53:43 +04:00
|
|
|
if (!fread(rec, 1, REC_DATA_OFFSET, fp)) {
|
2000-10-08 00:37:06 +04:00
|
|
|
fclose(fp);
|
|
|
|
fstack[flno].fp = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
return (EOF);
|
|
|
|
}
|
2009-04-13 15:07:59 +04:00
|
|
|
if (end - rec->data < (ptrdiff_t)rec->length) {
|
2009-08-16 23:53:43 +04:00
|
|
|
for (i = REC_DATA_OFFSET - 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);
|
|
|
|
}
|