Tizen 2.1 base
[framework/base/fuse.git] / example / fselclient.c
1 /*
2   FUSE fselclient: FUSE select example client
3   Copyright (C) 2008       SUSE Linux Products GmbH
4   Copyright (C) 2008       Tejun Heo <teheo@suse.de>
5
6   This program can be distributed under the terms of the GNU GPL.
7   See the file COPYING.
8
9   gcc -Wall fselclient.c -o fselclient
10 */
11
12 #include <sys/select.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <ctype.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22
23 #define FSEL_FILES      16
24
25 int main(void)
26 {
27         static const char hex_map[FSEL_FILES] = "0123456789ABCDEF";
28         int fds[FSEL_FILES];
29         int i, nfds;
30
31         for (i = 0; i < FSEL_FILES; i++) {
32                 char name[] = { hex_map[i], '\0' };
33                 fds[i] = open(name, O_RDONLY);
34                 if (fds[i] < 0) {
35                         perror("open");
36                         return 1;
37                 }
38         }
39         nfds = fds[FSEL_FILES - 1] + 1;
40
41         while (1) {
42                 static char buf[4096];
43                 fd_set rfds;
44                 int rc;
45
46                 FD_ZERO(&rfds);
47                 for (i = 0; i < FSEL_FILES; i++)
48                         FD_SET(fds[i], &rfds);
49
50                 rc = select(nfds, &rfds, NULL, NULL, NULL);
51
52                 if (rc < 0) {
53                         perror("select");
54                         return 1;
55                 }
56
57                 for (i = 0; i < FSEL_FILES; i++) {
58                         if (!FD_ISSET(fds[i], &rfds)) {
59                                 printf("_:   ");
60                                 continue;
61                         }
62                         printf("%X:", i);
63                         rc = read(fds[i], buf, sizeof(buf));
64                         if (rc < 0) {
65                                 perror("read");
66                                 return 1;
67                         }
68                         printf("%02d ", rc);
69                 }
70                 printf("\n");
71         }
72 }