annotate lib/fclose.c @ 10622:ac6f431cc95d

New module 'fclose'.
author Bruno Haible <bruno@clisp.org>
date Sat, 11 Oct 2008 14:18:29 +0200
parents
children cce8e54214d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10622
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* close replacement.
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2008 Free Software Foundation, Inc.
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <stdio.h>
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <errno.h>
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <unistd.h>
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 /* Override fclose() to call the overridden close(). */
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 int
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 rpl_fclose (FILE *fp)
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #undef fclose
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 {
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 int saved_errno = 0;
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 if (fflush (fp))
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 saved_errno = errno;
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 if (close (fileno (fp)) < 0 && saved_errno == 0)
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 saved_errno = errno;
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 fclose (fp); /* will fail with errno = EBADF */
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 if (saved_errno != 0)
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 {
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 errno = saved_errno;
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 return EOF;
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 }
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 return 0;
ac6f431cc95d New module 'fclose'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 }