Skip to content

Commit

Permalink
Fix parsing of -NUMBER. We now do things a digit at a time and
Browse files Browse the repository at this point in the history
keep track of what the last char from getopt was as well as the
previous value of optind so we know when a new number has started.
  • Loading branch information
Todd Miller committed Jul 10, 2003
1 parent 6c3913a commit deac8b7
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions grep.c
@@ -1,4 +1,4 @@
/* $OpenBSD: grep.c,v 1.16 2003/06/24 18:45:30 tedu Exp $ */
/* $OpenBSD: grep.c,v 1.17 2003/06/25 17:28:00 millert Exp $ */

/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
Expand Down Expand Up @@ -30,6 +30,7 @@
#include <sys/limits.h>
#include <sys/stat.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
Expand Down Expand Up @@ -229,8 +230,7 @@ free_patterns(void)
int
main(int argc, char *argv[])
{
char *tmp;
int c, i;
int c, lastc, prevoptind, i;

switch (__progname[0]) {
case 'e':
Expand Down Expand Up @@ -260,17 +260,17 @@ main(int argc, char *argv[])
#endif
}

lastc = '\0';
prevoptind = 0;
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':
tmp = argv[optind - 1];
if (tmp[0] == '-' && tmp[1] == c && !tmp[2])
Aflag = Bflag = strtol(++tmp, NULL, 10);
if (optind == prevoptind && isdigit(lastc))
Aflag = Bflag = (Aflag * 10) + (c - '0');
else
Aflag = Bflag = strtol(argv[optind] + 1,
NULL, 10);
Aflag = Bflag = c - '0';
break;
case 'A':
Aflag = strtol(optarg, NULL, 10);
Expand Down Expand Up @@ -397,6 +397,8 @@ main(int argc, char *argv[])
default:
usage();
}
lastc = c;
prevoptind = optind;
}

argc -= optind;
Expand Down

0 comments on commit deac8b7

Please sign in to comment.