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 @@ -24,8 +24,8 @@ atf_test_case noderef atf_test_case ignorecase atf_test_case dirloop -atf_test_case bigc -atf_test_case bigu +atf_test_case crange +atf_test_case urange atf_test_case prleak atf_test_case same @@ -389,34 +389,48 @@ atf_check diff -r a b } -bigc_head() +crange_head() { - atf_set "descr" "Context diff with very large context" + atf_set "descr" "Context diff context length range" } -bigc_body() +crange_body() { - 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:1 -o match:'--- 1,3 ---' \ - diff -C$(((1<<31)-2)) a b - atf_check -s exit:1 -o match:'--- 1,3 ---' \ - diff -Astone -C$(((1<<31)-2)) a b + echo $'x\nx\na\ny\ny' >a + echo $'x\nx\nb\ny\ny' >b + atf_check -s exit:2 -e match:'too small' \ + diff -C-1 a b + atf_check -s exit:2 -e match:'too small' \ + diff -C0 a b + atf_check -s exit:1 -o match:'--- 2,4 ---' \ + diff -C1 a b + atf_check -s exit:1 -o match:'--- 2,4 ---' \ + diff -Astone -C1 a b + atf_check -s exit:2 -e match:'too large' \ + diff -C$((1<<31)) a b + atf_check -s exit:1 -o match:'--- 1,5 ---' \ + diff -C$(((1<<31)-1)) a b + atf_check -s exit:1 -o match:'--- 1,5 ---' \ + diff -Astone -C$(((1<<31)-1)) a b } -bigu_head() +urange_head() { - atf_set "descr" "Unified diff with very large context" + atf_set "descr" "Unified diff context length range" } -bigu_body() +urange_body() { - 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:1 -o match:'^@@ -1,3 \+1,3 @@$' \ - diff -U$(((1<<31)-2)) a b - atf_check -s exit:1 -o match:'^@@ -1,3 \+1,3 @@$' \ - diff -Astone -U$(((1<<31)-2)) a b + echo $'x\nx\na\ny\ny' >a + echo $'x\nx\nb\ny\ny' >b + atf_check -s exit:2 -e match:'too small' \ + diff -U-1 a b + atf_check -s exit:1 -o match:'^@@ -3 \+3 @@$' \ + diff -U0 a b + atf_check -s exit:2 -e match:'too large' \ + diff -U$((1<<31)) a b + atf_check -s exit:1 -o match:'^@@ -1,5 \+1,5 @@$' \ + diff -U$(((1<<31)-1)) a b + atf_check -s exit:1 -o match:'^@@ -1,5 \+1,5 @@$' \ + diff -Astone -U$(((1<<31)-1)) a b } prleak_head() @@ -485,8 +499,8 @@ atf_add_test_case noderef atf_add_test_case ignorecase atf_add_test_case dirloop - atf_add_test_case bigc - atf_add_test_case bigu + atf_add_test_case crange + atf_add_test_case urange atf_add_test_case prleak atf_add_test_case same }