sort: unify the code to read from FILE *

Previously the code to read from a local file or stdin was sperarated
After the change to remove the home made line reader used for stdin
(replaced by getdelim) it apprears that the rest of the code which is
used to read from any FILE * but stdin can benefit from the exact same
change.
This commit is contained in:
Baptiste Daroussin 2022-10-13 09:36:48 +02:00
parent e8815fb30b
commit 8b9071360a

View File

@ -62,26 +62,18 @@ const char *compress_program;
size_t max_open_files = 16;
/*
* How much space we read from file at once
*/
#define READ_CHUNK (4096)
/*
* File reader structure
*/
struct file_reader
{
struct reader_buffer rb;
FILE *file;
char *fname;
char *buffer;
unsigned char *mmapaddr;
unsigned char *mmapptr;
size_t bsz;
size_t cbsz;
size_t mmapsize;
size_t strbeg;
int fd;
char elsymb;
};
@ -662,19 +654,6 @@ file_reader_init(const char *fsrc)
ret->file = openfile(fsrc, "r");
if (ret->file == NULL)
err(2, NULL);
if (strcmp(fsrc, "-")) {
ret->cbsz = READ_CHUNK;
ret->buffer = sort_malloc(ret->cbsz);
ret->bsz = 0;
ret->strbeg = 0;
ret->bsz = fread(ret->buffer, 1, ret->cbsz, ret->file);
if (ret->bsz == 0) {
if (ferror(ret->file))
err(2, NULL);
}
}
}
return (ret);
@ -707,79 +686,6 @@ file_reader_readline(struct file_reader *fr)
fr->mmapptr = strend + 1;
}
}
} else if (fr->file != stdin) {
char *strend;
size_t bsz1, remsz, search_start;
search_start = 0;
remsz = 0;
strend = NULL;
if (fr->bsz > fr->strbeg)
remsz = fr->bsz - fr->strbeg;
/* line read cycle */
for (;;) {
if (remsz > search_start)
strend = memchr(fr->buffer + fr->strbeg +
search_start, fr->elsymb, remsz -
search_start);
else
strend = NULL;
if (strend)
break;
if (feof(fr->file))
break;
if (fr->bsz != fr->cbsz)
/* NOTREACHED */
err(2, "File read software error 1");
if (remsz > (READ_CHUNK >> 1)) {
search_start = fr->cbsz - fr->strbeg;
fr->cbsz += READ_CHUNK;
fr->buffer = sort_realloc(fr->buffer,
fr->cbsz);
bsz1 = fread(fr->buffer + fr->bsz, 1,
READ_CHUNK, fr->file);
if (bsz1 == 0) {
if (ferror(fr->file))
err(2, NULL);
break;
}
fr->bsz += bsz1;
remsz += bsz1;
} else {
if (remsz > 0 && fr->strbeg>0)
bcopy(fr->buffer + fr->strbeg,
fr->buffer, remsz);
fr->strbeg = 0;
search_start = remsz;
bsz1 = fread(fr->buffer + remsz, 1,
fr->cbsz - remsz, fr->file);
if (bsz1 == 0) {
if (ferror(fr->file))
err(2, NULL);
break;
}
fr->bsz = remsz + bsz1;
remsz = fr->bsz;
}
}
if (strend == NULL)
strend = fr->buffer + fr->bsz;
if ((fr->buffer + fr->strbeg <= strend) &&
(fr->strbeg < fr->bsz) && (remsz>0))
ret = bwscsbdup(fr->buffer + fr->strbeg, strend -
fr->buffer - fr->strbeg);
fr->strbeg = (strend - fr->buffer) + 1;
} else {
int delim = sort_opts_vals.zflag ? '\0' : '\n';
ssize_t len = getdelim(&fr->buffer, &fr->bsz, delim, fr->file);