close-stream: declare local scalars to be "const"
[gnulib.git] / lib / ftello.c
1 /* An ftello() function that works around platform bugs.
2    Copyright (C) 2007, 2009-2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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 General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 /* Specification.  */
20 #include <stdio.h>
21
22 /* Get lseek.  */
23 #include <unistd.h>
24
25 off_t
26 ftello (FILE *fp)
27 #undef ftello
28 #if !HAVE_FTELLO
29 # undef ftell
30 # define ftello ftell
31 #endif
32 {
33 #if LSEEK_PIPE_BROKEN
34   /* mingw gives bogus answers rather than failure on non-seekable files.  */
35   if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
36     return -1;
37 #endif
38
39 #if defined __SL64 && defined __SCLE /* Cygwin */
40   if ((fp->_flags & __SL64) == 0)
41     {
42       /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
43          mode; but has an ftello that requires 64-bit mode.  */
44       FILE *tmp = fopen ("/dev/null", "r");
45       if (!tmp)
46         return -1;
47       fp->_flags |= __SL64;
48       fp->_seek64 = tmp->_seek64;
49       fclose (tmp);
50     }
51 #endif
52   return ftello (fp);
53 }