summaryrefslogtreecommitdiff
path: root/src/sort.c
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2003-09-05 20:53:08 +0000
committerJim Meyering <jim@meyering.net>2003-09-05 20:53:08 +0000
commit1f4715e0e8d5e1bb071e2ecd6e60b68603460fd7 (patch)
treee5630790434b1a576ca51c2b204a0632bc9f401f /src/sort.c
parent098a0f56ca1c6c3b89e48e42fcb4900b3fab58bd (diff)
downloadcoreutils-1f4715e0e8d5e1bb071e2ecd6e60b68603460fd7.tar.xz
Don't ignore -S if input is a pipe. Bug report by Michael McFarland in
<http://mail.gnu.org/archive/html/bug-coreutils/2003-09/msg00008.html>. (sort_buffer_size): Omit SIZE_BOUND arg. Compute the size_bound ourselves. if an input file is a pipe and the user specified a size, use that size instead of trying to guess the pipe size. This has the beneficial side effect of avoiding the overhead of default_sort_size in that case. All callers changed. (sort): Remove static var size; now done by sort_buffer_size.
Diffstat (limited to 'src/sort.c')
-rw-r--r--src/sort.c34
1 files changed, 25 insertions, 9 deletions
diff --git a/src/sort.c b/src/sort.c
index 7a2fe54ea..22b51e197 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -684,13 +684,18 @@ default_sort_size (void)
by FPS and FILES, which are alternate paths to the same files.
NFILES gives the number of input files; NFPS may be less. Assume
that each input line requires LINE_BYTES extra bytes' worth of line
- information. Return at most SIZE_BOUND. */
+ information. Do not exceed a bound on the size: if the bound is
+ not specified by the user, use a default. */
static size_t
sort_buffer_size (FILE *const *fps, int nfps,
char *const *files, int nfiles,
- size_t line_bytes, size_t size_bound)
+ size_t line_bytes)
{
+ /* A bound on the input size. If zero, the bound hasn't been
+ determined yet. */
+ static size_t size_bound;
+
/* In the worst case, each input byte is a newline. */
size_t worst_case_per_input_byte = line_bytes + 1;
@@ -712,7 +717,23 @@ sort_buffer_size (FILE *const *fps, int nfps,
!= 0)
die (_("stat failed"), files[i]);
- file_size = S_ISREG (st.st_mode) ? st.st_size : INPUT_FILE_SIZE_GUESS;
+ if (S_ISREG (st.st_mode))
+ file_size = st.st_size;
+ else
+ {
+ /* The file has unknown size. If the user specified a sort
+ buffer size, use that; otherwise, guess the size. */
+ if (sort_size)
+ return sort_size;
+ file_size = INPUT_FILE_SIZE_GUESS;
+ }
+
+ if (! size_bound)
+ {
+ size_bound = sort_size;
+ if (! size_bound)
+ size_bound = default_sort_size ();
+ }
/* Add the amount of memory needed to represent the worst case
where the input consists entirely of newlines followed by a
@@ -1963,10 +1984,6 @@ sort (char * const *files, int nfiles, char const *output_file)
int n_temp_files = 0;
bool output_file_created = false;
- static size_t size;
- if (! size && ! (size = sort_size))
- size = default_sort_size ();
-
buf.alloc = 0;
while (nfiles)
@@ -1979,8 +1996,7 @@ sort (char * const *files, int nfiles, char const *output_file)
if (! buf.alloc)
initbuf (&buf, bytes_per_line,
- sort_buffer_size (&fp, 1, files, nfiles,
- bytes_per_line, size));
+ sort_buffer_size (&fp, 1, files, nfiles, bytes_per_line));
buf.eof = false;
files++;
nfiles--;