diff options
author | Christian Neukirchen <chneukirchen@gmail.com> | 2016-07-12 20:43:44 +0200 |
---|---|---|
committer | Christian Neukirchen <chneukirchen@gmail.com> | 2016-07-12 20:43:44 +0200 |
commit | 9418394d14c07bf96820e19d6d36b5b102ec1084 (patch) | |
tree | b3b2af669f4ec05349d1639cd55651d55d4c2f3e | |
parent | 663dc56d8f9a6b545a209d4249fd4c65d52ef068 (diff) | |
download | mblaze-9418394d14c07bf96820e19d6d36b5b102ec1084.tar.gz mblaze-9418394d14c07bf96820e19d6d36b5b102ec1084.tar.xz mblaze-9418394d14c07bf96820e19d6d36b5b102ec1084.zip |
next: add offset, also negative
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | 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; } |