diff --git a/usr.bin/sort/bwstring.c b/usr.bin/sort/bwstring.c index 2f2737e94314..52173002986f 100644 --- a/usr.bin/sort/bwstring.c +++ b/usr.bin/sort/bwstring.c @@ -470,115 +470,6 @@ bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended) } } -/* - * Allocate and read a binary string from file. - * The strings are nl-ended or zero-ended, depending on the sort setting. - */ -struct bwstring * -bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb) -{ - wint_t eols; - - eols = zero_ended ? btowc('\0') : btowc('\n'); - - if (!zero_ended && (mb_cur_max > 1)) { - wchar_t *ret; - - ret = fgetwln(f, len); - - if (ret == NULL) { - if (!feof(f)) - err(2, NULL); - return (NULL); - } - if (*len > 0) { - if (ret[*len - 1] == (wchar_t)eols) - --(*len); - } - return (bwssbdup(ret, *len)); - - } else if (!zero_ended && (mb_cur_max == 1)) { - char *ret; - - ret = fgetln(f, len); - - if (ret == NULL) { - if (!feof(f)) - err(2, NULL); - return (NULL); - } - if (*len > 0) { - if (ret[*len - 1] == '\n') - --(*len); - } - return (bwscsbdup((unsigned char *)ret, *len)); - - } else { - *len = 0; - - if (feof(f)) - return (NULL); - - if (2 >= rb->fgetwln_z_buffer_size) { - rb->fgetwln_z_buffer_size += 256; - rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, - sizeof(wchar_t) * rb->fgetwln_z_buffer_size); - } - rb->fgetwln_z_buffer[*len] = 0; - - if (mb_cur_max == 1) - while (!feof(f)) { - int c; - - c = fgetc(f); - - if (c == EOF) { - if (*len == 0) - return (NULL); - goto line_read_done; - } - if (c == eols) - goto line_read_done; - - if (*len + 1 >= rb->fgetwln_z_buffer_size) { - rb->fgetwln_z_buffer_size += 256; - rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, - SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); - } - - rb->fgetwln_z_buffer[*len] = c; - rb->fgetwln_z_buffer[++(*len)] = 0; - } - else - while (!feof(f)) { - wint_t c; - - c = fgetwc(f); - - if (c == WEOF) { - if (*len == 0) - return (NULL); - goto line_read_done; - } - if (c == eols) - goto line_read_done; - - if (*len + 1 >= rb->fgetwln_z_buffer_size) { - rb->fgetwln_z_buffer_size += 256; - rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, - SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); - } - - rb->fgetwln_z_buffer[*len] = c; - rb->fgetwln_z_buffer[++(*len)] = 0; - } - -line_read_done: - /* we do not count the last 0 */ - return (bwssbdup(rb->fgetwln_z_buffer, *len)); - } -} - int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len) diff --git a/usr.bin/sort/file.c b/usr.bin/sort/file.c index 1fb02642b5c9..ffcd71e9f991 100644 --- a/usr.bin/sort/file.c +++ b/usr.bin/sort/file.c @@ -75,7 +75,7 @@ struct file_reader struct reader_buffer rb; FILE *file; char *fname; - unsigned char *buffer; + char *buffer; unsigned char *mmapaddr; unsigned char *mmapptr; size_t bsz; @@ -713,7 +713,7 @@ file_reader_readline(struct file_reader *fr) } } else if (fr->file != stdin) { - unsigned char *strend; + char *strend; size_t bsz1, remsz, search_start; search_start = 0; @@ -785,10 +785,16 @@ file_reader_readline(struct file_reader *fr) fr->strbeg = (strend - fr->buffer) + 1; } else { - size_t len = 0; - - ret = bwsfgetln(fr->file, &len, sort_opts_vals.zflag, - &(fr->rb)); + int delim = sort_opts_vals.zflag ? '\0' : '\n'; + ssize_t len = getdelim(&fr->buffer, &fr->bsz, delim, fr->file); + if (len < 0) { + if (!feof(fr->file)) + err(2, NULL); + return (NULL); + } + if (len > 0 && fr->buffer[len - 1] == delim) + len--; + ret = bwscsbdup(fr->buffer, len); } return (ret);