[hackers] [sbase] Handle explicitly the case of line 0 || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Mon, 11 Jan 2016 16:38:03 +0100 (CET)

commit 78fd6ff239f4b2fd626a1484e61dfa0ba3a8bdfa
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Wed Jan 6 22:04:29 2016 +0100
Commit: sin <sin_AT_2f30.org>
CommitDate: Mon Jan 11 15:37:58 2016 +0000

    Handle explicitly the case of line 0
    
    Line 0 is a special line added to allow operations with
    empty buffers, and we have to ensure that it is not going
    to match any regular expression. The code was written in
    a way that this case was handle implicitily, but this
    solution was working only for the first file loaded in
    ed, while the second file loaded in ed got a line with
    a dirty seek field. This solution check explicitily
    against invalid lines passed to makeline(), which
    allows to simplify the common case.

diff --git a/ed.c b/ed.c
index 0b03fd1..e98acda 100644
--- a/ed.c
+++ b/ed.c
_AT_@ -167,20 +167,20 @@ makeline(char *s, int *off)
         }
         lp = zero + lastidx;
 
- while ((c = *s) && *s != '\n')
- ++s;
- if (c == '\n')
- ++s;
- len = s - begin;
+ if (!s) {
+ lp->seek = -1;
+ len = 0;
+ } else {
+ while ((c = *s++) != '\n')
+ /* nothing */;
+ len = s - begin;
+ if ((lp->seek = lseek(scratch, 0, SEEK_END)) < 0 ||
+ write(scratch, begin, len) < 0) {
+ error("input/output error");
+ }
+ }
         if (off)
                 *off = len;
-
- if (len > 0)
- if ((lp->seek = lseek(scratch, 0, SEEK_END)) < 0 ||
- write(scratch, begin, len) < 0) {
- error("input/output error");
- }
-
         ++lastidx;
         return lp - zero;
 }
_AT_@ -208,8 +208,11 @@ gettxt(int line)
         char *p;
 
         lp = zero + getindex(line);
- off = lp->seek;
         sizetxt = 0;
+ off = lp->seek;
+
+ if (off == (off_t) -1)
+ return text = addchar('\0', text, &memtxt, &sizetxt);
 
 repeat:
         if (!csize || off < lasto || off - lasto >= csize) {
_AT_@ -339,7 +342,7 @@ setscratch()
                 error("scratch filename too long");
         if ((scratch = mkstemp(tmpname)) < 0)
                 error("failed to create scratch file");
- if ((k = makeline("", NULL)))
+ if ((k = makeline(NULL, NULL)))
                 error("input/output error in scratch file");
         relink(k, k, k, k);
         clearundo();
Received on Mon Jan 11 2016 - 16:38:03 CET

This archive was generated by hypermail 2.3.0 : Mon Jan 11 2016 - 16:48:16 CET