xref: /third_party/libfuse/example/poll_client.c (revision 6881f68f)
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 GPLv2.
7  See the file COPYING.
8*/
9
10/** @file
11 *
12 * This program tests the poll.c example file systsem.
13 *
14 * Compile with:
15 *
16 *      gcc -Wall poll_client.c -o poll_client
17 *
18 * ## Source code ##
19 * \include poll_client.c
20 */
21
22#include <fuse_config.h>
23
24#include <sys/select.h>
25#include <sys/time.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <ctype.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <errno.h>
34
35#define FSEL_FILES	16
36
37int main(void)
38{
39	static const char hex_map[FSEL_FILES] = "0123456789ABCDEF";
40	int fds[FSEL_FILES];
41	int i, nfds, tries;
42
43	for (i = 0; i < FSEL_FILES; i++) {
44		char name[] = { hex_map[i], '\0' };
45		fds[i] = open(name, O_RDONLY);
46		if (fds[i] < 0) {
47			perror("open");
48			return 1;
49		}
50	}
51	nfds = fds[FSEL_FILES - 1] + 1;
52
53	for(tries=0; tries < 16; tries++) {
54		static char buf[4096];
55		fd_set rfds;
56		int rc;
57
58		FD_ZERO(&rfds);
59		for (i = 0; i < FSEL_FILES; i++)
60			FD_SET(fds[i], &rfds);
61
62		rc = select(nfds, &rfds, NULL, NULL, NULL);
63
64		if (rc < 0) {
65			perror("select");
66			return 1;
67		}
68
69		for (i = 0; i < FSEL_FILES; i++) {
70			if (!FD_ISSET(fds[i], &rfds)) {
71				printf("_:   ");
72				continue;
73			}
74			printf("%X:", i);
75			rc = read(fds[i], buf, sizeof(buf));
76			if (rc < 0) {
77				perror("read");
78				return 1;
79			}
80			printf("%02d ", rc);
81		}
82		printf("\n");
83	}
84	return 0;
85}
86