#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
__RCSID("$NetBSD: file.c,v 1.13 2024/08/14 05:02:19 rin Exp $");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>
#include "grep.h"
#define MAXBUFSIZ (32 * 1024)
#define LNBUFBUMP 80
#ifndef WITHOUT_GZIP
static gzFile gzbufdesc;
#endif
#ifndef WITHOUT_BZ2
static BZFILE* bzbufdesc;
#endif
static unsigned char buffer[MAXBUFSIZ + 1];
static unsigned char *bufpos;
static size_t bufrem;
static unsigned char *lnbuf;
static size_t lnbuflen;
static inline int
grep_refill(struct file *f)
{
ssize_t nr = -1;
#ifndef WITHOUT_BZ2
int bzerr;
#endif
bufpos = buffer;
bufrem = 0;
#ifndef WITHOUT_GZIP
if (filebehave == FILE_GZIP) {
nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
if (nr == -1)
return -1;
}
#endif
#ifndef WITHOUT_BZ2
if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
switch (bzerr) {
case BZ_OK:
case BZ_STREAM_END:
break;
case BZ_DATA_ERROR_MAGIC:
BZ2_bzReadClose(&bzerr, bzbufdesc);
bzbufdesc = NULL;
if (lseek(f->fd, 0, SEEK_SET) == -1)
return (-1);
nr = read(f->fd, buffer, MAXBUFSIZ);
break;
default:
nr = -1;
}
if (nr == -1)
return -1;
}
#endif
if (nr == -1) {
nr = read(f->fd, buffer, MAXBUFSIZ);
}
if (nr < 0)
return (-1);
bufrem = nr;
return (0);
}
static inline void
grep_lnbufgrow(size_t newlen)
{
if (lnbuflen < newlen) {
lnbuf = grep_realloc(lnbuf, newlen);
lnbuflen = newlen;
}
}
static void
grep_copyline(size_t off, size_t len)
{
memcpy(lnbuf + off, bufpos, len);
lnbuf[off + len] = '\0';
}
char *
grep_fgetln(struct file *f, size_t *lenp)
{
unsigned char *p;
size_t len;
size_t off;
ptrdiff_t diff;
if (bufrem == 0 && grep_refill(f) != 0)
goto error;
if (bufrem == 0) {
*lenp = 0;
return ((char *)bufpos);
}
if ((p = memchr(bufpos, line_sep, bufrem)) != NULL) {
++p;
len = p - bufpos;
grep_lnbufgrow(len + 1);
grep_copyline(0, len);
*lenp = len;
bufrem -= len;
bufpos = p;
return (char *)lnbuf;
}
for (len = bufrem, off = 0; ; len += bufrem) {
grep_lnbufgrow(len + LNBUFBUMP);
grep_copyline(off, len - off);
off = len;
if (grep_refill(f) != 0)
goto error;
if (bufrem == 0)
break;
if ((p = memchr(bufpos, line_sep, bufrem)) == NULL)
continue;
++p;
diff = p - bufpos;
len += diff;
grep_lnbufgrow(len + 1);
grep_copyline(off, diff);
bufrem -= diff;
bufpos = p;
break;
}
*lenp = len;
return ((char *)lnbuf);
error:
*lenp = 0;
return (NULL);
}
static inline struct file *
grep_file_init(struct file *f)
{
#ifndef WITHOUT_GZIP
if (filebehave == FILE_GZIP &&
(gzbufdesc = gzdopen(f->fd, "r")) == NULL)
goto error;
#endif
#ifndef WITHOUT_BZ2
if (filebehave == FILE_BZIP &&
(bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
goto error;
#endif
if (grep_refill(f) != 0)
goto error;
if (!nulldataflag && binbehave != BINFILE_TEXT &&
memchr(bufpos, '\0', bufrem) != NULL)
f->binary = true;
return (f);
error:
close(f->fd);
free(f);
return (NULL);
}
struct file *
grep_open(const char *path)
{
struct file *f;
f = grep_malloc(sizeof *f);
memset(f, 0, sizeof *f);
if (path == NULL) {
lbflag = true;
f->fd = STDIN_FILENO;
} else if ((f->fd = open(path, O_RDONLY)) == -1) {
free(f);
return (NULL);
}
return (grep_file_init(f));
}
void
grep_close(struct file *f)
{
close(f->fd);
bufpos = buffer;
bufrem = 0;
free(lnbuf);
lnbuf = NULL;
lnbuflen = 0;
}