diff --git a/usr.bin/uniq/tests/uniq_test.sh b/usr.bin/uniq/tests/uniq_test.sh --- a/usr.bin/uniq/tests/uniq_test.sh +++ b/usr.bin/uniq/tests/uniq_test.sh @@ -160,6 +160,20 @@ atf_check -o inline:"y\n" cat actual } +atf_test_case stdout +stdout_head() { + atf_set descr "error writing to stdout" +} +stdout_body() { + ( + trap "" PIPE + echo a | uniq 2>stderr + echo $? >result + ) | true + atf_check -o inline:"1\n" cat result + atf_check -o match:"stdout" cat stderr +} + atf_init_test_cases() { atf_add_test_case basic @@ -175,4 +189,5 @@ atf_add_test_case count_unique atf_add_test_case interactive atf_add_test_case interactive_repeated + atf_add_test_case stdout } diff --git a/usr.bin/uniq/uniq.c b/usr.bin/uniq/uniq.c --- a/usr.bin/uniq/uniq.c +++ b/usr.bin/uniq/uniq.c @@ -84,7 +84,7 @@ int ch, comp; size_t prevbuflen, thisbuflen, b1; char *prevline, *thisline, *p; - const char *errstr, *ifn; + const char *errstr, *ifn, *ofn; cap_rights_t rights; (void) setlocale(LC_ALL, ""); @@ -142,6 +142,7 @@ ifp = stdin; ifn = "stdin"; ofp = stdout; + ofn = "stdout"; if (argc > 0 && strcmp(argv[0], "-") != 0) ifp = file(ifn = argv[0], "r"); cap_rights_init(&rights, CAP_FSTAT, CAP_READ); @@ -149,7 +150,7 @@ err(1, "unable to limit rights for %s", ifn); cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE); if (argc > 1) - ofp = file(argv[1], "w"); + ofp = file(ofn = argv[1], "w"); else cap_rights_set(&rights, CAP_IOCTL); if (caph_rights_limit(fileno(ofp), &rights) < 0) { @@ -240,6 +241,8 @@ (!dflag || (cflag && repeats > 0)) && (!uflag || repeats == 0)) show(ofp, prevline); + if (fflush(ofp) != 0) + err(1, "%s", ofn); exit(0); }