#include "human.h"
#include "memcoll.h"
#include "physmem.h"
+#include "stdio-safer.h"
#include "xalloc.h"
#include "xstrtol.h"
if (STREQ (file, "-"))
{
+ have_read_stdin = 1;
fp = stdin;
}
else
{
- if ((fp = fopen (file, how)) == NULL)
+ if ((fp = fopen_safer (file, how)) == NULL)
{
error (0, errno, "%s", file);
cleanup ();
}
}
- if (fp == stdin)
- have_read_stdin = 1;
return fp;
}
if (feof (fp))
clearerr (fp);
}
- else if (fp == stdout)
- {
- if (fflush (fp) != 0)
- {
- error (0, errno, _("flushing file"));
- cleanup ();
- exit (SORT_FAILURE);
- }
- }
else
{
if (fclose (fp) != 0)
sort (files, nfiles, ofp, outfile);
cleanup ();
- /* If we wait for the implicit flush on exit, and the parent process
- has closed stdout (e.g., exec >&- in a shell), then the output file
- winds up empty. I don't understand why. This is under SunOS,
- Solaris, Ultrix, and Irix. This premature fflush makes the output
- reappear. --karl@cs.umb.edu */
- if (fflush (ofp) < 0)
+ if (fclose (ofp) != 0)
error (SORT_FAILURE, errno, _("%s: write error"), outfile);
if (have_read_stdin && fclose (stdin) == EOF)