4 * Some cases use stdio, but want to flush after the write
5 * to get error handling (and to get better interactive
6 * behaviour - not buffering excessively).
8 * Of course, if the flush happened within the write itself,
9 * we've already lost the error code, and cannot report it any
10 * more. So we just ignore that case instead (and hope we get
11 * the right error code on the flush).
13 * If the file handle is stdout, and stdout is a file, then skip the
14 * flush entirely since it's not needed.
16 void maybe_flush_or_die(FILE *f
, const char *desc
)
18 static int skip_stdout_flush
= -1;
23 if (skip_stdout_flush
< 0) {
24 cp
= getenv("GIT_FLUSH");
26 skip_stdout_flush
= (atoi(cp
) == 0);
27 else if ((fstat(fileno(stdout
), &st
) == 0) &&
29 skip_stdout_flush
= 1;
31 skip_stdout_flush
= 0;
33 if (skip_stdout_flush
&& !ferror(f
))
39 die("write failure on %s: %s", desc
, strerror(errno
));
43 ssize_t
read_in_full(int fd
, void *buf
, size_t count
)
49 ssize_t loaded
= xread(fd
, p
, count
);
51 return total
? total
: loaded
;
60 ssize_t
write_in_full(int fd
, const void *buf
, size_t count
)
66 ssize_t written
= xwrite(fd
, p
, count
);
81 void write_or_die(int fd
, const void *buf
, size_t count
)
83 if (write_in_full(fd
, buf
, count
) < 0) {
86 die("write error (%s)", strerror(errno
));
90 int write_or_whine_pipe(int fd
, const void *buf
, size_t count
, const char *msg
)
92 if (write_in_full(fd
, buf
, count
) < 0) {
95 fprintf(stderr
, "%s: write error (%s)\n",
96 msg
, strerror(errno
));
103 int write_or_whine(int fd
, const void *buf
, size_t count
, const char *msg
)
105 if (write_in_full(fd
, buf
, count
) < 0) {
106 fprintf(stderr
, "%s: write error (%s)\n",
107 msg
, strerror(errno
));