aboutsummaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorJilles Tjoelker <jilles@FreeBSD.org>2011-01-08 00:03:18 +0000
committerJilles Tjoelker <jilles@FreeBSD.org>2011-01-08 00:03:18 +0000
commit919e14f01e89318aefdb8e282e9c789aa6fdd486 (patch)
treeb86c1734dc9ae41a27d4271519188d0056277885 /usr.bin
parent462e3e88b3e55eaa4328b31d5cb0761b888f29b8 (diff)
Notes
Diffstat (limited to 'usr.bin')
-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 8d4fe95a33d8..049d2ea1e830 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;