bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] exclude: yield proper errno on failure


From: Paul Eggert
Subject: [PATCH] exclude: yield proper errno on failure
Date: Tue, 9 Nov 2021 09:51:07 -0800

* lib/exclude.c (add_exclude_file): Do not assume that fclose
preserves errno on success.
---
 ChangeLog     |  6 ++++++
 lib/exclude.c | 23 ++++++++++-------------
 2 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bde24b951..cdfd2ddf3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-11-09  Paul Eggert  <eggert@cs.ucla.edu>
+
+       exclude: yield proper errno on failure
+       * lib/exclude.c (add_exclude_file): Do not assume that fclose
+       preserves errno on success.
+
 2021-11-07  Paul Eggert  <eggert@cs.ucla.edu>
 
        regex: break regcomp.c link with glibc
diff --git a/lib/exclude.c b/lib/exclude.c
index 417ab23d1..cff30fcb7 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;
 }
-- 
2.32.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]