Blame lib/ftello.c

Packit 8f70b4
/* An ftello() function that works around platform bugs.
Packit 8f70b4
   Copyright (C) 2007, 2009-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <stdio.h>
Packit 8f70b4
Packit 8f70b4
/* Get lseek.  */
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
Packit 8f70b4
#include "stdio-impl.h"
Packit 8f70b4
Packit 8f70b4
off_t
Packit 8f70b4
ftello (FILE *fp)
Packit 8f70b4
#undef ftello
Packit 8f70b4
#if !HAVE_FTELLO
Packit 8f70b4
# undef ftell
Packit 8f70b4
# define ftello ftell
Packit 8f70b4
#endif
Packit 8f70b4
#if _GL_WINDOWS_64_BIT_OFF_T
Packit 8f70b4
# undef ftello
Packit 8f70b4
# if HAVE__FTELLI64 /* msvc, mingw64 */
Packit 8f70b4
#  define ftello _ftelli64
Packit 8f70b4
# else /* mingw */
Packit 8f70b4
#  define ftello ftello64
Packit 8f70b4
# endif
Packit 8f70b4
#endif
Packit 8f70b4
{
Packit 8f70b4
#if LSEEK_PIPE_BROKEN
Packit 8f70b4
  /* mingw gives bogus answers rather than failure on non-seekable files.  */
Packit 8f70b4
  if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
Packit 8f70b4
    return -1;
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE /* Solaris */
Packit 8f70b4
  /* The Solaris stdio leaves the _IOREAD flag set after reading from a file
Packit 8f70b4
     reaches EOF and the program then starts writing to the file.  ftello
Packit 8f70b4
     gets confused by this.  */
Packit 8f70b4
  if (fp_->_flag & _IOWRT)
Packit 8f70b4
    {
Packit 8f70b4
      off_t pos;
Packit 8f70b4
Packit 8f70b4
      /* Call ftello nevertheless, for the side effects that it does on fp.  */
Packit 8f70b4
      ftello (fp);
Packit 8f70b4
Packit 8f70b4
      /* Compute the file position ourselves.  */
Packit 8f70b4
      pos = lseek (fileno (fp), (off_t) 0, SEEK_CUR);
Packit 8f70b4
      if (pos >= 0)
Packit 8f70b4
        {
Packit 8f70b4
          if ((fp_->_flag & _IONBF) == 0 && fp_->_base != NULL)
Packit 8f70b4
            pos += fp_->_ptr - fp_->_base;
Packit 8f70b4
        }
Packit 8f70b4
      return pos;
Packit 8f70b4
    }
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if defined __SL64 && defined __SCLE /* Cygwin */
Packit 8f70b4
  if ((fp->_flags & __SL64) == 0)
Packit 8f70b4
    {
Packit 8f70b4
      /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
Packit 8f70b4
         mode; but has an ftello that requires 64-bit mode.  */
Packit 8f70b4
      FILE *tmp = fopen ("/dev/null", "r");
Packit 8f70b4
      if (!tmp)
Packit 8f70b4
        return -1;
Packit 8f70b4
      fp->_flags |= __SL64;
Packit 8f70b4
      fp->_seek64 = tmp->_seek64;
Packit 8f70b4
      fclose (tmp);
Packit 8f70b4
    }
Packit 8f70b4
#endif
Packit 8f70b4
  return ftello (fp);
Packit 8f70b4
}