diff options
author | Jim Meyering <jim@meyering.net> | 2005-08-12 08:06:28 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2005-08-12 08:06:28 +0000 |
commit | f6ab92093a0e199da1fa0085829a2427709ed79d (patch) | |
tree | 1559d7d5a3146432838d2d0a6bcd2bf9f7c60650 | |
parent | 75ae174b6a070604d0c799a58879896da4ec5f40 (diff) | |
download | coreutils-f6ab92093a0e199da1fa0085829a2427709ed79d.tar.xz |
Add bulletproofing in case stdin is closed.
(have_read_stdin): Remove global variable.
(dc_parse_stream): Always use stdin (freopen, if needed) rather
than sometimes using fopen to get a new file descriptor.
Call fclose unconditionally.
(main): Don't close stdin here. If needed, now it's already done
by dc_parse_stream.
-rw-r--r-- | src/dircolors.c | 28 |
1 files changed, 5 insertions, 23 deletions
diff --git a/src/dircolors.c b/src/dircolors.c index 3598c79d6..02d82bf89 100644 --- a/src/dircolors.c +++ b/src/dircolors.c @@ -61,9 +61,6 @@ enum Shell_syntax variable. */ static struct obstack lsc_obstack; -/* True if the input file was the standard input. */ -static bool have_read_stdin; - /* FIXME: associate with ls_codes? */ static const char *const slack_codes[] = { @@ -373,28 +370,17 @@ dc_parse_stream (FILE *fp, const char *filename) static bool dc_parse_file (const char *filename) { - FILE *fp; bool ok; - bool is_stdin = STREQ (filename, "-"); - if (is_stdin) + if (! STREQ (filename, "-") && freopen (filename, "r", stdin) == NULL) { - have_read_stdin = true; - fp = stdin; - } - else - { - fp = fopen (filename, "r"); - if (fp == NULL) - { - error (0, errno, "%s", quote (filename)); - return false; - } + error (0, errno, "%s", filename); + return false; } - ok = dc_parse_stream (fp, filename); + ok = dc_parse_stream (stdin, filename); - if (!is_stdin && fclose (fp) != 0) + if (fclose (stdin) != 0) { error (0, errno, "%s", quote (filename)); return false; @@ -516,9 +502,5 @@ to select a shell syntax are mutually exclusive")); } } - - if (have_read_stdin && fclose (stdin) == EOF) - error (EXIT_FAILURE, errno, _("standard input")); - exit (ok ? EXIT_SUCCESS : EXIT_FAILURE); } |