diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2021-08-06 17:58:44 -0700 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2021-08-06 17:58:44 -0700 |
| commit | 887b1dc7f28944fcfab76558928e526cbf06b8a0 (patch) | |
| tree | c5178d1bfb46fbbdde786fd92debc8006d22d9d2 /bp.c | |
| parent | ac2e8f5a15a968f236387ed98e34b4a9250c9a92 (diff) | |
Properly set modifying_file/backup_file in case of errors
Diffstat (limited to 'bp.c')
| -rw-r--r-- | bp.c | 9 |
1 files changed, 8 insertions, 1 deletions
@@ -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); |
