ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libeio/demo.c
Revision: 1.3
Committed: Sat Mar 20 18:18:50 2010 UTC (14 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_7, rel-3_65
Changes since 1.2: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include <stdio.h>
2     #include <stdlib.h>
3     #include <unistd.h>
4     #include <poll.h>
5     #include <string.h>
6     #include <assert.h>
7     #include <fcntl.h>
8     #include <sys/types.h>
9     #include <sys/stat.h>
10    
11     #include "eio.h"
12    
13     int respipe [2];
14    
15     void
16     want_poll (void)
17     {
18     char dummy;
19     printf ("want_poll ()\n");
20     write (respipe [1], &dummy, 1);
21     }
22    
23     void
24     done_poll (void)
25     {
26     char dummy;
27     printf ("done_poll ()\n");
28     read (respipe [0], &dummy, 1);
29     }
30    
31     void
32     event_loop (void)
33     {
34     // an event loop. yeah.
35     struct pollfd pfd;
36     pfd.fd = respipe [0];
37     pfd.events = POLLIN;
38    
39     printf ("\nentering event loop\n");
40     while (eio_nreqs ())
41     {
42     poll (&pfd, 1, -1);
43     printf ("eio_poll () = %d\n", eio_poll ());
44     }
45     printf ("leaving event loop\n");
46     }
47    
48     int
49     res_cb (eio_req *req)
50     {
51     printf ("res_cb(%d|%s) = %d\n", req->type, req->data ? req->data : "?", EIO_RESULT (req));
52    
53     if (req->result < 0)
54     abort ();
55    
56     return 0;
57     }
58    
59     int
60     readdir_cb (eio_req *req)
61     {
62     char *buf = (char *)EIO_BUF (req);
63    
64     printf ("readdir_cb = %d\n", EIO_RESULT (req));
65    
66     if (EIO_RESULT (req) < 0)
67     return 0;
68    
69     while (EIO_RESULT (req)--)
70     {
71     printf ("readdir = <%s>\n", buf);
72     buf += strlen (buf) + 1;
73     }
74    
75     return 0;
76     }
77    
78     int
79     stat_cb (eio_req *req)
80     {
81     struct stat *buf = EIO_STAT_BUF (req);
82    
83     if (req->type == EIO_FSTAT)
84     printf ("fstat_cb = %d\n", EIO_RESULT (req));
85     else
86     printf ("stat_cb(%s) = %d\n", EIO_PATH (req), EIO_RESULT (req));
87    
88     if (!EIO_RESULT (req))
89     printf ("stat size %d perm 0%o\n", buf->st_size, buf->st_mode & 0777);
90    
91     return 0;
92     }
93    
94     int
95     read_cb (eio_req *req)
96     {
97     unsigned char *buf = (unsigned char *)EIO_BUF (req);
98    
99     printf ("read_cb = %d (%02x%02x%02x%02x %02x%02x%02x%02x)\n",
100     EIO_RESULT (req),
101     buf [0], buf [1], buf [2], buf [3],
102     buf [4], buf [5], buf [6], buf [7]);
103    
104     return 0;
105     }
106    
107     int last_fd;
108    
109     int
110     open_cb (eio_req *req)
111     {
112     printf ("open_cb = %d\n", EIO_RESULT (req));
113    
114     last_fd = EIO_RESULT (req);
115    
116     return 0;
117     }
118    
119     int
120     main (void)
121     {
122     printf ("pipe ()\n");
123     if (pipe (respipe)) abort ();
124    
125     printf ("eio_init ()\n");
126     if (eio_init (want_poll, done_poll)) abort ();
127    
128     do
129     {
130     /* avoid relative paths yourself(!) */
131 root 1.2 eio_mkdir ("eio-test-dir", 0777, 0, res_cb, "mkdir");
132     eio_nop (0, res_cb, "nop");
133 root 1.1 event_loop ();
134    
135 root 1.2 eio_stat ("eio-test-dir", 0, stat_cb, "stat");
136     eio_lstat ("eio-test-dir", 0, stat_cb, "stat");
137     eio_open ("eio-test-dir/eio-test-file", O_RDWR | O_CREAT, 0777, 0, open_cb, "open");
138     eio_symlink ("test", "eio-test-dir/eio-symlink", 0, res_cb, "symlink");
139     eio_mknod ("eio-test-dir/eio-fifo", S_IFIFO, 0, 0, res_cb, "mknod");
140     event_loop ();
141    
142     eio_utime ("eio-test-dir", 12345.678, 23456.789, 0, res_cb, "utime");
143     eio_futime (last_fd, 92345.678, 93456.789, 0, res_cb, "futime");
144     eio_chown ("eio-test-dir", getuid (), getgid (), 0, res_cb, "chown");
145     eio_fchown (last_fd, getuid (), getgid (), 0, res_cb, "fchown");
146     eio_fchmod (last_fd, 0123, 0, res_cb, "fchmod");
147 root 1.3 eio_readdir ("eio-test-dir", 0, 0, readdir_cb, "readdir");
148     eio_readdir ("/nonexistant", 0, 0, readdir_cb, "readdir");
149 root 1.2 eio_fstat (last_fd, 0, stat_cb, "stat");
150     eio_write (last_fd, "test\nfail\n", 10, 4, 0, res_cb, "write");
151     event_loop ();
152    
153     eio_read (last_fd, 0, 8, 0, EIO_PRI_DEFAULT, read_cb, "read");
154     eio_readlink ("eio-test-dir/eio-symlink", 0, res_cb, "readlink");
155     event_loop ();
156    
157     eio_dup2 (1, 2, EIO_PRI_DEFAULT, res_cb, "dup"); // dup stdout to stderr
158     eio_chmod ("eio-test-dir", 0765, 0, res_cb, "chmod");
159     eio_ftruncate (last_fd, 9, 0, res_cb, "ftruncate");
160     eio_fdatasync (last_fd, 0, res_cb, "fdatasync");
161     eio_fsync (last_fd, 0, res_cb, "fsync");
162     eio_sync (0, res_cb, "sync");
163     eio_busy (0.5, 0, res_cb, "busy");
164     event_loop ();
165    
166     eio_sendfile (1, last_fd, 4, 5, 0, res_cb, "sendfile"); // write "test\n" to stdout
167     eio_fstat (last_fd, 0, stat_cb, "stat");
168     event_loop ();
169    
170     eio_truncate ("eio-test-dir/eio-test-file", 6, 0, res_cb, "truncate");
171     eio_readahead (last_fd, 0, 64, 0, res_cb, "readahead");
172     event_loop ();
173    
174     eio_close (last_fd, 0, res_cb, "close");
175     eio_link ("eio-test-dir/eio-test-file", "eio-test-dir/eio-test-file-2", 0, res_cb, "link");
176     event_loop ();
177    
178     eio_rename ("eio-test-dir/eio-test-file", "eio-test-dir/eio-test-file-renamed", 0, res_cb, "rename");
179     event_loop ();
180    
181     eio_unlink ("eio-test-dir/eio-fifo", 0, res_cb, "unlink");
182     eio_unlink ("eio-test-dir/eio-symlink", 0, res_cb, "unlink");
183     eio_unlink ("eio-test-dir/eio-test-file-2", 0, res_cb, "unlink");
184     eio_unlink ("eio-test-dir/eio-test-file-renamed", 0, res_cb, "unlink");
185     event_loop ();
186    
187     eio_rmdir ("eio-test-dir", 0, res_cb, "rmdir");
188 root 1.1 event_loop ();
189     }
190     while (0);
191    
192     return 0;
193     }
194