diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2005-04-11 20:13:26 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2005-04-11 20:13:26 +0000 |
commit | 786ebb2ceca72f69aa2de701671fb41f53cb1489 (patch) | |
tree | 3be2fca49a61c125e441be3ea00f08110be50dc7 /src/uniq.c | |
parent | 45e6c262b923608e06f900acb62237cd5e2e869d (diff) | |
download | coreutils-786ebb2ceca72f69aa2de701671fb41f53cb1489.tar.xz |
Include stdio_safer.h.
(check_file): Don't assume fopen cannot return stdin or stdout.
Diffstat (limited to 'src/uniq.c')
-rw-r--r-- | src/uniq.c | 46 |
1 files changed, 20 insertions, 26 deletions
diff --git a/src/uniq.c b/src/uniq.c index 27322e93e..a02647091 100644 --- a/src/uniq.c +++ b/src/uniq.c @@ -30,6 +30,7 @@ #include "hard-locale.h" #include "posixver.h" #include "quote.h" +#include "stdio-safer.h" #include "xmemcoll.h" #include "xstrtol.h" #include "memcasecmp.h" @@ -261,24 +262,22 @@ writeline (struct linebuffer const *line, FILE *stream, static void check_file (const char *infile, const char *outfile) { - FILE *istream; FILE *ostream; struct linebuffer lb1, lb2; struct linebuffer *thisline, *prevline; + bool is_stdin = STREQ (infile, "-"); + bool is_stdout = STREQ (outfile, "-"); - if (STREQ (infile, "-")) - istream = stdin; - else - istream = fopen (infile, "r"); - if (istream == NULL) + if (!is_stdin && ! freopen (infile, "r", stdin)) error (EXIT_FAILURE, errno, "%s", infile); - - if (STREQ (outfile, "-")) + if (is_stdout) ostream = stdout; else - ostream = fopen (outfile, "w"); - if (ostream == NULL) - error (EXIT_FAILURE, errno, "%s", outfile); + { + ostream = fopen_safer (outfile, "w"); + if (! ostream) + error (EXIT_FAILURE, errno, "%s", outfile); + } thisline = &lb1; prevline = &lb2; @@ -298,11 +297,11 @@ check_file (const char *infile, const char *outfile) char *prevfield IF_LINT (= NULL); size_t prevlen IF_LINT (= 0); - while (!feof (istream)) + while (!feof (stdin)) { char *thisfield; size_t thislen; - if (readlinebuffer (thisline, istream) == 0) + if (readlinebuffer (thisline, stdin) == 0) break; thisfield = find_field (thisline); thislen = thisline->length - 1 - (thisfield - thisline->buffer); @@ -325,19 +324,19 @@ check_file (const char *infile, const char *outfile) uintmax_t match_count = 0; bool first_delimiter = true; - if (readlinebuffer (prevline, istream) == 0) + if (readlinebuffer (prevline, stdin) == 0) goto closefiles; prevfield = find_field (prevline); prevlen = prevline->length - 1 - (prevfield - prevline->buffer); - while (!feof (istream)) + while (!feof (stdin)) { bool match; char *thisfield; size_t thislen; - if (readlinebuffer (thisline, istream) == 0) + if (readlinebuffer (thisline, stdin) == 0) { - if (ferror (istream)) + if (ferror (stdin)) goto closefiles; break; } @@ -384,18 +383,13 @@ check_file (const char *infile, const char *outfile) } closefiles: - if (ferror (istream) || fclose (istream) == EOF) - error (EXIT_FAILURE, errno, _("error reading %s"), infile); + if (ferror (stdin) || fclose (stdin) != 0) + error (EXIT_FAILURE, 0, _("error reading %s"), infile); /* Check for errors and close ostream only if it's not stdout -- stdout is handled via the atexit-invoked close_stdout function. */ - if (ostream != stdout) - { - if (ferror (ostream)) - error (EXIT_FAILURE, 0, _("error writing %s"), outfile); - if (ostream != stdout && fclose (ostream) != 0) - error (EXIT_FAILURE, errno, _("error writing %s"), outfile); - } + if (!is_stdout && (ferror (ostream) || fclose (ostream) != 0)) + error (EXIT_FAILURE, 0, _("error writing %s"), outfile); free (lb1.buffer); free (lb2.buffer); |