Blame gl/ftello.c

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