]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(paste_parallel): Improve replacement for ENDLIST and CLOSED.
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 3 Sep 2004 01:23:15 +0000 (01:23 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 3 Sep 2004 01:23:15 +0000 (01:23 +0000)
src/paste.c

index 7d0f615b52601e4345141e34e4e54ec439407c3f..ad3f4425ded442a6aa42e5bc33aefcecea0f1c6a 100644 (file)
@@ -148,12 +148,10 @@ paste_parallel (size_t nfiles, char **fnamptr)
      store the delimiters for closed files. */
   char *delbuf = xmalloc (nfiles + 2);
 
-  /* Streams open to the files to process.  */
+  /* Streams open to the files to process; NULL if the corresponding
+     stream is closed.  */
   FILE **fileptr = xnmalloc (nfiles + 1, sizeof *fileptr);
 
-  /* Which of these streams are closed.  */
-  bool *closed = xcalloc (nfiles, sizeof *closed);
-
   /* Number of files still open to process.  */
   size_t files_open;
 
@@ -181,8 +179,6 @@ paste_parallel (size_t nfiles, char **fnamptr)
        }
     }
 
-  fileptr[files_open] = NULL;
-
   if (opened_stdin && have_read_stdin)
     error (EXIT_FAILURE, 0, _("standard input is closed"));
 
@@ -198,11 +194,12 @@ paste_parallel (size_t nfiles, char **fnamptr)
       size_t delims_saved = 0; /* Number of delims saved in `delbuf'. */
       size_t i;
 
-      for (i = 0; fileptr[i] && files_open; i++)
+      for (i = 0; i < nfiles && files_open; i++)
        {
          int chr IF_LINT (= 0);        /* Input character. */
          size_t line_length = 0;       /* Number of chars in line. */
-         if (! closed[i])
+
+         if (fileptr[i])
            {
              chr = getc (fileptr[i]);
              if (chr != EOF && delims_saved)
@@ -224,8 +221,8 @@ paste_parallel (size_t nfiles, char **fnamptr)
          if (line_length == 0)
            {
              /* EOF, read error, or closed file.
-                If an EOF or error, close the file and mark it in the list. */
-             if (! closed[i])
+                If an EOF or error, close the file */
+             if (fileptr[i])
                {
                  if (ferror (fileptr[i]))
                    {
@@ -240,11 +237,11 @@ paste_parallel (size_t nfiles, char **fnamptr)
                      ok = false;
                    }
 
-                 closed[i] = true;
+                 fileptr[i] = NULL;
                  files_open--;
                }
 
-             if (! fileptr[i + 1])
+             if (i + 1 == nfiles)
                {
                  /* End of this output line.
                     Is this the end of the whole thing? */
@@ -275,7 +272,7 @@ paste_parallel (size_t nfiles, char **fnamptr)
              somedone = true;
 
              /* Except for last file, replace last newline with delim. */
-             if (fileptr[i + 1])
+             if (i + 1 != nfiles)
                {
                  if (chr != '\n' && chr != EOF)
                    putc (chr, stdout);
@@ -295,7 +292,6 @@ paste_parallel (size_t nfiles, char **fnamptr)
        }
     }
   free (fileptr);
-  free (closed);
   free (delbuf);
   return ok;
 }