diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -137,10 +137,9 @@ int main(int argc, char **argv) { - const char *errstr = NULL; - char *ep, **oargv; - long l; - int ch, dflags, lastch, gotstdin, prevoptind, newarg; + const char *errstr; + char **oargv; + int ch, dflags, lastch, gotstdin, prevoptind, newarg; oargv = argv; gotstdin = 0; @@ -177,7 +176,7 @@ } if (diff_algorithm == D_DIFFNONE) { - printf("unknown algorithm: %s\n", optarg); + warnx("unknown algorithm: %s", optarg); usage(); } break; @@ -194,10 +193,13 @@ cflag = true; diff_format = D_CONTEXT; if (optarg != NULL) { - l = strtol(optarg, &ep, 10); - if (*ep != '\0' || l < 0 || l >= INT_MAX) + diff_context = (int) strtonum(optarg, + 1, INT_MAX, &errstr); + if (errstr != NULL) { + warnx("context size is %s: %s", + errstr, optarg); usage(); - diff_context = (int)l; + } } break; case 'd': @@ -294,10 +296,13 @@ conflicting_format(); diff_format = D_UNIFIED; if (optarg != NULL) { - l = strtol(optarg, &ep, 10); - if (*ep != '\0' || l < 0 || l >= INT_MAX) + diff_context = (int) strtonum(optarg, + 0, INT_MAX, &errstr); + if (errstr != NULL) { + warnx("context size is %s: %s", + errstr, optarg); usage(); - diff_context = (int)l; + } } break; case 'w': @@ -305,8 +310,8 @@ break; case 'W': width = (int) strtonum(optarg, 1, INT_MAX, &errstr); - if (errstr) { - warnx("Invalid argument for width"); + if (errstr != NULL) { + warnx("width is %s: %s", errstr, optarg); usage(); } break; @@ -346,8 +351,8 @@ break; case OPT_TSIZE: tabsize = (int) strtonum(optarg, 1, INT_MAX, &errstr); - if (errstr) { - warnx("Invalid argument for tabsize"); + if (errstr != NULL) { + warnx("tabsize is %s: %s", errstr, optarg); usage(); } break; @@ -364,9 +369,12 @@ colorflag = COLORFLAG_ALWAYS; else if (strncmp(optarg, "never", 5) == 0) colorflag = COLORFLAG_NEVER; - else - errx(2, "unsupported --color value '%s' (must be always, auto, or never)", - optarg); + else { + warnx("unsupported --color value " + "(must be always, auto, or never): " + "%s", optarg); + usage(); + } break; case OPT_NO_DEREFERENCE: noderef = true; diff --git a/usr.bin/diff/tests/diff_test.sh b/usr.bin/diff/tests/diff_test.sh --- a/usr.bin/diff/tests/diff_test.sh +++ b/usr.bin/diff/tests/diff_test.sh @@ -397,11 +397,11 @@ { echo $'x\na\ny' >a echo $'x\nb\ny' >b - atf_check -s exit:2 -e ignore diff -C$(((1<<31)-1)) a b + atf_check -s exit:2 -e ignore diff -C$((1<<31)) a b atf_check -s exit:1 -o match:'--- 1,3 ---' \ - diff -C$(((1<<31)-2)) a b + diff -C$(((1<<31)-1)) a b atf_check -s exit:1 -o match:'--- 1,3 ---' \ - diff -Astone -C$(((1<<31)-2)) a b + diff -Astone -C$(((1<<31)-1)) a b } bigu_head() @@ -412,11 +412,11 @@ { echo $'x\na\ny' >a echo $'x\nb\ny' >b - atf_check -s exit:2 -e ignore diff -U$(((1<<31)-1)) a b + atf_check -s exit:2 -e ignore diff -U$((1<<31)) a b atf_check -s exit:1 -o match:'^@@ -1,3 \+1,3 @@$' \ - diff -U$(((1<<31)-2)) a b + diff -U$(((1<<31)-1)) a b atf_check -s exit:1 -o match:'^@@ -1,3 \+1,3 @@$' \ - diff -Astone -U$(((1<<31)-2)) a b + diff -Astone -U$(((1<<31)-1)) a b } prleak_head()