MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8 
9 
10 #ifdef WIN32
11 #include <winsock2.h>
12 #endif
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/time.h>
16 #ifdef HAVE_SYS_SOCKET_H
17 #include <sys/socket.h>
18 #endif
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <errno.h>
26 
27 #include <event.h>
28 #include <evutil.h>
29 
30 int pair[2];
31 int test_okay = 1;
32 int called = 0;
33 
34 static void
35 write_cb(int fd, short event, void *arg)
36 {
37  const char *test = "test string";
38  int len;
39 
40  len = write(fd, test, strlen(test) + 1);
41 
42  printf("%s: write %d%s\n", __func__,
43  len, len ? "" : " - means EOF");
44 
45  if (len > 0) {
46  if (!called)
47  event_add(arg, NULL);
48  close(pair[0]);
49  } else if (called == 1)
50  test_okay = 0;
51 
52  called++;
53 }
54 
55 int
56 main (int argc, char **argv)
57 {
58  struct event ev;
59 
60 #ifndef WIN32
61  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
62  return (1);
63 #endif
64 
65  if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
66  return (1);
67 
68  /* Initalize the event library */
69  event_init();
70 
71  /* Initalize one event */
72  event_set(&ev, pair[1], EV_WRITE, write_cb, &ev);
73 
74  event_add(&ev, NULL);
75 
76  event_dispatch();
77 
78  return (test_okay);
79 }
80