diff options
author | David Gwynne <dlg@cvs.openbsd.org> | 2015-08-27 05:11:40 +0000 |
---|---|---|
committer | David Gwynne <dlg@cvs.openbsd.org> | 2015-08-27 05:11:40 +0000 |
commit | 39ce3a4069f9760e27233b2fd828797f928f73e8 (patch) | |
tree | b8a90f25a4500842b3a65e967bb3d7644dc6f641 /usr.bin | |
parent | 3609c204ce9cdb497e17c56cebb29e71fe32aab3 (diff) |
use strtonum to parse the number of lines of context.
this provides better error messages.
ok deraadt@ guenther@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/grep/grep.c | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 041d81ec9f8..ddaabd23566 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: grep.c,v 1.51 2015/04/30 13:49:04 millert Exp $ */ +/* $OpenBSD: grep.c,v 1.52 2015/08/27 05:11:39 dlg Exp $ */ /*- * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav @@ -235,7 +235,8 @@ main(int argc, char *argv[]) int c, lastc, prevoptind, newarg, i, needpattern, exprs, expr_sz; struct patfile *patfile, *pf_next; long l; - char *ep, **expr; + char **expr; + const char *errstr; SLIST_INIT(&patfilelh); switch (__progname[0]) { @@ -279,10 +280,9 @@ main(int argc, char *argv[]) break; case 'A': case 'B': - l = strtol(optarg, &ep, 10); - if (ep == optarg || *ep != '\0' || - l <= 0 || l >= INT_MAX) - errx(2, "context out of range"); + l = strtonum(optarg, 1, INT_MAX, &errstr); + if (errstr != NULL) + errx(2, "context %s", errstr); if (c == 'A') Aflag = (int)l; else @@ -292,10 +292,9 @@ main(int argc, char *argv[]) if (optarg == NULL) Aflag = Bflag = 2; else { - l = strtol(optarg, &ep, 10); - if (ep == optarg || *ep != '\0' || - l <= 0 || l >= INT_MAX) - errx(2, "context out of range"); + l = strtonum(optarg, 1, INT_MAX, &errstr); + if (errstr != NULL) + errx(2, "context %s", errstr); Aflag = Bflag = (int)l; } break; |