9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

commit 21195c3217d3a4a69848735cb7cab5e669fad8de
parent b7ce8129de5defa275d14b7656101ddcfb06cb77
Author: anselm@garbe.us <unknown>
Date:   Mon,  2 Jul 2012 21:52:15 +0200

applied sl's read patch
Diffstat:
Mread/read.1 | 15+++++++++++++++
Mread/read.c | 56++++++++++++++++++++++++++++++++++++++++++++------------
2 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/read/read.1 b/read/read.1 @@ -9,6 +9,9 @@ cat, read, nobs \- catenate files .br .B read [ +.B -c +] [ +[ .B -m ] [ .B -n @@ -62,6 +65,18 @@ causes it to read no more than .I nline lines. .PP +With the +.B -c +flag, +.I read +copies exactly +.I nbytes +of characters instead of lines. It is mutually exclusive with +.B -n +and +.B -m +flag. +.PP .I Read always executes a single .B write diff --git a/read/read.c b/read/read.c @@ -3,6 +3,7 @@ int multi; int nlines; +vlong nchars; char *status = nil; int @@ -53,29 +54,60 @@ lines(int fd, char *file) } void +chars(int fd, char *file) +{ + char buf[8*1024]; + vlong m; + int n; + + for(m = 0; m < nchars; m += n){ + n = sizeof(buf); + if(n > (nchars - m)) + n = nchars - m; + if((n = read(fd, buf, n)) < 0){ + fprint(2, "read: error reading %s: %r\n", file); + exits("read error"); + } + if(n == 0){ + if(m == 0) + status = "eof"; + break; + } + write(1, buf, n); + } +} + +void +usage(void) +{ + fprint(2, "usage: read [-m] [-n nlines] [-c nbytes] [files...]\n"); + exits("usage"); +} + +void main(int argc, char *argv[]) { + void (*proc)(int, char*); int i, fd; - char *s; + proc = lines; ARGBEGIN{ + case 'c': + nchars = atoll(EARGF(usage())); + proc = chars; + break; + case 'n': + nlines = atoi(EARGF(usage())); + break; case 'm': multi = 1; break; - case 'n': - s = ARGF(); - if(s){ - nlines = atoi(s); - break; - } - /* fall through */ default: - fprint(2, "usage: read [-m] [-n nlines] [files...]\n"); - exits("usage"); + usage(); }ARGEND if(argc == 0) - lines(0, "<stdin>"); + (*proc)(0, "<stdin>"); else for(i=0; i<argc; i++){ fd = open(argv[i], OREAD); @@ -83,7 +115,7 @@ main(int argc, char *argv[]) fprint(2, "read: can't open %s: %r\n", argv[i]); exits("open"); } - lines(fd, argv[i]); + (*proc)(fd, argv[i]); close(fd); }