summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2021-11-09 09:50:28 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2021-11-09 09:50:53 -0800
commita840d0ac60442e7e195d077edc0ac7585c7260f4 (patch)
tree953d964e78d5876f93701b9a5107a22639a86f32 /lib
parente20b35c9167ff7c051d0314b356d7e7a304f828f (diff)
downloadgnulib-a840d0ac60442e7e195d077edc0ac7585c7260f4.tar.gz
exclude: yield proper errno on failure
* lib/exclude.c (add_exclude_file): Do not assume that fclose preserves errno on success.
Diffstat (limited to 'lib')
-rw-r--r--lib/exclude.c23
1 files changed, 10 insertions, 13 deletions
diff --git a/lib/exclude.c b/lib/exclude.c
index 417ab23d1d..cff30fcb7e 100644
--- a/lib/exclude.c
+++ b/lib/exclude.c
@@ -602,7 +602,7 @@ add_exclude (struct exclude *ex, char const *pattern, int options)
/* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
OPTIONS. LINE_END terminates each pattern in the file. If
LINE_END is a space character, ignore trailing spaces and empty
- lines in FP. Return -1 on failure, 0 on success. */
+ lines in FP. Return -1 (setting errno) on failure, 0 on success. */
int
add_exclude_fp (void (*add_func) (struct exclude *, char const *, int, void *),
@@ -674,19 +674,16 @@ add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
struct exclude *ex, char const *file_name, int options,
char line_end)
{
- bool use_stdin = file_name[0] == '-' && !file_name[1];
- FILE *in;
- int rc = 0;
+ if (strcmp (file_name, "-") == 0)
+ return add_exclude_fp (call_addfn, ex, stdin, options, line_end, add_func);
- if (use_stdin)
- in = stdin;
- else if (! (in = fopen (file_name, "re")))
+ FILE *in = fopen (file_name, "re");
+ if (!in)
return -1;
-
- rc = add_exclude_fp (call_addfn, ex, in, options, line_end, &add_func);
-
- if (!use_stdin && fclose (in) != 0)
- rc = -1;
-
+ int rc = add_exclude_fp (call_addfn, ex, in, options, line_end, add_func);
+ int e = errno;
+ if (fclose (in) != 0)
+ return -1;
+ errno = e;
return rc;
}