[hackers] [9base] implemented 'tac' in a suckless way || pancake<nopcode.org>

From: <hg_AT_suckless.org>
Date: Thu, 1 Apr 2010 16:54:35 +0000 (UTC)

changeset: 57:69ca40f2af9c
tag: tip
user: pancake<nopcode.org>
date: Thu Apr 01 16:03:15 2010 +0200
files: Makefile mtime/mtime tac/Makefile tac/tac.1 tac/tac.c
description:
implemented 'tac' in a suckless way
remove mtime binary from the repository

diff -r 3e53e1a5886c -r 69ca40f2af9c Makefile
--- a/Makefile Sat Mar 27 23:23:08 2010 +0000
+++ b/Makefile Thu Apr 01 16:03:15 2010 +0200
@@ -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 -r 3e53e1a5886c -r 69ca40f2af9c mtime/mtime
Binary file mtime/mtime has changed
diff -r 3e53e1a5886c -r 69ca40f2af9c tac/Makefile
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tac/Makefile Thu Apr 01 16:03:15 2010 +0200
@@ -0,0 +1,10 @@
+# tac - reverse line order cat
+# Depends on ../lib9
+
+TARG = tac
+
+include ../std.mk
+
+pre-uninstall:
+
+post-install:
diff -r 3e53e1a5886c -r 69ca40f2af9c tac/tac.1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tac/tac.1 Thu Apr 01 16:03:15 2010 +0200
@@ -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 -r 3e53e1a5886c -r 69ca40f2af9c tac/tac.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tac/tac.c Thu Apr 01 16:03:15 2010 +0200
@@ -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);
+}
Received on Thu Apr 01 2010 - 16:54:35 UTC

This archive was generated by hypermail 2.2.0 : Thu Apr 01 2010 - 17:00:07 UTC