From 9418394d14c07bf96820e19d6d36b5b102ec1084 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Tue, 12 Jul 2016 20:43:44 +0200 Subject: [PATCH] next: add offset, also negative --- Makefile | 3 ++- next.c | 28 ++++++++++++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index ff78e45..90b14ac 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CFLAGS=-g -O1 -Wall -Wno-switch -Wextra -Wwrite-strings -fstack-protector-strong -D_FORTIFY_SOURCE=2 -ALL = scan thread hdr show list +ALL = scan thread hdr show list next all: $(ALL) @@ -9,6 +9,7 @@ thread: blaze822.o thread.o hdr: blaze822.o hdr.o show: blaze822.o show.o list: list.o +next: next.o clean: FRC -rm -f $(ALL) *.o diff --git a/next.c b/next.c index f82e437..b04f3b9 100644 --- a/next.c +++ b/next.c @@ -24,21 +24,37 @@ main(int argc, char *argv[]) exit(102); char *s = strstr(map, argv[1]); + int a = 1; + if (argc > 2) + a = atoi(argv[2]); + if (!argv[1][0] || !s) { // default to first line s = map; } else { - while (*s && *s != '\n') - s++; - s++; + if (a > 0) { + while (a-- > 0) { + while (*s && *s != '\n') + s++; + s++; + } + } else { + while (a++ <= 0 && map < s) { + s--; + while (map < s && *s != '\n') + s--; + } + if (map < s && *s == '\n') + s++; + } } char *t = s; while (*t && *t != '\n') t++; if (!*t) - exit(1); + return 1; t++; - write(1, s, t-s); - + if (write(1, s, t-s) <= 0) + return 2; return 0; }