t5505: On MinGW, use Windows path in expected output of "git remote show"
[git/mingw.git] / write_or_die.c
blobf3b8566ab85515f008ebedfc0c08b1d09f6aef4d
1 #include "cache.h"
3 /*
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;
19 struct stat st;
20 char *cp;
22 if (f == stdout) {
23 if (skip_stdout_flush < 0) {
24 cp = getenv("GIT_FLUSH");
25 if (cp)
26 skip_stdout_flush = (atoi(cp) == 0);
27 else if ((fstat(fileno(stdout), &st) == 0) &&
28 S_ISREG(st.st_mode))
29 skip_stdout_flush = 1;
30 else
31 skip_stdout_flush = 0;
33 if (skip_stdout_flush && !ferror(f))
34 return;
36 if (fflush(f)) {
38 * On Windows, EPIPE is returned only by the first write()
39 * after the reading end has closed its handle; subsequent
40 * write()s return EINVAL.
42 if (errno == EPIPE || errno == EINVAL)
43 exit(0);
44 die("write failure on %s: %s", desc, strerror(errno));
48 int read_in_full(int fd, void *buf, size_t count)
50 char *p = buf;
51 ssize_t total = 0;
53 while (count > 0) {
54 ssize_t loaded = xread(fd, p, count);
55 if (loaded <= 0)
56 return total ? total : loaded;
57 count -= loaded;
58 p += loaded;
59 total += loaded;
62 return total;
65 int write_in_full(int fd, const void *buf, size_t count)
67 const char *p = buf;
68 ssize_t total = 0;
70 while (count > 0) {
71 ssize_t written = xwrite(fd, p, count);
72 if (written < 0)
73 return -1;
74 if (!written) {
75 errno = ENOSPC;
76 return -1;
78 count -= written;
79 p += written;
80 total += written;
83 return total;
86 void write_or_die(int fd, const void *buf, size_t count)
88 if (write_in_full(fd, buf, count) < 0) {
89 if (errno == EPIPE)
90 exit(0);
91 die("write error (%s)", strerror(errno));
95 int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
97 if (write_in_full(fd, buf, count) < 0) {
98 if (errno == EPIPE)
99 exit(0);
100 fprintf(stderr, "%s: write error (%s)\n",
101 msg, strerror(errno));
102 return 0;
105 return 1;
108 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
110 if (write_in_full(fd, buf, count) < 0) {
111 fprintf(stderr, "%s: write error (%s)\n",
112 msg, strerror(errno));
113 return 0;
116 return 1;