aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/sed.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-12-15 19:46:16 -0600
committerRob Landley <rob@landley.net>2018-12-15 19:46:16 -0600
commiteadbf601a0c96470a6ec04aa079165a420ab8abd (patch)
tree1e228d1d01ec4f3008ef813aaab9e2f0c47ef034 /toys/posix/sed.c
parentc2a44fbe073fb13ab9407ac3a072c7b847383b62 (diff)
downloadtoybox-eadbf601a0c96470a6ec04aa079165a420ab8abd.tar.gz
Add sed -iEXT to keep backup files.
Diffstat (limited to 'toys/posix/sed.c')
-rw-r--r--toys/posix/sed.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/toys/posix/sed.c b/toys/posix/sed.c
index 03179ca2..3073a69c 100644
--- a/toys/posix/sed.c
+++ b/toys/posix/sed.c
@@ -11,7 +11,7 @@
* What's the right thing to do for -i when write fails? Skip to next?
* test '//q' with no previous regex, also repeat previous regex?
-USE_SED(NEWTOY(sed, "(help)(version)e*f*inEr[+Er]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE|TOYFLAG_NOHELP))
+USE_SED(NEWTOY(sed, "(help)(version)e*f*i:;nEr[+Er]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE|TOYFLAG_NOHELP))
config SED
bool "sed"
@@ -24,7 +24,7 @@ config SED
-e Add SCRIPT to list
-f Add contents of SCRIPT_FILE to list
- -i Edit each file in place
+ -i Edit each file in place (-iEXT keeps backup file with extension EXT)
-n No default output (use the p command to output matched lines)
-r Use extended regular expression syntax
-E Alias for -r
@@ -170,6 +170,7 @@ config SED
#include "toys.h"
GLOBALS(
+ char *i;
struct arg_list *f, *e;
// processed pattern list
@@ -640,6 +641,12 @@ static void do_sed_file(int fd, char *name)
}
do_lines(fd, sed_line);
if (FLAG(i)) {
+ if (TT.i && *TT.i) {
+ char *s = xmprintf("%s%s", name, TT.i);
+
+ xrename(name, s);
+ free(s);
+ }
replace_tempfile(-1, TT.fdout, &tmp);
TT.fdout = 1;
TT.nextline = 0;