Close the output file, and do any associated cleanup. If FP and FD are both specified, they refer to the same open file; in this case FP is closed, but FD is still used in cleanup. */
| 622 | If FP and FD are both specified, they refer to the same open file; |
| 623 | in this case FP is closed, but FD is still used in cleanup. */ |
| 624 | static void |
| 625 | closeout (FILE *fp, int fd, pid_t pid, char const *name) |
| 626 | { |
| 627 | if (fp != NULL && fclose (fp) != 0 && ! ignorable (errno)) |
| 628 | error (EXIT_FAILURE, errno, "%s", quotef (name)); |
| 629 | if (fd >= 0) |
| 630 | { |
| 631 | if (fp == NULL && close (fd) < 0) |
| 632 | error (EXIT_FAILURE, errno, "%s", quotef (name)); |
| 633 | for (int j = 0; j < n_open_pipes; ++j) |
| 634 | { |
| 635 | if (open_pipes[j] == fd) |
| 636 | { |
| 637 | open_pipes[j] = open_pipes[--n_open_pipes]; |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | if (pid > 0) |
| 643 | { |
| 644 | int wstatus; |
| 645 | if (waitpid (pid, &wstatus, 0) < 0) |
| 646 | error (EXIT_FAILURE, errno, _("waiting for child process")); |
| 647 | else if (WIFSIGNALED (wstatus)) |
| 648 | { |
| 649 | int sig = WTERMSIG (wstatus); |
| 650 | if (sig != SIGPIPE) |
| 651 | { |
| 652 | char signame[MAX (SIG2STR_MAX, INT_BUFSIZE_BOUND (int))]; |
| 653 | if (sig2str (sig, signame) != 0) |
| 654 | sprintf (signame, "%d", sig); |
| 655 | error (sig + 128, 0, |
| 656 | _("with FILE=%s, signal %s from command: %s"), |
| 657 | quotef (name), signame, filter_command); |
| 658 | } |
| 659 | } |
| 660 | else if (WIFEXITED (wstatus)) |
| 661 | { |
| 662 | int ex = WEXITSTATUS (wstatus); |
| 663 | if (ex != 0) |
| 664 | error (ex, 0, _("with FILE=%s, exit %d from command: %s"), |
| 665 | quotef (name), ex, filter_command); |
| 666 | } |
| 667 | else |
| 668 | { |
| 669 | /* shouldn't happen. */ |
| 670 | error (EXIT_FAILURE, 0, |
| 671 | _("unknown status from command (0x%X)"), wstatus + 0u); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /* Write BYTES bytes at BP to an output file. |
| 677 | If NEW_FILE_FLAG is true, open the next output file. |