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
|
19 jelson 1.1 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <errno.h>
26
27 int main(int argc, char *argv[])
28 {
29 int fd, retval;
30 char buf[4096];
31
32 if (argc != 2) {
33 fprintf(stderr, "usage: %s <filename>\n", argv[0]);
34 exit(1);
35 }
36
37 if ((fd = open(argv[1], O_RDONLY | O_NONBLOCK)) < 0) {
38 perror(argv[1]);
39 exit(1);
40 jelson 1.1 }
41
42 while ((retval = read(fd, buf, sizeof(buf))) > 0) {
43 if (write(STDOUT_FILENO, buf, retval) < 0) {
44 perror("writing to stdout");
45 break;
46 }
47 }
48
49 if (retval < 0 && errno != EAGAIN) {
50 perror(argv[1]);
51 exit(1);
52 }
53
54 return 0;
55 }
|