t9p: parse lines in rdwr command - plan9port - [fork] Plan 9 from user space
 (HTM) git clone git://src.adamsgaard.dk/plan9port
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a72478870ae66b7ac1e73b1d22b578cd31852f33
 (DIR) parent 36cd4c58c1346375b98f517fb8568be5bb47618d
 (HTM) Author: David Arroyo <david@aqwari.net>
       Date:   Sun, 31 Jan 2021 00:51:32 -0500
       
       9p: parse lines in rdwr command
       
       Use bio(3) to read at most one line of input per iteration, even
       if there is more than one line available in the input buffer. This
       makes it easier to interact with line-oriented ctl files like that of
       factotum(4) from shell scripts, without the need to control when
       and how much data is flushed to a pipe.
       
       Diffstat:
         M src/cmd/9p.c                        |      14 +++++++++-----
       
       1 file changed, 9 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/src/cmd/9p.c b/src/cmd/9p.c
       t@@ -302,8 +302,10 @@ void
        xrdwr(int argc, char **argv)
        {
                char buf[4096];
       +        char *p;
                int n;
                CFid *fid;
       +        Biobuf *b;
        
                ARGBEGIN{
                default:
       t@@ -313,6 +315,8 @@ xrdwr(int argc, char **argv)
                if(argc != 1)
                        usage();
        
       +        if((b = Bfdopen(0, OREAD)) == nil)
       +                sysfatal("out of memory");
                fid = xopen(argv[0], ORDWR);
                for(;;){
                        fsseek(fid, 0, 0);
       t@@ -322,15 +326,15 @@ xrdwr(int argc, char **argv)
                                if(write(1, buf, n) < 0 || write(1, "\n", 1) < 0)
                                        sysfatal("write error: %r");
                        }
       -                n = read(0, buf, sizeof buf);
       -                if(n <= 0)
       +                if((p = Brdstr(b, '\n', 1)) == nil)
                                break;
       -                if(buf[n-1] == '\n')
       -                        n--;
       -                if(fswrite(fid, buf, n) != n)
       +                n = strlen(p);
       +                if(fswrite(fid, p, n) != n)
                                fprint(2, "write: %r\n");
       +                free(p);
                }
                fsclose(fid);
       +        Bterm(b);
                threadexitsall(0);
        }