aboutsummaryrefslogtreecommitdiff
path: root/bp.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-08-06 17:58:44 -0700
committerBruce Hill <bruce@bruce-hill.com>2021-08-06 17:58:44 -0700
commit887b1dc7f28944fcfab76558928e526cbf06b8a0 (patch)
treec5178d1bfb46fbbdde786fd92debc8006d22d9d2 /bp.c
parentac2e8f5a15a968f236387ed98e34b4a9250c9a92 (diff)
Properly set modifying_file/backup_file in case of errors
Diffstat (limited to 'bp.c')
-rw-r--r--bp.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/bp.c b/bp.c
index 3195885..8f805ef 100644
--- a/bp.c
+++ b/bp.c
@@ -290,7 +290,14 @@ static int process_file(def_t *defs, const char *filename, pat_t *pattern)
f = copy;
}
FILE *out = fopen(filename, "w");
- matches += print_matches(out, defs, f, pattern);
+ // Set these temporary values in case the program crashes while in the
+ // middle of inplace modifying a file. If that happens, these variables
+ // are used to restore the original file contents.
+ modifying_file = out; backup_file = f;
+ {
+ matches += print_matches(out, defs, f, pattern);
+ }
+ modifying_file = NULL; backup_file = NULL;
fclose(out);
} else {
matches += print_matches(stdout, defs, f, pattern);