From: Jim Meyering Date: Tue, 22 Jan 2002 10:51:53 +0000 (+0000) Subject: (swallow_file_in_memory): Work even if `open' returns 0. X-Git-Tag: TEXTUTILS-2_0_20~18 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3da062869b0fabb0f02375a1ece02c6d410adc05;p=thirdparty%2Fcoreutils.git (swallow_file_in_memory): Work even if `open' returns 0. Check for `close' error. --- diff --git a/src/ptx.c b/src/ptx.c index 1955cd0f76..9b101d215d 100644 --- a/src/ptx.c +++ b/src/ptx.c @@ -524,9 +524,9 @@ swallow_file_in_memory (const char *file_name, BLOCK *block) /* As special cases, a file name which is NULL or "-" indicates standard input, which is already opened. In all other cases, open the file from its name. */ - - if (!file_name || !*file_name || strcmp (file_name, "-") == 0) - file_handle = fileno (stdin); + bool using_stdin = !file_name || !*file_name || strcmp (file_name, "-") == 0; + if (using_stdin) + file_handle = STDIN_FILENO; else if ((file_handle = open (file_name, O_RDONLY)) < 0) error (EXIT_FAILURE, errno, "%s", file_name); @@ -593,8 +593,8 @@ swallow_file_in_memory (const char *file_name, BLOCK *block) /* Close the file, but only if it was not the standard input. */ - if (file_handle != fileno (stdin)) - close (file_handle); + if (! using_stdin && close (file_handle) != 0) + error (EXIT_FAILURE, errno, "%s", file_name); } /* Sort and search routines. */