20
|
1 #include <unistd.h>
|
|
2 #include <fcntl.h>
|
|
3 #include <err.h>
|
|
4
|
|
5 #include "utils.h"
|
|
6 #include "mode.h"
|
|
7 #include "output.h"
|
|
8
|
|
9 static int outputfd = -1;
|
|
10
|
|
11 static
|
|
12 void
|
|
13 output_open(void)
|
|
14 {
|
|
15 outputfd = open(mode.output_file, O_WRONLY|O_CREAT|O_TRUNC, 0664);
|
|
16 if (outputfd < 0) {
|
|
17 warn("%s", mode.output_file);
|
|
18 die();
|
|
19 }
|
|
20 }
|
|
21
|
|
22 void
|
|
23 output(const char *buf, size_t len)
|
|
24 {
|
|
25 size_t done;
|
|
26 ssize_t result;
|
|
27 static unsigned write_errors = 0;
|
|
28
|
|
29 if (outputfd < 0) {
|
|
30 output_open();
|
|
31 }
|
|
32
|
|
33 /* XXX this will often come by ones and twos, should buffer */
|
|
34
|
|
35 done = 0;
|
|
36 while (done < len) {
|
|
37 result = write(outputfd, buf+done, len-done);
|
|
38 if (result == -1) {
|
|
39 warn("%s: write", mode.output_file);
|
|
40 complain_failed();
|
|
41 write_errors++;
|
|
42 if (write_errors > 5) {
|
|
43 warnx("%s: giving up", mode.output_file);
|
|
44 die();
|
|
45 }
|
|
46 /* XXX is this really a good idea? */
|
|
47 sleep(1);
|
|
48 }
|
|
49 done += (size_t)result;
|
|
50 }
|
|
51 }
|
|
52
|
|
53 void
|
|
54 output_eof(void)
|
|
55 {
|
|
56 if (outputfd >= 0) {
|
|
57 close(outputfd);
|
|
58 }
|
|
59 outputfd = -1;
|
|
60 }
|