Remove building with NOCRYPTO option
[minix.git] / external / bsd / libevent / dist / sample / event-test.c
blob339c663c0e218583b27bf26066a4f2570575ef8e
1 /* $NetBSD: event-test.c,v 1.1.1.1 2013/04/11 16:43:31 christos Exp $ */
2 /*
3 * XXX This sample code was once meant to show how to use the basic Libevent
4 * interfaces, but it never worked on non-Unix platforms, and some of the
5 * interfaces have changed since it was first written. It should probably
6 * be removed or replaced with something better.
8 * Compile with:
9 * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
12 #include <event2/event-config.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #ifndef WIN32
17 #include <sys/queue.h>
18 #include <unistd.h>
19 #include <sys/time.h>
20 #else
21 #include <winsock2.h>
22 #include <windows.h>
23 #endif
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
30 #include <event.h>
32 static void
33 fifo_read(evutil_socket_t fd, short event, void *arg)
35 char buf[255];
36 int len;
37 struct event *ev = arg;
38 #ifdef WIN32
39 DWORD dwBytesRead;
40 #endif
42 /* Reschedule this event */
43 event_add(ev, NULL);
45 fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
46 (int)fd, event, arg);
47 #ifdef WIN32
48 len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
50 /* Check for end of file. */
51 if (len && dwBytesRead == 0) {
52 fprintf(stderr, "End Of File");
53 event_del(ev);
54 return;
57 buf[dwBytesRead] = '\0';
58 #else
59 len = read(fd, buf, sizeof(buf) - 1);
61 if (len == -1) {
62 perror("read");
63 return;
64 } else if (len == 0) {
65 fprintf(stderr, "Connection closed\n");
66 return;
69 buf[len] = '\0';
70 #endif
71 fprintf(stdout, "Read: %s\n", buf);
74 int
75 main(int argc, char **argv)
77 struct event evfifo;
78 #ifdef WIN32
79 HANDLE socket;
80 /* Open a file. */
81 socket = CreateFileA("test.txt", /* open File */
82 GENERIC_READ, /* open for reading */
83 0, /* do not share */
84 NULL, /* no security */
85 OPEN_EXISTING, /* existing file only */
86 FILE_ATTRIBUTE_NORMAL, /* normal file */
87 NULL); /* no attr. template */
89 if (socket == INVALID_HANDLE_VALUE)
90 return 1;
92 #else
93 struct stat st;
94 const char *fifo = "event.fifo";
95 int socket;
97 if (lstat(fifo, &st) == 0) {
98 if ((st.st_mode & S_IFMT) == S_IFREG) {
99 errno = EEXIST;
100 perror("lstat");
101 exit(1);
105 unlink(fifo);
106 if (mkfifo(fifo, 0600) == -1) {
107 perror("mkfifo");
108 exit(1);
111 /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
112 #ifdef __linux
113 socket = open(fifo, O_RDWR | O_NONBLOCK, 0);
114 #else
115 socket = open(fifo, O_RDONLY | O_NONBLOCK, 0);
116 #endif
118 if (socket == -1) {
119 perror("open");
120 exit(1);
123 fprintf(stderr, "Write data to %s\n", fifo);
124 #endif
125 /* Initalize the event library */
126 event_init();
128 /* Initalize one event */
129 #ifdef WIN32
130 event_set(&evfifo, (evutil_socket_t)socket, EV_READ, fifo_read, &evfifo);
131 #else
132 event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
133 #endif
135 /* Add it to the active events, without a timeout */
136 event_add(&evfifo, NULL);
138 event_dispatch();
139 #ifdef WIN32
140 CloseHandle(socket);
141 #endif
142 return (0);