logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>

uniq-fopen.patch (2141B)


  1. SPDX-FileCopyrightText: 2005 Paul Eggert <eggert@cs.ucla.edu>
  2. SPDX-FileCopyrightText: 2023 Emily Trau <emily@downunderctf.com>
  3. SPDX-License-Identifier: GPL-2.0-or-later
  4. uniq: don't assume fopen cannot return stdin or stdout.
  5. Backport of https://git.savannah.gnu.org/cgit/coreutils.git/commit/?id=786ebb2ceca72f69aa2de701671fb41f53cb1489
  6. --- coreutils-5.0/src/uniq.c
  7. +++ coreutils-5.0/src/uniq.c
  8. @@ -30,6 +30,7 @@
  9. #include "error.h"
  10. #include "hard-locale.h"
  11. #include "posixver.h"
  12. +#include "stdio-safer.h"
  13. #include "xmemcoll.h"
  14. #include "xstrtol.h"
  15. #include "memcasecmp.h"
  16. @@ -267,20 +268,26 @@ check_file (const char *infile, const char *outfile)
  17. FILE *ostream;
  18. struct linebuffer lb1, lb2;
  19. struct linebuffer *thisline, *prevline;
  20. + bool is_stdin = STREQ (infile, "-");
  21. + bool is_stdout = STREQ (outfile, "-");
  22. - if (STREQ (infile, "-"))
  23. + if (is_stdin)
  24. istream = stdin;
  25. else
  26. - istream = fopen (infile, "r");
  27. - if (istream == NULL)
  28. - error (EXIT_FAILURE, errno, "%s", infile);
  29. + {
  30. + istream = fopen_safer (infile, "r");
  31. + if (! istream)
  32. + error (EXIT_FAILURE, errno, "%s", infile);
  33. + }
  34. - if (STREQ (outfile, "-"))
  35. + if (is_stdout)
  36. ostream = stdout;
  37. else
  38. - ostream = fopen (outfile, "w");
  39. - if (ostream == NULL)
  40. - error (EXIT_FAILURE, errno, "%s", outfile);
  41. + {
  42. + ostream = fopen_safer (outfile, "w");
  43. + if (! ostream)
  44. + error (EXIT_FAILURE, errno, "%s", outfile);
  45. + }
  46. thisline = &lb1;
  47. prevline = &lb2;
  48. @@ -377,12 +384,12 @@ check_file (const char *infile, const char *outfile)
  49. }
  50. closefiles:
  51. - if (ferror (istream) || fclose (istream) == EOF)
  52. + if (!is_stdin && (ferror (istream) || fclose (istream) != 0))
  53. error (EXIT_FAILURE, errno, _("error reading %s"), infile);
  54. /* Close ostream only if it's not stdout -- the latter is closed
  55. via the atexit-invoked close_stdout. */
  56. - if (ostream != stdout && (ferror (ostream) || fclose (ostream) == EOF))
  57. + if (!is_stdout && (ferror (ostream) || fclose (ostream) != 0))
  58. error (EXIT_FAILURE, errno, _("error writing %s"), outfile);
  59. free (lb1.buffer);