bug-gnulib
[Top][All Lists]
Advanced

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

[bug-gnulib] API change of fwriteerror module


From: Bruno Haible
Subject: [bug-gnulib] API change of fwriteerror module
Date: Mon, 17 Jan 2005 13:54:37 +0100
User-agent: KMail/1.5

Hi,

After Jim's changes to the closeout module, fwriteerror also needed an
update. The fwriteerror() function now needs to fclose() the stream,
otherwise it cannot detect write failure reliably on NFS. Code that used

    if (fwriteerror (fp))
      ... signal error ...
    fclose (fp);

now needs to drop the fclose call.

Bruno


2005-01-06  Bruno Haible  <address@hidden>

        * fwriteerror.h (fwriteerror): Change specification to include fclose.
        * fwriteerror.c: Include <stdbool.h>.
        (fwriteerror): At the end, close the file stream. Record whether
        stdout was already closed.

diff -r -c3 --exclude=ChangeLog GNULIB/gnulib-20050105/lib/fwriteerror.h 
/packages/gettext/gettext-work/gettext-cvs/gettext-tools/lib/fwriteerror.h
*** GNULIB/gnulib-20050105/lib/fwriteerror.h    2003-09-14 10:58:29.000000000 
+0200
--- /packages/gettext/gettext-work/gettext-cvs/gettext-tools/lib/fwriteerror.h  
2005-01-07 03:37:42.000000000 +0100
***************
*** 1,5 ****
  /* Detect write error on a stream.
!    Copyright (C) 2003 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, 2005 Free Software Foundation, Inc.
     Written by Bruno Haible <address@hidden>, 2003.
  
     This program is free software; you can redistribute it and/or modify
***************
*** 22,28 ****
       (a) Test the return value of every fwrite() or fprintf() call, and react
           immediately.
       (b) Just before fclose(), test the error indicator in the stream and
!          the return value of the final fflush() or fclose() call.
  
     The benefit of (a) is that non file related errors (such that ENOMEM during
     fprintf) and temporary error conditions can be diagnosed accurately.
--- 22,28 ----
       (a) Test the return value of every fwrite() or fprintf() call, and react
           immediately.
       (b) Just before fclose(), test the error indicator in the stream and
!          the return value of the final fclose() call.
  
     The benefit of (a) is that non file related errors (such that ENOMEM during
     fprintf) and temporary error conditions can be diagnosed accurately.
***************
*** 40,51 ****
  
  #include <stdio.h>
  
! /* Write out the not yet written buffered contents of the stream FP, and then
!    test whether some error occurred on the stream FP.  FP must be a stream
!    opened for writing.
!    Return 0 if no error occurred.  In this case it can be assumed that
!    fclose (fp) will succeed.
     Return -1 and set errno if there was an error.  The errno value will be 0
     if the cause of the error cannot be determined.
!  */
  extern int fwriteerror (FILE *fp);
--- 40,51 ----
  
  #include <stdio.h>
  
! /* Write out the not yet written buffered contents of the stream FP, close
!    the stream FP, and test whether some error occurred on the stream FP.
!    FP must be a stream opened for writing.
!    Return 0 if no error occurred and fclose (fp) succeeded.
     Return -1 and set errno if there was an error.  The errno value will be 0
     if the cause of the error cannot be determined.
!    For any given stream FP other than stdout, fwriteerror (FP) may only be
!    called once.  */
  extern int fwriteerror (FILE *fp);
diff -r -c3 --exclude=ChangeLog GNULIB/gnulib-20050105/lib/fwriteerror.c 
/packages/gettext/gettext-work/gettext-cvs/gettext-tools/lib/fwriteerror.c
*** GNULIB/gnulib-20050105/lib/fwriteerror.c    2003-09-14 11:24:27.000000000 
+0200
--- /packages/gettext/gettext-work/gettext-cvs/gettext-tools/lib/fwriteerror.c  
2005-01-07 03:37:42.000000000 +0100
***************
*** 1,5 ****
  /* Detect write error on a stream.
!    Copyright (C) 2003 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-2005 Free Software Foundation, Inc.
     Written by Bruno Haible <address@hidden>, 2003.
  
     This program is free software; you can redistribute it and/or modify
***************
*** 24,47 ****
  #include "fwriteerror.h"
  
  #include <errno.h>
  
  int
  fwriteerror (FILE *fp)
  {
    /* Need to
       1. test the error indicator of the stream,
!      2. flush the buffers (what fclose() would do), testing for error again.
!      We can equally well swap these steps; this leads to smaller code.  */
  
    /* 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 (fflush (fp))
-     return -1; /* errno is set here */
- 
    if (ferror (fp))
      {
        /* 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
--- 24,53 ----
  #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
***************
*** 55,60 ****
--- 61,73 ----
        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;
  }
  
***************
*** 109,118 ****
          else
            fprintf (stderr, "Test %u:%u: fwriteerror found no error!\n",
                     i, j);
- 
-         if (fclose (stream))
-           fprintf (stderr, "Test %u:%u: fclose failed, errno = %d\n",
-                    i, j, errno);
        }
      }
  
--- 122,127 ----





reply via email to

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