9base

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

commit 942791ab23de64d2580e3143ba3866ad85fa8ab3
parent 3bdea00b978d1f91addcee5a8afd01acd7db3ea4
Author: pancake <nopcode.org>
Date:   Thu,  1 Apr 2010 16:03:15 +0200

implemented 'tac' in a suckless way
remove mtime binary from the repository
Diffstat:
Makefile | 4++--
mtime/mtime | 0
tac/Makefile | 10++++++++++
tac/tac.1 | 28++++++++++++++++++++++++++++
tac/tac.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 100 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,8 +3,8 @@ include config.mk SUBDIRS = lib9 yacc awk basename bc cal cat cleanname date dc du echo \ - fortune freq getflags grep hoc ls mk mkdir mtime \ - rc read sed seq sleep sort tee test touch tr troff uniq + fortune freq getflags grep hoc ls mk mkdir mtime rc read \ + sed seq sleep sort tac tee test touch tr troff uniq # factor primes diff --git a/mtime/mtime b/mtime/mtime Binary files differ. diff --git a/tac/Makefile b/tac/Makefile @@ -0,0 +1,10 @@ +# tac - reverse line order cat +# Depends on ../lib9 + +TARG = tac + +include ../std.mk + +pre-uninstall: + +post-install: diff --git a/tac/tac.1 b/tac/tac.1 @@ -0,0 +1,28 @@ +.TH TAC 1 +.SH NAME +tac \- reverse concatenate files +.SH SYNOPSIS +.B tac +[ +.I file ... +] +.SH DESCRIPTION +.I Tac +reads each +.I file +in sequence and writes it on the standard output in reverse line order. +.IP +.L +tac file +.LP +prints a file in reverse line order +.IP +.L +tac file1 file2 >file3 +.LP +Concatenate reversed file1 and file2 into file3 +.LP +.SH SEE ALSO +.IR cat (1) +.SH BUGS +Same as in cat diff --git a/tac/tac.c b/tac/tac.c @@ -0,0 +1,60 @@ +/* author: pancake<nopcode.org> */ +#include <u.h> +#include <libc.h> + +static vlong bsize = 0; +static char *buf; +#define LINES 4096 + +void +tac() +{ + int i, j; + char *ptr, **nls; + nls = malloc(LINES*sizeof(nls)); + for(i=1, ptr=buf; ptr;) { + assert(nls != NULL); + for(j=0; j<LINES && (ptr=strchr(ptr+1, '\n')); j++) + nls[i++] = ptr+1; + nls = realloc(nls, (i+LINES)*sizeof(nls)); + } + *nls = buf; + while(i--) + write(1, nls[i], nls[i+1]-nls[i]); + free(nls); +} + +void +load(int f) +{ + vlong nsize, size = seek(f, 0, 2); + if (size>0) { + nsize = bsize + size; + buf = realloc(buf, nsize); + seek(f, 0, 0); + read(f, buf+bsize, size); + bsize = nsize; + } else + while ((size = read(f, buf+bsize, LINES))>0) + bsize+=size; +} + +void +main(int argc, char *argv[]) +{ + int i, f; + buf = malloc(1); + assert(buf != NULL); + if (argc == 1) + load(0); + else for(i=1; i<argc; i++){ + f = open(argv[i], OREAD); + if(f >= 0){ + load(f); + close(f); + }else sysfatal("can't open %s: %r", argv[i]); + } + tac(); + free(buf); + exits(0); +}