commit a41bc0926d3d369009bf864cffafe83313b73c30
Author: zavok <an2qzavok@gmail.com>
Date: Fri, 28 Feb 2020 13:24:34 +0300
start
Diffstat:
A | Makefile | | | 32 | ++++++++++++++++++++++++++++++++ |
A | README | | | 3 | +++ |
A | arg.h | | | 50 | ++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | example.tsv | | | 3 | +++ |
A | svex.c | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 143 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,32 @@
+.POSIX:
+
+PREFIX = /usr/local
+MANDIR = $(PREFIX)/share/man/man1
+
+BIN = svex
+MAN = $(BIN).1
+OBJ = $(BIN:=.o)
+
+all: $(BIN)
+
+$(BIN): $(OBJ)
+ $(CC) $(OBJ) $(LDFLAGS) $(LIBS) -o $@
+
+$(OBJ):
+
+install: $(BIN)
+ mkdir -p $(DESTDIR)$(PREFIX)/bin/
+ cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin/
+ chmod 555 $(DESTDIR)$(PREFIX)/bin/$(BIN)
+ mkdir -p $(DESTDIR)$(MANDIR)
+ cp -f $(MAN) $(DESTDIR)$(MANDIR)
+
+uninstall:
+ rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN)
+ rm -f $(DESTDIR)$(MANDIR)/$(MAN)
+
+clean:
+ rm -f $(BIN) $(OBJ)
+
+.c.o:
+ $(CC) $(CFLAGS) -c $<
diff --git a/README b/README
@@ -0,0 +1,3 @@
+svex - [tab] separated value explorer.
+
+
diff --git a/arg.h b/arg.h
@@ -0,0 +1,50 @@
+/*
+ * Copy me if you can.
+ * by 20h
+ */
+
+#ifndef ARG_H__
+#define ARG_H__
+
+extern char *argv0;
+
+/* use main(int argc, char *argv[]) */
+#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
+ argv[0] && argv[0][0] == '-'\
+ && argv[0][1];\
+ argc--, argv++) {\
+ char argc_;\
+ char **argv_;\
+ int brk_;\
+ if (argv[0][1] == '-' && argv[0][2] == '\0') {\
+ argv++;\
+ argc--;\
+ break;\
+ }\
+ int i_;\
+ for (i_ = 1, brk_ = 0, argv_ = argv;\
+ argv[0][i_] && !brk_;\
+ i_++) {\
+ if (argv_ != argv)\
+ break;\
+ argc_ = argv[0][i_];\
+ switch (argc_)
+
+#define ARGEND }\
+ }
+
+#define ARGC() argc_
+
+#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\
+ ((x), abort(), (char *)0) :\
+ (brk_ = 1, (argv[0][i_+1] != '\0')?\
+ (&argv[0][i_+1]) :\
+ (argc--, argv++, argv[0])))
+
+#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\
+ (char *)0 :\
+ (brk_ = 1, (argv[0][i_+1] != '\0')?\
+ (&argv[0][i_+1]) :\
+ (argc--, argv++, argv[0])))
+
+#endif
diff --git a/example.tsv b/example.tsv
@@ -0,0 +1,3 @@
+John Galt
+John Smith
+John Doe
diff --git a/svex.c b/svex.c
@@ -0,0 +1,55 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include "arg.h"
+
+char *argv0;
+
+char *fname;
+
+char *buff;
+ulong blen;
+
+void load(void);
+
+void
+usage (void)
+{
+ dprintf(2, "usage: %s file.tsv\n", argv0);
+ exit(0);
+}
+
+int
+main (int argc, char *argv[])
+{
+ blen = 0;
+ buff = 0;
+ ARGBEGIN{
+ default: usage();
+ } ARGEND
+ if (argc == 0) usage();
+ if (argc > 1) usage();
+ fname = argv[1];
+ load();
+ return 0;
+}
+
+void
+load(void)
+{
+ int f;
+ ulong n;
+ char b[1024], *bp;
+ bp = buff;
+ f = open(fname, O_RDONLY);
+ while ((n = read(f, b, 1024)) > 0){
+ buff = realloc(buff, blen + n);
+ memcpy(bp, b, n);
+ bp += n;
+ blen += n;
+ }
+ close(f);
+}