~ubuntu-branches/ubuntu/utopic/coreutils/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/fclose.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2012-11-28 03:03:42 UTC
  • mfrom: (8.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20121128030342-21zanj8354gas5gr
Tags: 8.20-3ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - Make 'uname -i -p' return the real processor/hardware, instead of
    unknown.
  - Build-depend on gettext:any instead of on gettext, so that apt-get can
    properly resolve build-dependencies on the tool when cross-building.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* fclose replacement.
2
 
   Copyright (C) 2008-2011 Free Software Foundation, Inc.
 
2
   Copyright (C) 2008-2012 Free Software Foundation, Inc.
3
3
 
4
4
   This program is free software: you can redistribute it and/or modify
5
5
   it under the terms of the GNU General Public License as published by
23
23
#include <unistd.h>
24
24
 
25
25
#include "freading.h"
 
26
#include "msvc-inval.h"
 
27
 
 
28
#undef fclose
 
29
 
 
30
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
 
31
static int
 
32
fclose_nothrow (FILE *fp)
 
33
{
 
34
  int result;
 
35
 
 
36
  TRY_MSVC_INVAL
 
37
    {
 
38
      result = fclose (fp);
 
39
    }
 
40
  CATCH_MSVC_INVAL
 
41
    {
 
42
      result = EOF;
 
43
      errno = EBADF;
 
44
    }
 
45
  DONE_MSVC_INVAL;
 
46
 
 
47
  return result;
 
48
}
 
49
#else
 
50
# define fclose_nothrow fclose
 
51
#endif
26
52
 
27
53
/* Override fclose() to call the overridden fflush() or close().  */
28
54
 
29
55
int
30
56
rpl_fclose (FILE *fp)
31
 
#undef fclose
32
57
{
33
58
  int saved_errno = 0;
34
59
  int fd;
37
62
  /* Don't change behavior on memstreams.  */
38
63
  fd = fileno (fp);
39
64
  if (fd < 0)
40
 
    return fclose (fp);
 
65
    return fclose_nothrow (fp);
41
66
 
42
67
  /* We only need to flush the file if it is not reading or if it is
43
68
     seekable.  This only guarantees the file position of input files
55
80
  if (close (fd) < 0 && saved_errno == 0)
56
81
    saved_errno = errno;
57
82
 
58
 
  fclose (fp); /* will fail with errno = EBADF, if we did not lose a race */
 
83
  fclose_nothrow (fp); /* will fail with errno = EBADF,
 
84
                          if we did not lose a race */
59
85
 
60
86
#else /* !WINDOWS_SOCKETS */
61
87
  /* Call fclose() and invoke all hooks of the overridden close().  */
64
90
  /* Note about multithread-safety: There is a race condition here as well.
65
91
     Some other thread could open fd between our calls to fclose and
66
92
     _gl_unregister_fd.  */
67
 
  result = fclose (fp);
 
93
  result = fclose_nothrow (fp);
68
94
  if (result == 0)
69
95
    _gl_unregister_fd (fd);
70
96
# else
71
97
  /* No race condition here.  */
72
 
  result = fclose (fp);
 
98
  result = fclose_nothrow (fp);
73
99
# endif
74
100
 
75
101
#endif /* !WINDOWS_SOCKETS */