summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2016-07-13 16:47:06 +0200
committerChristian Neukirchen <chneukirchen@gmail.com>2016-07-13 16:47:06 +0200
commit797ddf96dc0864cf8895fdc827f5a2385d1acc4a (patch)
tree586fc27e1d348e7f5121323d7d4936836835ac95
parent105f520c7e2c40939f78be061e143687dadfa0fb (diff)
downloadmblaze-797ddf96dc0864cf8895fdc827f5a2385d1acc4a.tar.gz
mblaze-797ddf96dc0864cf8895fdc827f5a2385d1acc4a.tar.xz
mblaze-797ddf96dc0864cf8895fdc827f5a2385d1acc4a.zip
next: plain numbers are now absolute, use +/- prefix for relative
-rw-r--r--next.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/next.c b/next.c
index b04f3b9..14d8643 100644
--- a/next.c
+++ b/next.c
@@ -23,15 +23,25 @@ main(int argc, char *argv[])
 	if (!map)
 		exit(102);
 
-	char *s = strstr(map, argv[1]);
-	int a = 1;
+	char *s;
+	char *arg;
 	if (argc > 2)
-		a = atoi(argv[2]);
+		arg = argv[2];
+	else
+		arg = "+1";
 
-	if (!argv[1][0] || !s) {
+	if (argc <= 1 || !argv[1][0]) {
 		// default to first line
-		s = map;
-	} else {
+		arg = "1";
+	}
+
+	if (*arg == '+' || *arg == '-') {
+		s = strstr(map, argv[1]);
+		int a = atoi(arg);
+		if (!s) {
+			s = map;
+			a = 0;
+		}
 		if (a > 0) {
 			while (a-- > 0) {
 				while (*s && *s != '\n')
@@ -47,6 +57,17 @@ main(int argc, char *argv[])
 			if (map < s && *s == '\n')
 				s++;
 		}
+	} else {
+		int a = atoi(arg);
+		s = map;
+		while (--a > 0) {
+			char *n = strchr(s+1, '\n');
+			if (!n)
+				break;
+			s = n;
+		}
+		if (s && *s)
+			s++;
 	}
 	char *t = s;
 	while (*t && *t != '\n')