annotate output.c @ 20:40748b097655

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