summaryrefslogtreecommitdiffstats
path: root/usr.bin/sed/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'usr.bin/sed/main.c')
-rw-r--r--usr.bin/sed/main.c25
1 files changed, 21 insertions, 4 deletions
diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c
index 8d4fe95..049d2ea 100644
--- a/usr.bin/sed/main.c
+++ b/usr.bin/sed/main.c
@@ -338,18 +338,35 @@ mf_fgets(SPACE *sp, enum e_spflag spflag)
if (infile != NULL) {
fclose(infile);
if (*oldfname != '\0') {
- if (rename(fname, oldfname) != 0) {
+ /* if there was a backup file, remove it */
+ unlink(oldfname);
+ /*
+ * Backup the original. Note that hard links
+ * are not supported on all filesystems.
+ */
+ if ((link(fname, oldfname) != 0) &&
+ (rename(fname, oldfname) != 0)) {
warn("rename()");
- unlink(tmpfname);
+ if (*tmpfname)
+ unlink(tmpfname);
exit(1);
}
*oldfname = '\0';
}
if (*tmpfname != '\0') {
if (outfile != NULL && outfile != stdout)
- fclose(outfile);
+ if (fclose(outfile) != 0) {
+ warn("fclose()");
+ unlink(tmpfname);
+ exit(1);
+ }
outfile = NULL;
- rename(tmpfname, fname);
+ if (rename(tmpfname, fname) != 0) {
+ /* this should not happen really! */
+ warn("rename()");
+ unlink(tmpfname);
+ exit(1);
+ }
*tmpfname = '\0';
}
outfname = NULL;
OpenPOWER on IntegriCloud