(root)/
findutils-4.9.0/
gl/
lib/
fclose.c
       1  /* fclose replacement.
       2     Copyright (C) 2008-2022 Free Software Foundation, Inc.
       3  
       4     This file is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU Lesser General Public License as
       6     published by the Free Software Foundation; either version 2.1 of the
       7     License, or (at your option) any later version.
       8  
       9     This file is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  
      19  /* Specification.  */
      20  #include <stdio.h>
      21  
      22  #include <errno.h>
      23  #include <unistd.h>
      24  
      25  #include "freading.h"
      26  #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
      27  # include "msvc-inval.h"
      28  #endif
      29  
      30  #undef fclose
      31  
      32  #if HAVE_MSVC_INVALID_PARAMETER_HANDLER
      33  static int
      34  fclose_nothrow (FILE *fp)
      35  {
      36    int result;
      37  
      38    TRY_MSVC_INVAL
      39      {
      40        result = fclose (fp);
      41      }
      42    CATCH_MSVC_INVAL
      43      {
      44        result = EOF;
      45        errno = EBADF;
      46      }
      47    DONE_MSVC_INVAL;
      48  
      49    return result;
      50  }
      51  #else
      52  # define fclose_nothrow fclose
      53  #endif
      54  
      55  /* Override fclose() to call the overridden fflush() or close().  */
      56  
      57  int
      58  rpl_fclose (FILE *fp)
      59  {
      60    int saved_errno = 0;
      61    int fd;
      62    int result = 0;
      63  
      64    /* Don't change behavior on memstreams.  */
      65    fd = fileno (fp);
      66    if (fd < 0)
      67      return fclose_nothrow (fp);
      68  
      69    /* We only need to flush the file if it is not reading or if it is
      70       seekable.  This only guarantees the file position of input files
      71       if the fflush module is also in use.  */
      72    if ((!freading (fp) || lseek (fileno (fp), 0, SEEK_CUR) != -1)
      73        && fflush (fp))
      74      saved_errno = errno;
      75  
      76    /* fclose() calls close(), but we need to also invoke all hooks that our
      77       overridden close() function invokes.  See lib/close.c.  */
      78  #if WINDOWS_SOCKETS
      79    /* Call the overridden close(), then the original fclose().
      80       Note about multithread-safety: There is a race condition where some
      81       other thread could open fd between our close and fclose.  */
      82    if (close (fd) < 0 && saved_errno == 0)
      83      saved_errno = errno;
      84  
      85    fclose_nothrow (fp); /* will fail with errno = EBADF,
      86                            if we did not lose a race */
      87  
      88  #else /* !WINDOWS_SOCKETS */
      89    /* Call fclose() and invoke all hooks of the overridden close().  */
      90  
      91  # if REPLACE_FCHDIR
      92    /* Note about multithread-safety: There is a race condition here as well.
      93       Some other thread could open fd between our calls to fclose and
      94       _gl_unregister_fd.  */
      95    result = fclose_nothrow (fp);
      96    if (result == 0)
      97      _gl_unregister_fd (fd);
      98  # else
      99    /* No race condition here.  */
     100    result = fclose_nothrow (fp);
     101  # endif
     102  
     103  #endif /* !WINDOWS_SOCKETS */
     104  
     105    if (saved_errno != 0)
     106      {
     107        errno = saved_errno;
     108        result = EOF;
     109      }
     110  
     111    return result;
     112  }