Exemplo n.º 1
0
/* Flush all pending data on STREAM according to POSIX rules.  Both
   output and seekable input streams are supported.  */
int
rpl_fflush (FILE *stream)
{
  int result;
  off_t pos;

  /* When stream is NULL, POSIX and C99 only require flushing of "output
     streams and update streams in which the most recent operation was not
     input", and all implementations do this.

     When stream is "an output stream or an update stream in which the most
     recent operation was not input", POSIX and C99 requires that fflush
     writes out any buffered data, and all implementations do this.

     When stream is, however, an input stream or an update stream in
     which the most recent operation was input, C99 specifies nothing,
     and POSIX only specifies behavior if the stream is seekable.
     mingw, in particular, drops the input buffer, leaving the file
     descriptor positioned at the end of the input buffer. I.e. ftell
     (stream) is lost.  We don't want to call the implementation's
     fflush in this case.

     We test ! freading (stream) here, rather than fwriting (stream), because
     what we need to know is whether the stream holds a "read buffer", and on
     mingw this is indicated by _IOREAD, regardless of _IOWRT.  */
  if (stream == NULL || ! freading (stream))
    return fflush (stream);

  /* POSIX does not specify fflush behavior for non-seekable input
     streams.  Some implementations purge unread data, some return
     EBADF, some do nothing.  */
  pos = ftello (stream);
  if (pos == -1)
    {
      errno = EBADF;
      return EOF;
    }

  /* To get here, we must be flushing a seekable input stream, so the
     semantics of fpurge are now appropriate to clear the buffer.  To
     avoid losing data, the lseek is also necessary.  */
  result = fpurge (stream);
  if (result != 0)
    return result;

  pos = lseek (fileno (stream), pos, SEEK_SET);
  if (pos == -1)
    return EOF;
  /* After a successful lseek, update the file descriptor's position cache
     in the stream.  */
#if defined __sferror           /* FreeBSD, NetBSD, OpenBSD, MacOS X, Cygwin */
  stream->_offset = pos;
  stream->_flags |= __SOFF;
#endif

  return 0;
}
Exemplo n.º 2
0
int
rpl_fclose (FILE *fp)
{
  int saved_errno = 0;
  int fd;
  int result = 0;

  /* Don't change behavior on memstreams.  */
  fd = fileno (fp);
  if (fd < 0)
    return fclose_nothrow (fp);

  /* We only need to flush the file if it is not reading or if it is
     seekable.  This only guarantees the file position of input files
     if the fflush module is also in use.  */
  if ((!freading (fp) || lseek (fileno (fp), 0, SEEK_CUR) != -1)
      && fflush (fp))
    saved_errno = errno;

  /* fclose() calls close(), but we need to also invoke all hooks that our
     overridden close() function invokes.  See lib/close.c.  */
#if WINDOWS_SOCKETS
  /* Call the overridden close(), then the original fclose().
     Note about multithread-safety: There is a race condition where some
     other thread could open fd between our close and fclose.  */
  if (close (fd) < 0 && saved_errno == 0)
    saved_errno = errno;

  fclose_nothrow (fp); /* will fail with errno = EBADF,
                          if we did not lose a race */

#else /* !WINDOWS_SOCKETS */
  /* Call fclose() and invoke all hooks of the overridden close().  */

# if REPLACE_FCHDIR
  /* Note about multithread-safety: There is a race condition here as well.
     Some other thread could open fd between our calls to fclose and
     _gl_unregister_fd.  */
  result = fclose_nothrow (fp);
  if (result == 0)
    _gl_unregister_fd (fd);
# else
  /* No race condition here.  */
  result = fclose_nothrow (fp);
# endif

#endif /* !WINDOWS_SOCKETS */

  if (saved_errno != 0)
    {
      errno = saved_errno;
      result = EOF;
    }

  return result;
}
Exemplo n.º 3
0
int
main ()
{
  FILE *fp;

  /* Create a file with some contents.  Write-only file is never reading.  */
  fp = fopen (TESTFILE, "w");
  if (fp == NULL)
    goto skip;
  ASSERT (!freading (fp));
  if (fwrite ("foobarsh", 1, 8, fp) < 8)
    goto skip;
  ASSERT (!freading (fp));
  if (fclose (fp))
    goto skip;

  /* Open it in read-only mode.  Read-only file is always reading.  */
  fp = fopen (TESTFILE, "r");
  if (fp == NULL)
    goto skip;
  ASSERT (freading (fp));
  if (fgetc (fp) != 'f')
    goto skip;
  ASSERT (freading (fp));
  if (fseek (fp, 2, SEEK_CUR))
    goto skip;
  ASSERT (freading (fp));
  if (fgetc (fp) != 'b')
    goto skip;
  ASSERT (freading (fp));
  fflush (fp);
  ASSERT (freading (fp));
  if (fgetc (fp) != 'a')
    goto skip;
  ASSERT (freading (fp));
  if (fseek (fp, 0, SEEK_END))
    goto skip;
  ASSERT (freading (fp));
  if (fclose (fp))
    goto skip;

  /* Open it in read-write mode.  POSIX requires a reposition (fseek,
     fsetpos, rewind) or EOF when transitioning from read to write;
     freading is only deterministic after input or output, but this
     test case should be portable even on open, after reposition, and
     at EOF.  */
  /* First a scenario with only fgetc, fseek, fputc.  */
  fp = fopen (TESTFILE, "r+");
  if (fp == NULL)
    goto skip;
  ASSERT (!freading (fp));
  if (fgetc (fp) != 'f')
    goto skip;
  ASSERT (freading (fp));
  if (fseek (fp, 2, SEEK_CUR))
    goto skip;
  /* freading (fp) is undefined here, but fwriting (fp) is false.  */
  if (fgetc (fp) != 'b')
    goto skip;
  ASSERT (freading (fp));
  /* This fseek call is necessary when switching from reading to writing.
     See the description of fopen(), ISO C 99 7.19.5.3.(6).  */
  if (fseek (fp, 0, SEEK_CUR) != 0)
    goto skip;
  /* freading (fp) is undefined here, but fwriting (fp) is false.  */
  if (fputc ('x', fp) != 'x')
    goto skip;
  ASSERT (!freading (fp));
  if (fseek (fp, 0, SEEK_END))
    goto skip;
  /* freading (fp) is undefined here, because on some implementations (e.g.
     glibc) fseek causes a buffer to be read.
     fwriting (fp) is undefined as well.  */
  if (fclose (fp))
    goto skip;

  /* Open it in read-write mode.  POSIX requires a reposition (fseek,
     fsetpos, rewind) or EOF when transitioning from read to write;
     freading is only deterministic after input or output, but this
     test case should be portable even on open, after reposition, and
     at EOF.  */
  /* Here a scenario that includes fflush.  */
  fp = fopen (TESTFILE, "r+");
  if (fp == NULL)
    goto skip;
  ASSERT (!freading (fp));
  if (fgetc (fp) != 'f')
    goto skip;
  ASSERT (freading (fp));
  if (fseek (fp, 2, SEEK_CUR))
    goto skip;
  /* freading (fp) is undefined here, but fwriting (fp) is false.  */
  if (fgetc (fp) != 'b')
    goto skip;
  ASSERT (freading (fp));
  fflush (fp);
  /* freading (fp) is undefined here, but fwriting (fp) is false.  */
  if (fgetc (fp) != 'x')
    goto skip;
  ASSERT (freading (fp));
  /* This fseek call is necessary when switching from reading to writing.
     See the description of fopen(), ISO C 99 7.19.5.3.(6).  */
  if (fseek (fp, 0, SEEK_CUR) != 0)
    goto skip;
  /* freading (fp) is undefined here, but fwriting (fp) is false.  */
  if (fputc ('z', fp) != 'z')
    goto skip;
  ASSERT (!freading (fp));
  if (fseek (fp, 0, SEEK_END))
    goto skip;
  /* freading (fp) is undefined here, because on some implementations (e.g.
     glibc) fseek causes a buffer to be read.
     fwriting (fp) is undefined as well.  */
  if (fclose (fp))
    goto skip;

  /* Open it in append mode.  Write-only file is never reading.  */
  fp = fopen (TESTFILE, "a");
  if (fp == NULL)
    goto skip;
  ASSERT (!freading (fp));
  if (fwrite ("bla", 1, 3, fp) < 3)
    goto skip;
  ASSERT (!freading (fp));
  if (fclose (fp))
    goto skip;

  return 0;

 skip:
  fprintf (stderr, "Skipping test: file operations failed.\n");
  return 77;
}
Exemplo n.º 4
0
/* Flush all pending data on STREAM according to POSIX rules.  Both
   output and seekable input streams are supported.  */
int
rpl_fflush (FILE *stream)
{
    /* When stream is NULL, POSIX and C99 only require flushing of "output
       streams and update streams in which the most recent operation was not
       input", and all implementations do this.

       When stream is "an output stream or an update stream in which the most
       recent operation was not input", POSIX and C99 requires that fflush
       writes out any buffered data, and all implementations do this.

       When stream is, however, an input stream or an update stream in
       which the most recent operation was input, C99 specifies nothing,
       and POSIX only specifies behavior if the stream is seekable.
       mingw, in particular, drops the input buffer, leaving the file
       descriptor positioned at the end of the input buffer. I.e. ftell
       (stream) is lost.  We don't want to call the implementation's
       fflush in this case.

       We test ! freading (stream) here, rather than fwriting (stream), because
       what we need to know is whether the stream holds a "read buffer", and on
       mingw this is indicated by _IOREAD, regardless of _IOWRT.  */
    if (stream == NULL || ! freading (stream))
        return fflush (stream);

#if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Antares, Linux libc5 */

    clear_ungetc_buffer_preserving_position (stream);

    return fflush (stream);

#else
    {
        /* Notes about the file-position indicator:
           1) The file position indicator is incremented by fgetc() and decremented
              by ungetc():
              <http://www.opengroup.org/susv3/functions/fgetc.html>
                "... the fgetc() function shall ... advance the associated file
                 position indicator for the stream ..."
              <http://www.opengroup.org/susv3/functions/ungetc.html>
                "The file-position indicator is decremented by each successful
                 call to ungetc()..."
           2) <http://www.opengroup.org/susv3/functions/ungetc.html> says:
                "The value of the file-position indicator for the stream after
                 reading or discarding all pushed-back bytes shall be the same
                 as it was before the bytes were pushed back."
              Here we are discarding all pushed-back bytes.  But more specifically,
           3) <http://www.opengroup.org/austin/aardvark/latest/xshbug3.txt> says:
                "[After fflush(),] the file offset of the underlying open file
                 description shall be set to the file position of the stream, and
                 any characters pushed back onto the stream by ungetc() ... shall
                 be discarded."  */

        /* POSIX does not specify fflush behavior for non-seekable input
           streams.  Some implementations purge unread data, some return
           EBADF, some do nothing.  */
        off_t pos = ftello (stream);
        if (pos == -1)
        {
            errno = EBADF;
            return EOF;
        }

        /* Clear the ungetc buffer.  */
        clear_ungetc_buffer (stream);

        /* To get here, we must be flushing a seekable input stream, so the
           semantics of fpurge are now appropriate to clear the buffer.  To
           avoid losing data, the lseek is also necessary.  */
        {
            int result = fpurge (stream);
            if (result != 0)
                return result;
        }

# if (defined __sferror || defined __DragonFly__) && defined __SNPT /* FreeBSD, NetBSD, OpenBSD, DragonFly, MacOS X, Cygwin */

        {
            /* Disable seek optimization for the next fseeko call.  This tells the
               following fseeko call to seek to the desired position directly, rather
               than to seek to a block-aligned boundary.  */
            int saved_flags = disable_seek_optimization (stream);
            int result = fseeko (stream, pos, SEEK_SET);

            restore_seek_optimization (stream, saved_flags);
            return result;
        }

# else

        pos = lseek (fileno (stream), pos, SEEK_SET);
        if (pos == -1)
            return EOF;
        /* After a successful lseek, update the file descriptor's position cache
           in the stream.  */
        update_fpos_cache (stream, pos);

        return 0;

# endif
    }
#endif
}