1 /*
2 * nbtest.c: read and write in non-blocking mode
3 * This should run with any Unix
4 *
5 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
6 * Copyright (C) 2001 O'Reilly & Associates
7 *
8 * The source code in this file can be freely used, adapted,
9 * and redistributed in source or binary form, so long as an
10 * acknowledgment appears in derived source files. The citation
11 * should list that the code comes from the book "Linux Device
12 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
13 * by O'Reilly & Associates. No warranty is attached;
14 * we cannot take responsibility for errors or fitness for use.
15 */
16
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <errno.h>
22
23 char buffer[4096];
24
25 int main(int argc, char **argv)
26 {
27 int delay = 1, n, m = 0;
28
29 if (argc > 1)
30 delay=atoi(argv[1]);
31 fcntl(0, F_SETFL, fcntl(0,F_GETFL) | O_NONBLOCK); /* stdin */
32 fcntl(1, F_SETFL, fcntl(1,F_GETFL) | O_NONBLOCK); /* stdout */
33
34 while (1) {
35 n = read(0, buffer, 4096);
36 if (n >= 0)
37 m = write(1, buffer, n);
38 if ((n < 0 || m < 0) && (errno != EAGAIN))
39 break;
40 sleep(delay);
41 }
42 perror(n < 0 ? "stdin" : "stdout");
43 exit(1);
44 }
45
|
This page was automatically generated by the
LXR engine.
|