1 jelson 1.1 /*
2 * nbcat: a simple program similar to 'cat', but which uses
3 * nonblocking reads.
4 *
5 * This utility can be used to copy the current contents of an emlog
6 * device without blocking to wait for more input. For example:
7 *
8 * nbcat /var/log/emlog-device-instance > /tmp/saved-log-file
9 *
10 * ...will copy the current contents of the named emlog device to a
11 * file in /tmp.
12 *
13 * This code is freeware and may be distributed without restriction.
14 *
15 * Jeremy Elson <jelson@circlemud.org>
16 * August 11, 2001
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 jelson 1.1 #include <fcntl.h>
23 #include <errno.h>
24
25 int main(int argc, char *argv[])
26 {
27 int fd, retval;
28 char buf[4096];
29
30 if (argc != 2) {
31 fprintf(stderr, "usage: %s <filename>\n", argv[0]);
32 exit(1);
33 }
34
35 if ((fd = open(argv[1], O_RDONLY | O_NONBLOCK)) < 0) {
36 perror(argv[1]);
37 exit(1);
38 }
39
40 while ((retval = read(fd, buf, sizeof(buf))) > 0) {
41 if (write(STDOUT_FILENO, buf, retval) < 0) {
42 perror("writing to stdout");
43 jelson 1.1 break;
44 }
45 }
46
47 if (retval < 0 && errno != EAGAIN) {
48 perror(argv[1]);
49 exit(1);
50 }
51
52 return 0;
53 }
|