]> Savannah Git Hosting - gnulib.git/commitdiff
exclude: yield proper errno on failure
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 9 Nov 2021 17:50:28 +0000 (09:50 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 9 Nov 2021 17:50:53 +0000 (09:50 -0800)
* lib/exclude.c (add_exclude_file): Do not assume that fclose
preserves errno on success.

ChangeLog
lib/exclude.c

index bde24b95129ac273d99a78c978dc1acbc8beeab9..cdfd2ddf35cc6de49a7e91a2df74053ba3b26096 100644 (file)
--- 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
index 417ab23d1dc59ae440d2496bbf973328f7483ff5..cff30fcb7efd84232faf0a352e8e9a2cd4b348c8 100644 (file)
@@ -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;
 }