Hash :
222b0486
Author :
Date :
2005-09-19T17:28:14
Use a consistent style for including <config.h>. * __fpending.c, acl.c, argmatch.c, argp-help.c, argp-parse.c, argp-pvh.c, backupfile.c, basename.c, c-stack.c, calloc.c, check-version.c, cloexec.c, closeout.c, copy-file.c, creat-safer.c, cycle-check.c, dirfd.c, dirname.c, dup-safer.c, dup2.c, euidaccess.c, exclude.c, exitfail.c, fatal-signal.c, fd-safer.c, file-type.c, fileblocks.c, filemode.c, filenamecat.c, findprog.c, fnmatch.c, fopen-safer.c, free.c, fsusage.c, ftruncate.c, full-write.c, fwriteerror.c, getaddrinfo.c, getcwd.c, getdelim.c, getline.c, getlogin_r.c, getndelim2.c, getnline.c, getopt1.c, getpass.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c, human.c, idcache.c, inet_ntop.c, isdir.c, long-options.c, malloc.c, memcasecmp.c, memcmp.c, memcoll.c, memcpy.c, memmove.c, mkdir-p.c, modechange.c, mountlist.c, open-safer.c, physmem.c, pipe-safer.c, pipe.c, poll.c, posixver.c, progname.c, progreloc.c, putenv.c, quote.c, quotearg.c, readline.c, readlink.c, realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c, same.c, save-cwd.c, savedir.c, sig2str.c, strcspn.c, strerror.c, stripslash.c, strncasecmp.c, strndup.c, strnlen.c, strnlen1.c, strsep.c, strstr.c, strtod.c, strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c, userspec.c, utimecmp.c, version-etc-fsf.c, version-etc.c, wait-process.c, xalloc-die.c, xgetcwd.c, xmalloc.c, xmemcoll.c, xnanosleep.c, xreadlink.c, xsetenv.c, xstrndup.c, xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Standardize inclusion of config.h. * __fpending.h, dirfd.h, getdate.h, human.h, inttostr.h: Removed inclusion of config.h from header files. * inttostr.c: Adjusted in-tree users. * timespec.h: Remove superfluous warning to include config.h. * atexit.c, chdir-long.c chown.c, fchown-stub.c, getgroups.c, gettimeofday.c, lchown.c, lstat.c, mkdir.c, mkstemp.c, nanosleep.c, openat.c, raise.c, readtokens0.c, readutmp.c, unlinkdir.c: Guard inclusion of config.h with HAVE_CONFIG_H.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* Detect write error on a stream.
Copyright (C) 2003-2005 Free Software Foundation, Inc.
Written by Bruno Haible <bruno@clisp.org>, 2003.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
/* Specification. */
#include "fwriteerror.h"
#include <errno.h>
#include <stdbool.h>
int
fwriteerror (FILE *fp)
{
/* State to allow multiple calls to fwriteerror (stdout). */
static bool stdout_closed = false;
if (fp == stdout && stdout_closed)
return 0;
/* Need to
1. test the error indicator of the stream,
2. flush the buffers both in userland and in the kernel, through fclose,
testing for error again. */
/* Clear errno, so that on non-POSIX systems the caller doesn't see a
wrong value of errno when we return -1. */
errno = 0;
if (ferror (fp))
{
if (fflush (fp))
return -1; /* errno is set here */
/* The stream had an error earlier, but its errno was lost. If the
error was not temporary, we can get the same errno by writing and
flushing one more byte. We can do so because at this point the
stream's contents is garbage anyway. */
if (fputc ('\0', fp) == EOF)
return -1; /* errno is set here */
if (fflush (fp))
return -1; /* errno is set here */
/* Give up on errno. */
errno = 0;
return -1;
}
/* If we are closing stdout, don't attempt to do it later again. */
if (fp == stdout)
stdout_closed = true;
if (fclose (fp))
return -1; /* errno is set here */
return 0;
}
#if TEST
/* Name of a file on which writing fails. On systems without /dev/full,
you can choose a filename on a full filesystem. */
#define UNWRITABLE_FILE "/dev/full"
int
main ()
{
static int sizes[] =
{
511, 512, 513,
1023, 1024, 1025,
2047, 2048, 2049,
4095, 4096, 4097,
8191, 8192, 8193
};
static char dummy[8193];
unsigned int i, j;
for (i = 0; i < sizeof (sizes) / sizeof (sizes[0]); i++)
{
size_t size = sizes[i];
for (j = 0; j < 2; j++)
{
/* Run a test depending on i and j:
Write size bytes and then calls fflush if j==1. */
FILE *stream = fopen (UNWRITABLE_FILE, "w");
if (stream == NULL)
{
fprintf (stderr, "Test %u:%u: could not open file\n", i, j);
continue;
}
fwrite (dummy, 347, 1, stream);
fwrite (dummy, size - 347, 1, stream);
if (j)
fflush (stream);
if (fwriteerror (stream) == -1)
{
if (errno != ENOSPC)
fprintf (stderr, "Test %u:%u: fwriteerror ok, errno = %d\n",
i, j, errno);
}
else
fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
i, j);
}
}
return 0;
}
#endif