From 98ad58770935ae5bc084e196871a3c7d0741c623 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Mon, 14 Jan 2002 08:36:48 +0000 Subject: (mergefps): Fix bug when eliminating empty files. --- src/sort.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/sort.c b/src/sort.c index 3b3bbccb7..52942279d 100644 --- a/src/sort.c +++ b/src/sort.c @@ -1648,28 +1648,28 @@ mergefps (char **files, register int nfiles, saved.text = NULL; /* Read initial lines from each input file. */ - for (i = 0; i < nfiles; ++i) + for (i = 0; i < nfiles; ) { fps[i] = xfopen (files[i], "r"); initbuf (&buffer[i], sizeof (struct line), MAX (merge_buffer_size, sort_size / nfiles)); - /* If a file is empty, eliminate it from future consideration. */ - while (i < nfiles && !fillbuf (&buffer[i], fps[i], files[i])) + if (fillbuf (&buffer[i], fps[i], files[i])) + { + struct line const *linelim = buffer_linelim (&buffer[i]); + cur[i] = linelim - 1; + base[i] = linelim - buffer[i].nlines; + i++; + } + else { + /* fps[i] is empty; eliminate it from future consideration. */ xfclose (fps[i], files[i]); zaptemp (files[i]); + free (buffer[i].buf); --nfiles; for (j = i; j < nfiles; ++j) files[j] = files[j + 1]; } - if (i == nfiles) - free (buffer[i].buf); - else - { - struct line const *linelim = buffer_linelim (&buffer[i]); - cur[i] = linelim - 1; - base[i] = linelim - buffer[i].nlines; - } } if (! ofp) -- cgit v1.2.3-54-g00ecf