Add handling for any nil-length string passed to -i for the backup extension.
Add a note that this is kinda-sorta dangerous to the manual page.
This commit is contained in:
+14
-2
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user