summaryrefslogtreecommitdiff
path: root/usr.bin/grep
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2003-09-07 19:40:55 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2003-09-07 19:40:55 +0000
commit004fd48934eb95316775723b596d4d9108378d75 (patch)
tree2ed6c3e4c6769d36a337ebf42a8452254dc7db7e /usr.bin/grep
parent6fdf01f874010c5282043b5b905a2e4bc1e9d368 (diff)
Fix "grep -number" support for multi-digit numbers. At issue is
the fact that optind refers to the *next* argument to be consumed by getopt(), not the current one. This means we have to keep track of when we are working with a new argv entry by hand. OK hugh@
Diffstat (limited to 'usr.bin/grep')
-rw-r--r--usr.bin/grep/grep.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c
index d7c1e0e2ef0..341ac5b8381 100644
--- a/usr.bin/grep/grep.c
+++ b/usr.bin/grep/grep.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grep.c,v 1.22 2003/07/16 19:08:21 millert Exp $ */
+/* $OpenBSD: grep.c,v 1.23 2003/09/07 19:40:54 millert Exp $ */
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
@@ -230,7 +230,7 @@ free_patterns(void)
int
main(int argc, char *argv[])
{
- int c, lastc, prevoptind, i;
+ int c, lastc, prevoptind, newarg, i;
long l;
char *ep;
@@ -263,18 +263,18 @@ main(int argc, char *argv[])
}
lastc = '\0';
- prevoptind = 0;
+ newarg = 1;
+ prevoptind = 1;
while ((c = getopt_long(argc, argv, optstr,
long_options, NULL)) != -1) {
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
- if (optind == prevoptind && isdigit(lastc)) {
- if (Aflag > INT_MAX / 10)
- errx(2, "context out of range");
- Aflag = Bflag = (Aflag * 10) + (c - '0');
- } else
- Aflag = Bflag = c - '0';
+ if (newarg || !isdigit(lastc))
+ Aflag = 0;
+ else if (Aflag > INT_MAX / 10)
+ errx(2, "context out of range");
+ Aflag = Bflag = (Aflag * 10) + (c - '0');
break;
case 'A':
case 'B':
@@ -412,9 +412,9 @@ main(int argc, char *argv[])
usage();
}
lastc = c;
+ newarg = optind != prevoptind;
prevoptind = optind;
}
-
argc -= optind;
argv += optind;