next: add offset, also negative

This commit is contained in:
Christian Neukirchen 2016-07-12 20:43:44 +02:00
parent 663dc56d8f
commit 9418394d14
2 changed files with 24 additions and 7 deletions

View File

@ -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

28
next.c
View File

@ -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;
}