annotate lib/ftello.c @ 40186:8964917f9574

autoupdate
author Karl Berry <karl@freefriends.org>
date Mon, 18 Feb 2019 08:02:49 -0800
parents b06060465f09
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* An ftello() function that works around platform bugs.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2007, 2009-2019 Free Software Foundation, Inc.
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8872
diff changeset
4 This program is free software: you can redistribute it and/or modify
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8872
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8872
diff changeset
7 (at your option) any later version.
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8872
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 #include <config.h>
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 /* Specification. */
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20 #include <stdio.h>
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21
8872
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
22 /* Get lseek. */
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
23 #include <unistd.h>
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
24
13294
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
25 #include "stdio-impl.h"
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
26
13122
01741ff1a40e ftello: Fix C++ test error on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
27 off_t
01741ff1a40e ftello: Fix C++ test error on mingw.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
28 ftello (FILE *fp)
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29 #undef ftello
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 #if !HAVE_FTELLO
8872
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
31 # undef ftell
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 # define ftello ftell
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33 #endif
16795
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
34 #if _GL_WINDOWS_64_BIT_OFF_T
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
35 # undef ftello
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
36 # if HAVE__FTELLI64 /* msvc, mingw64 */
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
37 # define ftello _ftelli64
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
38 # else /* mingw */
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
39 # define ftello ftello64
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
40 # endif
6a9ea5d7b466 Large File Support for native Windows platforms.
Bruno Haible <bruno@clisp.org>
parents: 16201
diff changeset
41 #endif
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 {
8872
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
43 #if LSEEK_PIPE_BROKEN
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
44 /* mingw gives bogus answers rather than failure on non-seekable files. */
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
45 if (lseek (fileno (fp), 0, SEEK_CUR) == -1)
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
46 return -1;
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
47 #endif
dfee3b4fd46c Fix lseek on mingw.
Eric Blake <ebb9@byu.net>
parents: 8868
diff changeset
48
13294
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
49 #if FTELLO_BROKEN_AFTER_SWITCHING_FROM_READ_TO_WRITE /* Solaris */
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
50 /* The Solaris stdio leaves the _IOREAD flag set after reading from a file
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
51 reaches EOF and the program then starts writing to the file. ftello
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
52 gets confused by this. */
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
53 if (fp_->_flag & _IOWRT)
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
54 {
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
55 off_t pos;
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
56
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
57 /* Call ftello nevertheless, for the side effects that it does on fp. */
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
58 ftello (fp);
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
59
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
60 /* Compute the file position ourselves. */
14064
d39d9d84e5e2 ftello: avoid compilation failure with SunStudio c89
Eric Blake <eblake@redhat.com>
parents: 13294
diff changeset
61 pos = lseek (fileno (fp), (off_t) 0, SEEK_CUR);
13294
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
62 if (pos >= 0)
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
63 {
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
64 if ((fp_->_flag & _IONBF) == 0 && fp_->_base != NULL)
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
65 pos += fp_->_ptr - fp_->_base;
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
66 }
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
67 return pos;
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
68 }
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
69 #endif
4263e09100fb ftell, ftello: Work around Solaris bug.
Bruno Haible <bruno@clisp.org>
parents: 13122
diff changeset
70
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 #if defined __SL64 && defined __SCLE /* Cygwin */
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72 if ((fp->_flags & __SL64) == 0)
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 {
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 /* Cygwin 1.5.0 through 1.5.24 failed to open stdin in 64-bit
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
75 mode; but has an ftello that requires 64-bit mode. */
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
76 FILE *tmp = fopen ("/dev/null", "r");
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 if (!tmp)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
78 return -1;
8868
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 fp->_flags |= __SL64;
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80 fp->_seek64 = tmp->_seek64;
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
81 fclose (tmp);
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 }
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
83 #endif
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 return ftello (fp);
90e71310a07f Fix fseeko/ftello on cygwin 1.5.24.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
85 }