Mercurial > gnulib
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 |
rev | line source |
---|---|
8868 | 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 | 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 | 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 | 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 | |
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 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
8868 | 16 |
17 #include <config.h> | |
18 | |
19 /* Specification. */ | |
20 #include <stdio.h> | |
21 | |
8872 | 22 /* Get lseek. */ |
23 #include <unistd.h> | |
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 | 29 #undef ftello |
30 #if !HAVE_FTELLO | |
8872 | 31 # undef ftell |
8868 | 32 # define ftello ftell |
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 | 42 { |
8872 | 43 #if LSEEK_PIPE_BROKEN |
44 /* mingw gives bogus answers rather than failure on non-seekable files. */ | |
45 if (lseek (fileno (fp), 0, SEEK_CUR) == -1) | |
46 return -1; | |
47 #endif | |
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 | 71 #if defined __SL64 && defined __SCLE /* Cygwin */ |
72 if ((fp->_flags & __SL64) == 0) | |
73 { | |
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 | 76 FILE *tmp = fopen ("/dev/null", "r"); |
77 if (!tmp) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
78 return -1; |
8868 | 79 fp->_flags |= __SL64; |
80 fp->_seek64 = tmp->_seek64; | |
81 fclose (tmp); | |
82 } | |
83 #endif | |
84 return ftello (fp); | |
85 } |