]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - include/closestream.h
1 #ifndef UTIL_LINUX_CLOSESTREAM_H
2 #define UTIL_LINUX_CLOSESTREAM_H
5 #ifdef HAVE_STDIO_EXT_H
13 #ifndef CLOSE_EXIT_CODE
14 # define CLOSE_EXIT_CODE EXIT_FAILURE
17 #ifndef HAVE___FPENDING
19 __fpending(FILE *stream
__attribute__((__unused__
)))
26 close_stream(FILE * stream
)
28 const int some_pending
= (__fpending(stream
) != 0);
29 const int prev_fail
= (ferror(stream
) != 0);
30 const int fclose_fail
= (fclose(stream
) != 0);
32 if (prev_fail
|| (fclose_fail
&& (some_pending
|| errno
!= EBADF
))) {
33 if (!fclose_fail
&& !(errno
== EPIPE
))
40 /* Meant to be used atexit(close_stdout); */
44 if (close_stream(stdout
) != 0 && !(errno
== EPIPE
)) {
46 warn(_("write error"));
48 warnx(_("write error"));
49 _exit(CLOSE_EXIT_CODE
);
52 if (close_stream(stderr
) != 0)
53 _exit(CLOSE_EXIT_CODE
);
58 fsync(int fd
__attribute__((__unused__
)))
67 const int fsync_fail
= (fsync(fd
) != 0);
68 const int close_fail
= (close(fd
) != 0);
70 if (fsync_fail
|| close_fail
)
75 #endif /* UTIL_LINUX_CLOSESTREAM_H */