git: 2afc73005cb2 - stable/13 - sort: unify the code to read from FILE *

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Wed, 19 Oct 2022 08:01:32 UTC
The branch stable/13 has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=2afc73005cb2c288a2445af6be10484e14ea7409

commit 2afc73005cb2c288a2445af6be10484e14ea7409
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2022-10-13 07:36:48 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2022-10-19 07:59:29 +0000

    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.
    
    (cherry picked from commit 8b9071360a312f2bb2104864af0b9905acac87bc)
---
 usr.bin/sort/file.c | 94 -----------------------------------------------------
 1 file changed, 94 deletions(-)

diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c
index b48948e1edee..437be09819b5 100644
--- a/usr.bin/sort/file.c
+++ b/usr.bin/sort/file.c
@@ -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;
 };
@@ -663,19 +655,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);
@@ -708,79 +687,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 {
 		ssize_t len;