bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: sort -S 120M ignored w/ pipes


From: Paul Eggert
Subject: Re: sort -S 120M ignored w/ pipes
Date: 04 Sep 2003 14:01:44 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3

Michael McFarland <address@hidden> writes:

> if I call 'cat in.txt | sort -S 120M > out.txt', sort seems to
> ignore the '-S' parameter and behave exactly as if it wasn't
> provided.

Thanks for reporting that bug.  Here is a proposed patch.

2003-09-04  Paul Eggert  <address@hidden>

        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>.

        * src/sort.c (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.

--- sort.c.~1.267.~     Mon Aug  4 01:55:44 2003
+++ sort.c      Thu Sep  4 13:44:07 2003
@@ -676,13 +676,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;
 
@@ -704,7 +709,23 @@ sort_buffer_size (FILE *const *fps, int 
          != 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
@@ -1955,10 +1976,6 @@ sort (char * const *files, int nfiles, c
   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)
@@ -1971,8 +1988,7 @@ sort (char * const *files, int nfiles, c
 
       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--;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]