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;
}
}
- fileptr[files_open] = NULL;
-
if (opened_stdin && have_read_stdin)
error (EXIT_FAILURE, 0, _("standard input is closed"));
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)
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]))
{
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? */
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);
}
}
free (fileptr);
- free (closed);
free (delbuf);
return ok;
}