]> arthur.barton.de Git - netatalk.git/blob - libevent/test/test-weof.c
Add libevent
[netatalk.git] / libevent / test / test-weof.c
1 /*
2  * Compile with:
3  * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4  */
5 #include "event2/event-config.h"
6
7 #ifdef WIN32
8 #include <winsock2.h>
9 #else
10 #include <unistd.h>
11 #endif
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #ifdef _EVENT_HAVE_SYS_TIME_H
15 #include <sys/time.h>
16 #endif
17 #ifdef _EVENT_HAVE_SYS_SOCKET_H
18 #include <sys/socket.h>
19 #endif
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <signal.h>
25 #include <errno.h>
26
27 #include "event2/event.h"
28 #include "event2/event_struct.h"
29 #include "event2/event_compat.h"
30 #include "event2/util.h"
31
32 #ifdef _EVENT___func__
33 #define __func__ _EVENT___func__
34 #endif
35
36 evutil_socket_t pair[2];
37 int test_okay = 1;
38 int called = 0;
39
40 static void
41 write_cb(evutil_socket_t fd, short event, void *arg)
42 {
43         const char *test = "test string";
44         int len;
45
46         len = send(fd, test, (int)strlen(test) + 1, 0);
47
48         printf("%s: write %d%s\n", __func__,
49             len, len ? "" : " - means EOF");
50
51         if (len > 0) {
52                 if (!called)
53                         event_add(arg, NULL);
54                 evutil_closesocket(pair[0]);
55         } else if (called == 1)
56                 test_okay = 0;
57
58         called++;
59 }
60
61 int
62 main(int argc, char **argv)
63 {
64         struct event ev;
65
66 #ifdef WIN32
67         WORD wVersionRequested;
68         WSADATA wsaData;
69         int     err;
70
71         wVersionRequested = MAKEWORD(2, 2);
72
73         err = WSAStartup(wVersionRequested, &wsaData);
74 #endif
75
76 #ifndef WIN32
77         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
78                 return (1);
79 #endif
80
81         if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
82                 return (1);
83
84         /* Initalize the event library */
85         event_init();
86
87         /* Initalize one event */
88         event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
89
90         event_add(&ev, NULL);
91
92         event_dispatch();
93
94         return (test_okay);
95 }
96