X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Ffwriteerror.c;h=bb881a5bf3eda0b0f5a64704253cab13118804d0;hb=23eecb48e39afd0d267d64d40ba6bf97aa865e13;hp=21bd09804e7c0b35d4013588e95751be9583c5eb;hpb=0215a9d0af452e1851385549d69d4af6a4106183;p=gnulib.git diff --git a/lib/fwriteerror.c b/lib/fwriteerror.c index 21bd09804..bb881a5bf 100644 --- a/lib/fwriteerror.c +++ b/lib/fwriteerror.c @@ -1,5 +1,5 @@ /* Detect write error on a stream. - Copyright (C) 2003-2006, 2008 Free Software Foundation, Inc. + Copyright (C) 2003-2006, 2008-2013 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software: you can redistribute it and/or modify @@ -32,7 +32,7 @@ do_fwriteerror (FILE *fp, bool ignore_ebadf) if (fp == stdout) { if (stdout_closed) - return 0; + return 0; /* If we are closing stdout, don't attempt to do it later again. */ stdout_closed = true; @@ -41,14 +41,14 @@ do_fwriteerror (FILE *fp, bool ignore_ebadf) /* This function returns an error indication if there was a previous failure or if fclose failed, with two exceptions: - Ignore an fclose failure if there was no previous error, no data - remains to be flushed, and fclose failed with EBADF. That can - happen when a program like cp is invoked like this `cp a b >&-' - (i.e., with standard output closed) and doesn't generate any - output (hence no previous error and nothing to be flushed). + remains to be flushed, and fclose failed with EBADF. That can + happen when a program like cp is invoked like this 'cp a b >&-' + (i.e., with standard output closed) and doesn't generate any + output (hence no previous error and nothing to be flushed). - Ignore an fclose failure due to EPIPE. That can happen when a - program blocks or ignores SIGPIPE, and the output pipe or socket - has no readers now. The EPIPE tells us that we should stop writing - to this output. That's what we are doing anyway here. + program blocks or ignores SIGPIPE, and the output pipe or socket + has no readers now. The EPIPE tells us that we should stop writing + to this output. That's what we are doing anyway here. Need to 1. test the error indicator of the stream, @@ -62,15 +62,15 @@ do_fwriteerror (FILE *fp, bool ignore_ebadf) if (ferror (fp)) { if (fflush (fp)) - goto close_preserving_errno; /* errno is set here */ + goto close_preserving_errno; /* 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. */ + 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) - goto close_preserving_errno; /* errno is set here */ + goto close_preserving_errno; /* errno is set here */ if (fflush (fp)) - goto close_preserving_errno; /* errno is set here */ + goto close_preserving_errno; /* errno is set here */ /* Give up on errno. */ errno = 0; goto close_preserving_errno; @@ -79,16 +79,16 @@ do_fwriteerror (FILE *fp, bool ignore_ebadf) if (ignore_ebadf) { /* We need an explicit fflush to tell whether some output was already - done on FP. */ + done on FP. */ if (fflush (fp)) - goto close_preserving_errno; /* errno is set here */ + goto close_preserving_errno; /* errno is set here */ if (fclose (fp) && errno != EBADF) - goto got_errno; /* errno is set here */ + goto got_errno; /* errno is set here */ } else { if (fclose (fp)) - goto got_errno; /* errno is set here */ + goto got_errno; /* errno is set here */ } return 0; @@ -125,7 +125,7 @@ fwriteerror_no_ebadf (FILE *fp) #if TEST /* Name of a file on which writing fails. On systems without /dev/full, - you can choose a filename on a full filesystem. */ + you can choose a filename on a full file system. */ #define UNWRITABLE_FILE "/dev/full" int @@ -147,32 +147,32 @@ main () 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); - } + { + /* 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;