diff options
author | 2003-09-07 19:40:54 +0000 | |
---|---|---|
committer | 2003-09-07 19:40:54 +0000 | |
commit | 8fa3ac30e80581ab3fc1b4106a1f00e51d1654a2 (patch) | |
tree | 8f43bbe0c81bc95581c9b3bbe7b06dfb16526146 /usr.bin/grep/grep.c | |
parent | escape `.' to avoid a double space; (diff) | |
download | wireguard-openbsd-8fa3ac30e80581ab3fc1b4106a1f00e51d1654a2.tar.xz wireguard-openbsd-8fa3ac30e80581ab3fc1b4106a1f00e51d1654a2.zip |
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/grep.c')
-rw-r--r-- | usr.bin/grep/grep.c | 20 |
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; |