summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--usr.bin/sed/main.c16
-rw-r--r--usr.bin/sed/sed.17
2 files changed, 21 insertions, 2 deletions
diff --git a/usr.bin/sed/main.c b/usr.bin/sed/main.c
index b4c0916..812f62e 100644
--- a/usr.bin/sed/main.c
+++ b/usr.bin/sed/main.c
@@ -320,6 +320,8 @@ mf_fgets(sp, spflag)
fname = files->fname;
if ((f = fopen(fname, "r")) == NULL)
err(1, "%s", fname);
+ if (inplace != NULL && *inplace == '\0')
+ unlink(fname);
}
if ((c = getc(f)) != EOF) {
(void)ungetc(c, f);
@@ -366,6 +368,8 @@ mf_fgets(sp, spflag)
fname = files->fname;
if ((f = fopen(fname, "r")) == NULL)
err(1, "%s", fname);
+ if (inplace != NULL && *inplace == '\0')
+ unlink(fname);
}
}
(void)ungetc(c, f);
@@ -427,8 +431,16 @@ inplace_edit(filename)
return -1;
}
- strlcpy(backup, *filename, MAXPATHLEN);
- strlcat(backup, inplace, MAXPATHLEN);
+ if (*inplace == '\0') {
+ char template[] = "/tmp/sed.XXXXXXXXXX";
+
+ if (mktemp(template) == NULL)
+ err(1, "mktemp");
+ strlcpy(backup, template, MAXPATHLEN);
+ } else {
+ strlcpy(backup, *filename, MAXPATHLEN);
+ strlcat(backup, inplace, MAXPATHLEN);
+ }
input = open(*filename, O_RDONLY);
if (input == -1)
diff --git a/usr.bin/sed/sed.1 b/usr.bin/sed/sed.1
index 4005463..e7bd8b2 100644
--- a/usr.bin/sed/sed.1
+++ b/usr.bin/sed/sed.1
@@ -102,6 +102,13 @@ The editing commands should each be listed on a separate line.
.It Fl i Ar extension
Edit files in-place, saving backups with the specified
.Ar extension .
+If a zero-length
+.Ar extension
+is given, no backup will be saved.
+It is not recommended to give a zero-length
+.Ar extension
+when in-place editing files, as you risk corruption or partial content
+in situations where disk space is exhausted, etc.
.It Fl n
By default, each line of input is echoed to the standard output after
all of the commands have been applied to it.
OpenPOWER on IntegriCloud