changeset 1654:c8be6e37a616

sed bugfix: N or n at end of script would save the terminating NULL as the resume position, so the script would restart from beginning.
author Rob Landley <rob@landley.net>
date Tue, 13 Jan 2015 04:28:19 -0600
parents 58cb2d7bd461
children 45a612fb0ce6
files toys/posix/sed.c
diffstat 1 files changed, 5 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/toys/posix/sed.c	Tue Jan 13 03:35:37 2015 -0600
+++ b/toys/posix/sed.c	Tue Jan 13 04:28:19 2015 -0600
@@ -297,7 +297,9 @@
   if (line[len-1] == '\n') line[--len] = eol++;
   TT.count++;
 
-  logrus = TT.restart ? TT.restart : (void *)TT.pattern;
+  // The restart-1 is because we added one to make sure it wasn't NULL,
+  // otherwise N as last command would restart script
+  logrus = TT.restart ? ((struct step *)TT.restart)-1 : (void *)TT.pattern;
   TT.restart = 0;
 
   while (logrus) {
@@ -452,14 +454,14 @@
       toybuf[off++] = '$';
       emit(toybuf, off, 1);
     } else if (c=='n') {
-      TT.restart = logrus->next;
+      TT.restart = logrus->next+1;
 
       break;
     } else if (c=='N') {
       // Can't just grab next line because we could have multiple N and
       // we need to actually read ahead to get N;$p EOF detection right.
       if (pline) {
-        TT.restart = logrus->next;
+        TT.restart = logrus->next+1;
         extend_string(&line, TT.nextline, len, -TT.nextlen);
         free(TT.nextline);
         TT.nextline = line;