]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Don't ignore -S if input is a pipe. Bug report by Michael McFarland in
authorJim Meyering <jim@meyering.net>
Fri, 5 Sep 2003 20:53:08 +0000 (20:53 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 5 Sep 2003 20:53:08 +0000 (20:53 +0000)
<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.

src/sort.c

index 7a2fe54ea9d827244ec5a469d8a59b263428828b..22b51e197bf9b49287687780ffe6750e936e8347 100644 (file)
@@ -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--;