bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] fwriteerror() interface


From: Bruno Haible
Subject: Re: [bug-gnulib] fwriteerror() interface
Date: Mon, 24 Apr 2006 14:00:09 +0200
User-agent: KMail/1.5

Ben Pfaff wrote on 2006-02-27:
> I'm not sure I understand the fwriteerror() interface.

The basic idea is to be able to write:

    output_file = fopen (file_name, "w");
    if (output_file == NULL)
      error (EXIT_FAILURE, errno, _("error while opening \"%s\" for writing"),
             file_name);

    ... /* Lots of fputc and fprintf calls to output_file */

    /* Make sure nothing went wrong.  */
    if (fwriteerror (output_file))
      error (EXIT_FAILURE, errno, _("error while writing \"%s\" file"),
             file_name);

The function fwriteerror does everything that's necessary.

> As I read
> the function comment, fwriteerror() should always close the
> stream provided to it (except in the case of stdout).  But the
> implementation does not close the stream if an error was reported
> on the stream.

True. I probably missed that because in my uses of fwriteerror(),
I use  error (EXIT_FAILURE ...)  in case of failure, and the state of
the stream doesn't matter in this case.

Thanks!

2006-04-23  Bruno Haible  <address@hidden>

        * fwriteerror.c (fwriteerror): Call fclose also when an error
        condition was already detected.
        Reported by Ben Pfaff <address@hidden>.

*** fwriteerror.c.bak   2005-09-21 13:08:20.000000000 +0200
--- fwriteerror.c       2006-04-24 01:38:14.000000000 +0200
***************
*** 1,5 ****
  /* Detect write error on a stream.
!    Copyright (C) 2003-2005 Free Software Foundation, Inc.
     Written by Bruno Haible <address@hidden>, 2003.
  
     This program is free software; you can redistribute it and/or modify
--- 1,5 ----
  /* Detect write error on a stream.
!    Copyright (C) 2003-2006 Free Software Foundation, Inc.
     Written by Bruno Haible <address@hidden>, 2003.
  
     This program is free software; you can redistribute it and/or modify
***************
*** 47,64 ****
    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.  */
--- 47,72 ----
    if (ferror (fp))
      {
        if (fflush (fp))
!       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.  */
        if (fputc ('\0', fp) == EOF)
!       goto close_preserving_errno; /* errno is set here */
        if (fflush (fp))
!       goto close_preserving_errno; /* errno is set here */
        /* Give up on errno.  */
        errno = 0;
!      close_preserving_errno:
!       /* There's an error.  Nevertheless call fclose(fp), for consistency
!        with the other cases.  */
!       {
!       int saved_errno = errno;
!       fclose (fp);
!       errno = saved_errno;
!       return -1;
!       }
      }
  
    /* If we are closing stdout, don't attempt to do it later again.  */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]