|
1 | 1 | /* |
2 | | - Copyright (C) 2002-2012, 2014-2023 Free Software Foundation, Inc. |
| 2 | + Copyright (C) 2002-2012, 2014-2025 Free Software Foundation, Inc. |
3 | 3 | Written by Keisuke Nishida, Roger While, Simon Sobisch, Ron Norman |
4 | 4 |
|
5 | 5 | This file is part of GnuCOBOL. |
@@ -4725,71 +4725,56 @@ cob_file_close (cob_file_api *a, cob_file *f, const int opt) |
4725 | 4725 | #endif |
4726 | 4726 | } |
4727 | 4727 | /* Close the file */ |
4728 | | - if (f->organization == COB_ORG_LINE_SEQUENTIAL) { |
4729 | | - if (f->flag_is_pipe) { |
4730 | | - if (f->file_pid) { |
4731 | | - if (f->file) |
4732 | | - fclose (f->file); |
4733 | | - if (f->fileout |
4734 | | - && f->fileout != f->file) |
4735 | | - fclose (f->fileout); |
| 4728 | + if (f->flag_is_pipe) { |
| 4729 | + if (f->file_pid) { |
| 4730 | + if (f->file) { |
| 4731 | + fclose (f->file); |
| 4732 | + } |
| 4733 | + if (f->fileout |
| 4734 | + && f->fileout != f->file) { |
| 4735 | + fclose (f->fileout); |
| 4736 | + } |
4736 | 4737 | #if defined (HAVE_UNISTD_H) && !(defined (_WIN32)) |
4737 | | - { |
4738 | | - int sts; |
4739 | | - errno = 0; |
4740 | | - kill (f->file_pid, 0); |
4741 | | - if (errno == ESRCH) { |
4742 | | - waitpid (f->file_pid, &sts, WNOHANG); |
4743 | | - } |
4744 | | - else { |
4745 | | - cob_sleep_msec (50); |
4746 | | - kill (f->file_pid, SIGKILL); |
4747 | | - cob_sleep_msec (50); |
4748 | | - waitpid (f->file_pid, &sts, 0); |
4749 | | - } |
| 4738 | + { |
| 4739 | + int sts; |
| 4740 | + errno = 0; |
| 4741 | + kill (f->file_pid, 0); |
| 4742 | + if (errno == ESRCH) { |
| 4743 | + waitpid (f->file_pid, &sts, WNOHANG); |
| 4744 | + } |
| 4745 | + else { |
| 4746 | + cob_sleep_msec (50); |
| 4747 | + kill (f->file_pid, SIGKILL); |
| 4748 | + cob_sleep_msec (50); |
| 4749 | + waitpid (f->file_pid, &sts, 0); |
| 4750 | + } |
4750 | 4751 | } |
4751 | 4752 | #elif defined (_WIN32) |
4752 | | - { |
4753 | | - char buff[COB_MINI_BUFF]; |
4754 | | - snprintf((char *)&buff, COB_MINI_MAX, "TASKKILL /PID %d", f->file_pid); |
4755 | | - system(buff); |
4756 | | - } |
4757 | | -#endif |
4758 | | - } else { |
4759 | | - pclose ((FILE *)f->file); |
| 4753 | + { |
| 4754 | + char buff[COB_MINI_BUFF]; |
| 4755 | + snprintf ((char *)&buff, COB_MINI_MAX, "TASKKILL /PID %d", f->file_pid); |
| 4756 | + system (buff); |
4760 | 4757 | } |
4761 | | - f->flag_is_pipe = 0; |
4762 | | - f->file = f->fileout = NULL; |
4763 | | - f->fd = f->fdout = -1; |
4764 | | - f->file_pid = 0; |
4765 | | - return COB_STATUS_00_SUCCESS; |
4766 | | - } |
4767 | | -#ifdef _MSC_VER /* explicit only stream or fd close with this compiler */ |
4768 | | - if (f->file != NULL) { |
4769 | | - fclose ((FILE *)f->file); |
4770 | | - f->file = NULL; |
4771 | | -#ifdef _WIN32 |
4772 | | - /* at least on MSVC that closes the underlying file descriptor, too */ |
4773 | | - f->fd = -1; |
4774 | | -#endif |
4775 | | - } |
4776 | | -#endif |
4777 | | - } else { |
4778 | | - if (f->fd >= 0) { |
4779 | | - close (f->fd); |
4780 | | - f->fd = -1; |
4781 | | -#ifdef _MSC_VER |
4782 | | - f->file = NULL; |
4783 | 4758 | #endif |
| 4759 | + } else { |
| 4760 | + pclose ((FILE *)f->file); |
4784 | 4761 | } |
| 4762 | + f->flag_is_pipe = 0; |
| 4763 | + f->file = f->fileout = NULL; |
| 4764 | + f->fd = f->fdout = -1; |
| 4765 | + f->file_pid = 0; |
| 4766 | + return COB_STATUS_00_SUCCESS; |
4785 | 4767 | } |
4786 | | -#ifndef _MSC_VER /* explicit disallowed there, is that the same for other compilers? */ |
4787 | 4768 | if (f->file != NULL) { |
| 4769 | + /* This also closes the underlying fd */ |
4788 | 4770 | fclose ((FILE *)f->file); |
4789 | 4771 | f->file = NULL; |
4790 | 4772 | f->fd = -1; |
| 4773 | + } else |
| 4774 | + if (f->fd >= 0) { |
| 4775 | + close (f->fd); |
| 4776 | + f->fd = -1; |
4791 | 4777 | } |
4792 | | -#endif |
4793 | 4778 | if (opt == COB_CLOSE_NO_REWIND) { |
4794 | 4779 | f->open_mode = COB_OPEN_CLOSED; |
4795 | 4780 | return COB_STATUS_07_SUCCESS_NO_UNIT; |
|
0 commit comments