tadd write -l - 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 276bf4edf1829a03198b9b35152af35eacf51061 (DIR) parent 83c4506aa5241351323a62b8fdee825058286f20 (HTM) Author: rsc <devnull@localhost> Date: Fri, 11 Feb 2005 19:44:04 +0000 add write -l Diffstat: M src/cmd/9p.c | 32 ++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) --- (DIR) diff --git a/src/cmd/9p.c b/src/cmd/9p.c t@@ -1,6 +1,7 @@ #include <u.h> #include <signal.h> #include <libc.h> +#include <bio.h> #include <fcall.h> #include <9pclient.h> #include <auth.h> t@@ -15,7 +16,7 @@ usage(void) fprint(2, "possible cmds:\n"); fprint(2, " read name\n"); fprint(2, " readfd name\n"); - fprint(2, " write name\n"); + fprint(2, " write [-l] name\n"); fprint(2, " writefd name\n"); fprint(2, " stat name\n"); // fprint(2, " ls name\n"); t@@ -176,8 +177,14 @@ xwrite(int argc, char **argv) char buf[1024]; int n, did; CFid *fid; + Biobuf *b; + char *p; + int byline; + byline = 0; ARGBEGIN{ + case 'l': + byline = 1; default: usage(); }ARGEND t@@ -187,10 +194,25 @@ xwrite(int argc, char **argv) did = 0; fid = xopen(argv[0], OWRITE|OTRUNC); - while((n = read(0, buf, sizeof buf)) > 0){ - did = 1; - if(fswrite(fid, buf, n) != n) - sysfatal("write error: %r"); + if(byline){ + n = 0; + b = malloc(sizeof *b); + if(b == nil) + sysfatal("out of memory"); + Binit(b, 0, OREAD); + while((p = Brdstr(b, '\n', 0)) != nil){ + n = strlen(p); + did = 1; + if(fswrite(fid, p, n) != n) + sysfatal("write error: %r"); + } + free(b); + }else{ + while((n = read(0, buf, sizeof buf)) > 0){ + did = 1; + if(fswrite(fid, buf, n) != n) + sysfatal("write error: %r"); + } } if(n == 0 && !did){ if(fswrite(fid, buf, 0) != 0)