xref: /openbsd/regress/lib/libevent/test-eof.c (revision 5f15e431)
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 #ifdef HAVE_SYS_TIME_H
16 #include <sys/time.h>
17 #endif
18 #ifdef HAVE_SYS_SOCKET_H
19 #include <sys/socket.h>
20 #endif
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <errno.h>
29 
30 #include <event.h>
31 #include <evutil.h>
32 
33 int test_okay = 1;
34 int called = 0;
35 
36 static void
read_cb(int fd,short event,void * arg)37 read_cb(int fd, short event, void *arg)
38 {
39 	char buf[256];
40 	int len;
41 
42 	len = recv(fd, buf, sizeof(buf), 0);
43 
44 	printf("%s: read %d%s\n", __func__,
45 	    len, len ? "" : " - means EOF");
46 
47 	if (len) {
48 		if (!called)
49 			event_add(arg, NULL);
50 	} else if (called == 1)
51 		test_okay = 0;
52 
53 	called++;
54 }
55 
56 #ifndef SHUT_WR
57 #define SHUT_WR 1
58 #endif
59 
60 int
main(int argc,char ** argv)61 main (int argc, char **argv)
62 {
63 	struct event ev;
64 	const char *test = "test string";
65 	int pair[2];
66 
67 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
68 		return (1);
69 
70 
71 	send(pair[0], test, strlen(test)+1, 0);
72 	shutdown(pair[0], SHUT_WR);
73 
74 	/* Initalize the event library */
75 	event_init();
76 
77 	/* Initalize one event */
78 	event_set(&ev, pair[1], EV_READ, read_cb, &ev);
79 
80 	event_add(&ev, NULL);
81 
82 	event_dispatch();
83 
84 	return (test_okay);
85 }
86 
87