Remove building with NOCRYPTO option
[minix.git] / external / bsd / libevent / dist / sample / time-test.c
blobe0754718094f1fbb449ca69e9a6bf9bc46384aa8
1 /* $NetBSD: time-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 time-test time-test.c -L/usr/local/lib -levent
12 #include <sys/types.h>
14 #include <event2/event-config.h>
16 #include <sys/stat.h>
17 #ifndef WIN32
18 #include <sys/queue.h>
19 #include <unistd.h>
20 #endif
21 #include <time.h>
22 #ifdef _EVENT_HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25 #include <fcntl.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
31 #include <event2/event.h>
32 #include <event2/event_struct.h>
33 #include <event2/util.h>
35 #ifdef WIN32
36 #include <winsock2.h>
37 #endif
39 struct timeval lasttime;
41 int event_is_persistent;
43 static void
44 timeout_cb(evutil_socket_t fd, short event, void *arg)
46 struct timeval newtime, difference;
47 struct event *timeout = arg;
48 double elapsed;
50 evutil_gettimeofday(&newtime, NULL);
51 evutil_timersub(&newtime, &lasttime, &difference);
52 elapsed = difference.tv_sec +
53 (difference.tv_usec / 1.0e6);
55 printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
56 (int)newtime.tv_sec, elapsed);
57 lasttime = newtime;
59 if (! event_is_persistent) {
60 struct timeval tv;
61 evutil_timerclear(&tv);
62 tv.tv_sec = 2;
63 event_add(timeout, &tv);
67 int
68 main(int argc, char **argv)
70 struct event timeout;
71 struct timeval tv;
72 struct event_base *base;
73 int flags;
75 #ifdef WIN32
76 WORD wVersionRequested;
77 WSADATA wsaData;
79 wVersionRequested = MAKEWORD(2, 2);
81 (void)WSAStartup(wVersionRequested, &wsaData);
82 #endif
84 if (argc == 2 && !strcmp(argv[1], "-p")) {
85 event_is_persistent = 1;
86 flags = EV_PERSIST;
87 } else {
88 event_is_persistent = 0;
89 flags = 0;
92 /* Initalize the event library */
93 base = event_base_new();
95 /* Initalize one event */
96 event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
98 evutil_timerclear(&tv);
99 tv.tv_sec = 2;
100 event_add(&timeout, &tv);
102 evutil_gettimeofday(&lasttime, NULL);
104 event_base_dispatch(base);
106 return (0);